Max Sum
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 156637 Accepted Submission (s): 36628
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 6Analysis: The problem is to find the largest continuous sub-sequence and the output of the maximum continuous sub-sequence and, starting subscript, terminating subscript. Very similar to the HDU 1231.
1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <string.h>5 using namespacestd;6 Const intMAX =10010;7 intDp[max],a;8 intMain ()9 {Ten intT,i,j,n,left,right,maxn,flag,num; Onescanf"%d",&T); A for(j=1; j<=t;j++) - { -scanf"%d",&n); themaxn=-9999; -num=0; -left=right=flag=1; - for(i=1; i<=n;i++) + { -scanf"%d",&a); +num+=A; A if(num>MAXN) at { -maxn=num; -left=Flag; -right=i; - } - if(num<0) in { -num=0; toflag=i+1; + } - } the if(j!=1) printf ("\ n"); *printf"Case %d:\n", j); $printf"%d%d%d\n", maxn,left,right);Panax Notoginseng } - return 0; the}
HDU 1003 Max Sum (max consecutive sub-order and)