HDOJ-1003-Max Sum

HDOJ-1003

Max Sum* 【HDOJ-1003-Max Sum】Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 342081 Accepted Submission(s): 81313
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
ansi,ansj分别为从第几个数开始和结束。
做法就是dp(动态规划),dp[i+1]就是前i个数所能得到的最大和,如果前一个dp小于0,那么从这个数开始重新计算,用ii暂时存储新的ansi,如果其中某一个dp大于了maxx(最大的dp),那么就让maxx=这个dp,顺便让ansi与ansj的值刷新成ii和现在的j
ps:其实可以一边读一遍操作,懒得改了
#include using namespace std; typedef long long ll; int t,h,i,j,n,a[100005],ansi,ansj,maxx,ii; int dp[100005]; void solve() { scanf("%d",&n); for(i=0; imaxx) { maxx=dp[i+1]; ansj=i+2; ansi=ii; } //printf("%d ",dp[i+1]); } printf("Case %d:\n%d %d %d\n",h-t,maxx,ansi,ansj); if(t!=0) printf("\n"); } int main() { /* freopen("data.in","r",stdin); freopen("data.out","w",stdout); */ scanf("%d",&t); h=t; while(t--) solve(); return 0; }

    推荐阅读