首页 > 代码库 > hdu 1003

hdu 1003

Max Sum
Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u

Submit Status

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

25 6 -1 5 4 -77 0 6 -1 1 -6 7 -5

Sample Output

Case 1:14 1 4Case 2:7 1 6

题意不说 和hdu1231很相似 刚刚写了1231,但被这题坑了 maxsum我开始初值设了0 一直wa
后面测试数据才发现自己错了
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>typedef long long ll;#define N 100005using namespace std;int a[N];int b[N];ll dp[N];int main(){    int t;    int n;    int i,j;    int first,next;    scanf("%d",&t);    for(j=1;j<=t;j++)    {        ll maxsum=-99999999;        scanf("%d",&n);        for(i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }        memset(dp,0,sizeof(dp));        first=1;        for(i=1;i<=n;i++)        {            if(dp[i-1]+a[i]>=a[i])                dp[i]=dp[i-1]+a[i];            else            {                dp[i]=a[i];                first=i;            }            b[i]=first;            maxsum=max(maxsum,dp[i]);        }        for(i=1;i<=n;i++)        {            if(maxsum==dp[i])            {                next=i;                //break;            }        }        printf("Case %d:\n",j);        if(j!=t)            printf("%I64d %d %d\n\n",maxsum,b[next],next);        else            printf("%I64d %d %d\n",maxsum,b[next],next);    }    return 0;}

 

hdu 1003