【HDU 杭电 1003 Max Sum】
2016-08-08 19:32
218 查看
Max Sum
Problem Description
Given a sequence a[1],a[2],a[3]……a
, 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
Problem Description
Given a sequence a[1],a[2],a[3]……a
, 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
#include<queue> #include<stack> #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define K 100011 #define INF 0x3f3f3f using namespace std; int pa[K]; int st[K];//当前的最大连续子序列的最大和 int main() { int i,j; int T; int n; int ans;//最大连续子序列的和 int a,b;//子序列的首尾位置 int test=1; int p=0;//最大连续子序列和的尾位置 scanf("%d",&T); while(T--) { if(p) printf("\n"); p=1; scanf("%d",&n); for(i=1;i<=n;i++) scanf("%d",&pa[i]); st[1]=pa[1];ans=pa[1]; b=1; for(i=2;i<=n;i++) { st[i]=max(pa[i],pa[i]+st[i-1]); //当前连续子序列的最大和 if(st[i]>ans)//更新最大和 { ans=st[i]; b=i; } } a=b; for(i=b;i>=1;i--)//求首位置 { if(st[i]>=0) a=i; else break; } printf("Case %d:\n",test++); printf("%d %d %d\n",ans,a,b); } return 0; }
相关文章推荐
- 杭电hdu 1233 还是畅通工程 prim
- 杭电hdu 1862 EXCEL排序 qsort
- HDU--杭电--2066--一个人的旅行--最短路
- 杭电ACM ——>HDU-1008电梯
- 【二分图+简单题】杭电 hdu 1151 Air Raid
- 杭电1171--Big Event in HDU(多重…
- 杭电1171 Big Event in HDU
- HDU 5795 A Simple Nim (博弈) ---2016杭电多校联合第六场
- 杭电 HDU 1002 A + B Problem II Java 大数的初级用法
- 杭电hdu 2000 ASCII码排序
- HDU--杭电--1242--Rescue--广搜--又是一个所谓的优先队列起作用的题,擦,虽然比上一个容易,不过还是拿来摆摆
- HDU1290 献给杭电五十周年校庆的礼物
- 杭电(hdu)2544 最短路
- 杭电OJ(HDU)-ACMSteps-Chapter Three-《FatMouse' Trade》《今年暑假不AC》《排名》《开门人和关门人》
- HDU杭电acm题目分类大全
- hdu 杭电 2473 Junk-Mail Filter
- 杭电 hdu 5625 Clarke and chemistry【水题】
- hdu 4554 杭电超级大水题
- 【最长上升子序列】杭电 hdu 1257 最少拦截系统
- 【杭电oj】1171 - Big Event in HDU(01背包)