Max SumTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 178139 Accepted Submission (s): 41558
Problem Descriptiongiven 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.
Inputthe 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 is b etween-1000 and 1000).
Outputfor Each test case, you should output of the lines. The first line was "Case #:", # means the number of the the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end posi tion of the sub-sequence. If there is more than one result, output the first one. Output a blank line between cases.
Sample Input
25 6-1 5 4-77 0 6-1 1-6 7-5
Sample Output
Case 1:14 1 4Case 2:7 1 6
talk about dynamic planning again! The specific code is as follows:
#include <iostream> #include <cstdio> #include <cstring>using namespaceStd; int Dp[100001]; int A[100001]; int S[100001]; int Main () { int T,K,Count=0;Cin>>T; while (T--) {Cin>>K;Count++;Memset(A,0 ,sizeof( A));Memset(Dp,0 ,sizeof( Dp));S[0]=S[1]=1; for (int I=1;I<=K;I++) {Cin>>A[I]; } for (int I=1;I<=K;I + +) { if( Dp[I-1]+A[I]>=A[I]) {Dp[I]=Dp[I-1]+A[I];S[I]=S[I-1]; } Else { Dp[I]=A[I];S[I]=I; }} int Start=1,End=1; int Max=Dp[1]; for (int I=2;I<=K;I + +) { if( Max<Dp[I]) {Max=Dp[I];Start=S[I];End=I; } }cout<<"Case"<<Count<<":"<<Endl;cout<<Max<<" "<<Start<<" "<<End<<Endl; if (T!=0)cout<<Endl; } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hangzhou Electric (HDU) ACM 1003 Max Sum