hdu 1003 Max Sum - dp

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(注意第一个5是表示一共5个数)

7 0 6 -1 1 -6 7 -5

 

Sample Output

Case 1: 14 1 4

Case 2: 7 1 6

 

思路:

注意考虑全为负的情况。

 

源代码:

 

 

 1 #include <iostream>
 2 #include<stdio.h>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8   int cas,s,e,s1;
 9   int j=1;
10   scanf("%d",&cas);
11   while(cas--)
12   {int sum=0,max=-100000;
13       int n,num;
14       scanf("%d",&n);
15       s1=1;
16       for(int i=1;i<=n;i++)
17         {
18 
19             scanf("%d",&num);
20             sum+=num;
21             if(sum>max)
22             {max=sum;
23             s=s1;
24             e=i;}
25             if(sum<0)
26             {
27                 sum=0;
28                 s1=i+1;
29             }
30 
31         }if(j-1)printf("\n");
32        printf("Case %d:\n",j++);
33         printf("%d %d %d\n",max,s,e);
34   }
35     return 0;
36 }

 

 

 

 

posted @ 2013-08-06 00:36  小の泽  阅读(113)  评论(0编辑  收藏  举报