HDU 1003 Max Sum(最大子序列和)

 

Max Sum

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


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
//坑爹 两组测试数组之间要换行 最末一组测试数组不要换行
//坑爹 貌似没说明如果某个状态算前面和只算自身一样大 怎么搞?
//AC代码
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
const int N=100000+10;
int dp[N];
int a[N];
int main(){
	int i,j,t,n,le,ri,big,re1,re2;
	scanf("%d",&t);
     for(j=1;j<=t;j++){
	    scanf("%d",&n);
	    scanf("%d",&a[1]);
	    big=dp[1]=a[1];
	    le=ri=re1=re2=1;
	    for(i=2;i<=n;i++){
	    	scanf("%d",&a[i]);
	    	if(dp[i-1]+a[i]>a[i]){
	    		dp[i]=dp[i-1]+a[i];
	    		ri++;
			}  		
			else{
				dp[i]=a[i];
				le=i;
				ri=i;
			}
			if(dp[i]>big)
			  {
			  	big=dp[i];
			  	re1=le;
			  	re2=ri;
			  }
		}
	       printf("Case %d:\n",j);
	       printf("%d %d %d\n",big,re1,re2);
	       if(j!=t)
	       printf("\n");
	}
	return 0;
}


你可能感兴趣的:(HDU 1003 Max Sum(最大子序列和))