hdu 1003 Max Sum (DP)

Max Sum

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


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
 

 

Author
Ignatius.L
 

 

Recommend
We have carefully selected several similar problems for you:  1421 1024 1505 2845 2577 
 
简单dp思想:
 1 //15 MS    236 KB    Visual C++    
 2 #include<stdio.h>
 3 int main(void)
 4 {
 5     int t,n,k=1;
 6     int a;
 7     scanf("%d",&t);
 8     while(t--)
 9     {
10         if(k>1) printf("\n");
11         scanf("%d",&n);
12         int l=1,r=1,sum=-0x7ffffff,tmp=0,tl=1;
13         for(int i=1;i<=n;i++){
14             scanf("%d",&a);
15             tmp+=a;
16             if(tmp>sum){
17                 sum=tmp;
18                 l=tl;
19                 r=i;
20             }
21             if(tmp<0){
22                 tmp=0;
23                 tl=i+1;          
24             }
25         }
26         printf("Case %d:\n",k++);
27         printf("%d %d %d\n",sum,l,r);
28     }
29     return 0;
30 }
31 /*
32 
33 30
34 3 -1 0 -1
35 3 -1 0 1
36 3 -1 -2 -3
37 
38 */

 

posted @ 2014-04-23 21:30  heaventouch  阅读(242)  评论(0编辑  收藏  举报