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 Input25 6-1 5 4-77 0 6-1 1-6 7-5
Sample outputcase 1:14 1 4Case 2:7 1 6
Authorignatius.l
Recommendwe has carefully selected several similar problems for you:1176 1087 1069 2084 1058 If all are negative, naturally find the largest negative number, otherwise you can use the dynamic State planning method, if the sum<0, then there is no need to continue to save, will only affect the back, the back of the smaller, this time the sum is 0. Code:
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#defineINF 0x3f3f3f3f#defineMAX 1000using namespacestd;intn,m,d;intMain () {scanf ("%d",&N); for(inti =1; I <= N;i + +) { if(I >1) Putchar ('\ n'); scanf ("%d",&m); intL =1, sum =0, Maxsum =0, a =0, B =0, Maxnum =-inf,t; for(intj =1; J <= M;j + +) {scanf ("%d",&d); if(D >maxnum) {Maxnum=D; T=J; } Sum+=D; if(Sum <0) {sum=0; L= j +1; Continue; } if(Sum >maxsum) {Maxsum=sum; A=l; b=J; } } if(!a) {a= B =T; Maxsum=Maxnum; } printf ("Case %d:\n%d%d%d\n", i,maxsum,a,b); }}
HDU 1003 Max Sum