【HDU】 1003 Max Sum(最大连续子序列和)
来源:互联网 发布:linux查找文件内容 编辑:程序博客网 时间:2024/05/17 22:48
Problem 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 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
Author
Ignatius.L
Recommend
We have carefully selected several similar problems for you: 1176 1069 2084 1058 1203
AC代码:
#include<cstdio>int a[100000+10];int main(){ int t,n,k=1; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]); int max=-1001,st,endd,sum=0,st1=1; //注意整数的范围 for(int i=1;i<=n;i++) { sum+=a[i]; if(sum>max) { max=sum; st=st1; endd=i; } if(sum<0) { sum=0; st1=i+1; //st1是临时的开始点,如果后面的sum<max,这个开始点也就不会计入 } } printf("Case %d:\n%d %d %d\n",k++,max,st,endd); if(t!=0) { printf("\n"); } } return 0;}
阅读全文
0 0
- HDU 1003 Max Sum(最大连续子序列和)
- HDU 1003----Max Sum(最大连续子序列和)
- 【HDU】 1003 Max Sum(最大连续子序列和)
- hdu 1003 MAX SUM(最大连续子序列和)
- HDU 1003 Max Sum 最大连续子序列的和
- HDU 1003 Max Sum(dp,最大连续子序列和)
- HDU 1003 Max Sum(dp,最大连续子序列和)
- HDU 1003 Max Sum 最大连续子序列和
- 【最大连续子序列和dp】hdu 1003 Max Sum
- hdu 1003 Max Sum (最大连续子序列)
- hdu 1003 Max Sum (最大连续子序列)
- HDU 1003 Max Sum(最大连续子序列和 经典DP)
- hdu 题目1003 Max Sum (最大连续子序列和)
- hdu 1003 Max Sum(最大连续子序列和) (学了一下分治)
- HDU 1003 Max Sum(最大连续子段和)
- HDU 1003 Max Sum(最大连续子列和)
- hdu 1003 Max Sum(分而治之||最大连续子序列 dp)
- HDU 1003 Max Sum【最大连续子序列】
- javeBean与xml之间的相互转换
- JS打印JSON对象和List
- 两圆相交,求相交的面积模板
- Automation testing experience -- from 梅子
- 自定义ProgressBar png样式支持直角显示
- 【HDU】 1003 Max Sum(最大连续子序列和)
- 在Spring MVC controller中配切点
- 基于Zigbee温湿度监测系统
- 数组查找元素
- shell脚本——判断文件的属性、内容、、
- Swift_学习笔记_泛型
- 知道别人的端口,如何看出端口有什么漏洞
- redis配置详解
- Spring源码阅读(七)—AOP创建代理