HDU 1003 Max Sum(动态规划)

                             Max Sum

                              Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

                                                 Total Submission(s): 107407    Accepted Submission(s): 24742

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
 
 
 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     int T, N;
 6     scanf("%d", &T);
 7     for(int i = 1; i <= T; i++)
 8     {
 9         int sum = 0, k = 1, nTemp;
10         int max_sum = -0x7ffffff;
11         int sSeq, eSeq;
12 
13         scanf("%d", &N);
14         for(int j = 1; j <= N; j++)
15         {
16             scanf("%d", &nTemp);
17             sum += nTemp;
18             if(sum > max_sum)
19             {
20                 max_sum = sum;
21                 sSeq = k;
22                 eSeq = j;
23             }
24             if(sum < 0)        //如果总和小于0,则其实位置从下一个开始
25             {
26                 k = j + 1;
27                 sum = 0;
28             }
29         }
30         if(i != 1)
31             printf("\n");
32         printf("Case %d:\n", i);
33         printf("%d %d %d\n", max_sum, sSeq, eSeq);
34     }
35     return 0;
36 }

 总结:发现自己题目做的太少了,以至于连很简单的题目都做不出来,只能说自己还不够努力吧!

posted @ 2013-05-25 10:16  Dreamcaihao  阅读(203)  评论(0编辑  收藏  举报