HDU 1003 Max Sum

Problem Description
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
 

 

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
 

 

Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
 

 

Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
 

 

Sample Output
Case 1:
14 1 4
Case 2:
7 1 6
这个题就是求最大联系和,我靠,我竟然在这种题上WA了啊,后来又PE了,人生啊!!!!转战HDU的第一题,难道是HDU想给我一个下马威
#include<map>
#include<set>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define  inf 0x0f0f0f0f

using namespace std;

const double pi=acos(-1.0);
const double eps=1e-8;
typedef pair<int,int>pii;

int a[100001];
int x,y,maxtemp,maxsum,tempx;

void get_max(int n)
{
    x=1;y=1,tempx=1;
    maxtemp=maxsum=a[1];
    for (int i=2;i<=n;i++)
    {
        if (maxtemp<0) {maxtemp=a[i];tempx=i;}
        else maxtemp+=a[i];
        if (maxsum<maxtemp)
        {
            maxsum=maxtemp;
            x=tempx;
            y=i;
        }
    }
    return;
}

int main()
{
   // freopen("in.txt","r",stdin);

    int t,n;
    scanf("%d",&t);
    for (int k=1;k<=t;k++)
    {
        scanf("%d",&n);
        for (int i=1;i<=n;i++) scanf("%d",&a[i]);
        get_max(n);
        printf("Case %d:\n",k);
        printf("%d %d %d\n",maxsum,x,y);
        if (k!=t) printf("\n");
    }
    //fclose(stdin);
    return 0;
}

 

posted @ 2014-04-24 15:05  Hust_BaoJia  阅读(137)  评论(0编辑  收藏  举报
努力