Max Sum hd 1003

来源:互联网 发布:h5直播源码 编辑:程序博客网 时间:2024/05/11 04:23
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

题意:给出一个数列,求出连续数列和的最大值,并且输出连续数列的起始点和终点

思路: 这道题也是很简单的,用now和before记录现在和之前的和,然后依次替换。。最后求出最大值

#include<cstdio>#include<algorithm>using namespace std;int main(){int t=1;int m,n,now,before,max,i,e,s,x;scanf("%d",&n);while(n--){scanf("%d",&m);for(i=1;i<=m;i++){scanf("%d",&now);if(i==1){max=before=now;s=e=x=1;}else{if(now>before+now){before=now;x=i;}else before=before+now;}if(max<before){max=before;s=x;e=i;}}printf("Case %d:\n%d %d %d\n",t++,max,s,e);if(n)printf("\n");}return 0;}


0 0