# hdu1003求最大和子序列

2017-12-01 08:52:56来源:CSDN作者:Soul_97人点击

# Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 263653    Accepted Submission(s): 62656

Problem DescriptionGiven 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.

InputThe 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).

OutputFor 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#include<bits/stdc++.h>using namespace std;int main(){    int t;    int a[100001];    int d[100001];    cin>>t;    int p=1;    while(t --)    {    	memset(a,0,sizeof(a));    	memset(d,0,sizeof(d));    	int n;    	int maxv,endv;    	cin>>n;    	for(int i=1;i<=n;i ++)    	cin>>a[i];    	d[1] = a[1];    	for(int j=2;j<=n;j++)    	{    		if(d[j-1] < 0)    		d[j] = a[j];    		else    		d[j] = d[j-1] + a[j];		}		maxv = d[1];		endv = 1;		for(int i=2;i<=n;i++)		{			if(maxv < d[i])			{				maxv = d[i];				endv = i;			}		}		int tmp = 0;		int startv;		for(int j=endv;j>=1;j--)		{			tmp += a[j];			if(tmp == maxv)			startv = j;		}		printf("Case %d:/n%d %d %d/n",p,maxv,startv,endv);		p++;		if(t)		cout<<endl;	}    return 0;}`