Max Sum
Time Limit: 1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64u
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 is b etween-1000 and 1000).
Output
For the 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 4 case 2:7 1 6
Analysis
state transition equation Dp[i] = max (Dp[i-1] + a[i], a[i] )
1#include <stdio.h>2#include <string.h>3 4 inta[100002];5 6 Main ()7 {8 intt,n,kkk=1, I,j;9 Tenscanf"%d",&T); One while(t--) A { - intmax,max_s,max_e,sum,sum_s; - //memset theprintf"Case %d:\n", kkk++); - -scanf"%d",&N); - for(i=1; i<=n;i++) + { -scanf"%d",&a[i]); + } A atmax = SUM = a[1]; -max_s = Max_e = sum_s =1; - - //printf ("sum =%d max =%d\n", Sum,max); - for(i=2; i<=n;i++) - { in if(sum + a[i] <A[i]) - { tosum =A[i]; +sum_s =i; - } the Elsesum = sum +A[i]; * $ if(Sum >max)Panax Notoginseng { -Max =sum; themax_s =sum_s; +Max_e =i; A } the //printf ("sum =%d max =%d\n", Sum,max); + } -printf"%d%d%d\n", max,max_s,max_e); $ if(T) $printf"\ n"); - } -}
Max Sum (maximum substring and)