HDU1003 DP

Max Sum

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


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
题意:
求最大连续子序列和
代码:
 1 //
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5 using namespace std;
 6 int main()
 7 {
 8     int t,n;
 9     scanf("%d",&t);
10     for(int k=1;k<=t;k++)
11     {
12         int dp[100005][3];
13         memset(dp,0,sizeof(dp));
14         int a[100005];
15         scanf("%d",&n);
16         for(int i=1;i<=n;i++)
17         scanf("%d",&a[i]);
18         dp[1][0]=a[1];
19         dp[1][1]=1;
20         dp[1][2]=1;
21         for(int i=2;i<=n;i++)
22         {
23             if(dp[i-1][0]<0)
24             {
25                 dp[i][0]=a[i];
26                 dp[i][1]=i;
27                 dp[i][2]=i;
28             }
29             else
30             {
31                 dp[i][2]=i;
32                 dp[i][1]=dp[i-1][1];
33                 dp[i][0]=a[i]+dp[i-1][0];
34             }
35         }
36         int tem=-200000000,temi;
37         for(int i=1;i<=n;i++)
38         {
39             if(dp[i][0]>tem)
40             {tem=dp[i][0];temi=i;}
41         }
42         printf("Case %d:\n",k);
43         printf("%d %d %d\n",dp[temi][0],dp[temi][1],dp[temi][2]);
44         if(k!=t) printf("\n");
45     }
46     return 0;
47 }

 

posted @ 2016-10-09 20:27  luckilzy  阅读(197)  评论(0编辑  收藏  举报