HDU1003 Max Sum
来源:互联网 发布:昆仑虚手游刷元宝软件 编辑:程序博客网 时间:2024/06/05 05:51
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 194034 Accepted Submission(s): 45254
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
25 6 -1 5 4 -77 0 6 -1 1 -6 7 -5
Sample Output
Case 1:14 1 4Case 2:7 1 6说实话是看了别人的思路过的。基本思路就是一个一个加起来找出最大值,当累加的值为负数时,就归为零,从下一个继续开始。#include<stdio.h>#define INF 0x3f3f3f3fint main(){ int t,n,a,i,j,sum,max,l,r,cas=0; scanf("%d",&t); while(t--) { cas++; sum=j=l=r=0; max=-INF; scanf("%d",&n); for(i=1; i<=n; i++) { scanf("%d",&a); sum+=a; if(sum>max)//比较当前sum与之前最大的连续子序列和,一旦比之前的max更大,就进行替换。 { max=sum; l=j+1; r=i; } if(sum<0)//sum为负数,就归为零,重新开始。 { sum=0; j=i;//重新开始需将j进行更改。 } } printf("Case %d:\n",cas); printf("%d %d %d\n",max,l,r); if(t) printf("\n"); } return 0;}
2 0
- HDU1003 MAX SUM
- hdu1003 Max Sum
- HDU1003 Max Sum
- HDU1003: Max Sum
- HDU1003 Max Sum
- hdu1003 Max Sum
- hdu1003 Max Sum
- HDU1003 Max Sum
- HDU1003:Max Sum
- HDU1003-Max Sum
- HDU1003 Max Sum【DP】
- HDU1003 Max Sum
- hdu1003 Max Sum
- hdu1003 Max Sum
- 【HDU1003】【Max Sum】
- HDU1003---Max Sum
- HDU1003 Max Sum
- hdu1003 Max Sum
- jQuery用addClass方法同时增加多个class属性
- 【操作系统】U盘如何挂载到Linux系统上
- iOS程序启动的流程
- argc和*argv[]函数简记
- 数据结构课程设计-12月28日
- HDU1003 Max Sum
- Mac os charles抓包http/https的简单使用介绍(附3.10.1mac破解版下载链接)
- 设计模式系列一:单例模式
- 架构之路(1):目标
- 行为树-学习笔记(1)
- 编程赚钱的7个方法
- 一次生活引发的“Lua随机数生成”问题
- top 内存耗用:VSS/RSS/PSS/USS
- 关于RunLoop