hdu 1003

来源:互联网 发布:linux的shell用法 编辑:程序博客网 时间:2024/06/08 09:17

Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 252133 Accepted Submission(s): 59775

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

怎么说呢第二次做还它喵的先t后wa ,以前看题解过的题,他喵的跟没做一样,还不是自己的东西,这跟高中不一样,高中题型跟这个比来说高中的题型太单一了,主要,每天都做,很快就能做到类似的,很快就真正的属于自己了,现在,做了一年,基本没遇到几个类似的,每天都在触及自己的知识盲区.

#include<iostream>#include<cstring>#include<cstdio>#include<string>#include<cmath>#include<map>#include<functional>#include<queue>#include<vector>#include<algorithm>typedef long long ll ;const ll mod=100000+7;using namespace std;int num[mod];int a[mod];int main(){   int t;   while(~scanf("%d",&t))   {       int k=0;       while(t--)       {            int n,i,j;            scanf("%d",&n);            num[0]=0;            for(i=1;i<=n;i++)            {                 num[i]=0;                 scanf("%d",&a[i]);            }            for(i=1;i<=n;i++)            {                if(num[i-1]<=0) num[i]=a[i];                else                    num[i]=num[i-1]+a[i];            }            int sum=num[1],p2=1,p1;            for(i=1;i<=n;i++)            {                if(num[i]>sum)                {                 p2=i;                sum=num[i];                }            }            p1=p2;            int tmp=0;            for(i=p2;i>=1;i--)            {                tmp+=a[i];                if(tmp==sum)                {                    p1=i;                }            }           printf("Case %d:\n%d %d %d",++k,sum,p1,p2);           if(t!=0)            puts("\n");           else            puts("");       }   }    return 0;}
原创粉丝点击