Max Sum
http://acm.hdu.edu.cn/showproblem.php?pid=1003
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
这是一类典型的动态规划题目,一维数组。
将数组中的数字逐一相加。
如果发现当前的和小于0,则抛弃前面一段,将sum重置为0。记录下一个为准最大段起始位置。
如果和大于等于0,则加上这一段。
如果和大于最大值max,记录这个max值,并更新最大段其实和终止位置。
注:
max初值必须小于数组中数据的最小值。
此题输出格式,当有多个测试用例的时候,最后一个不允许出现多余空行,其它相邻两个用例间加入一个空行。
http://acm.hdu.edu.cn/showproblem.php?pid=1003
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
这是一类典型的动态规划题目,一维数组。
将数组中的数字逐一相加。
如果发现当前的和小于0,则抛弃前面一段,将sum重置为0。记录下一个为准最大段起始位置。
如果和大于等于0,则加上这一段。
如果和大于最大值max,记录这个max值,并更新最大段其实和终止位置。
注:
max初值必须小于数组中数据的最小值。
此题输出格式,当有多个测试用例的时候,最后一个不允许出现多余空行,其它相邻两个用例间加入一个空行。
#include <stdio.h> #include <stdlib.h> int main (int argc, char const* argv[]) { int i, c, t, n, sum, max, first, last, temp; int a[100000]; scanf("%d", &t); c = 1; while (c <= t) { scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } sum = 0; max = -1001; first = 0; last = 0; temp = 1; for (i = 0; i < n; i++) { sum += a[i]; if (sum > max) { max = sum; first = temp; last = i + 1; } if (sum < 0) { sum = 0; temp = i + 2; } } printf("Case %d:\n", c); printf("%d %d %d\n", max, first, last); if (c != t) printf("\n"); c++; } return 0; }
发表评论
-
fhloj1051 投票
2013-07-04 19:42 0投票 源文件: b(.bas/.c/.cpp/.pas) 输 ... -
fhloj1050 足球赛
2013-07-04 19:36 567足球赛 源文件: a(.bas/.c/.cpp/.pas) ... -
fhloj1092 五子棋
2013-07-04 12:01 673五子棋 源文件: gobang(.bas/.c/.cpp/ ... -
fhloj1091 拼单词
2013-07-04 11:53 706拼单词 源文件: words ... -
fhloj1090 21点游戏
2013-07-04 11:44 60221点游戏 源文件: poker(.bas/.c/.cpp ... -
fhloj1089 帮奶奶算帐
2013-07-04 11:17 562帮奶奶算账 源代码:bill.bas/pas 输入文件:bil ... -
hdu1019 gcd和lcm
2012-12-06 15:09 744Least Common Multiple http://a ... -
hdu1021 推理规律
2012-12-06 09:24 892Fibonacci Again http://acm.hdu ... -
hud1008 电梯 迭代模拟计算
2012-12-04 18:24 991Elevator http://acm.hdu.edu.cn ... -
hdu1001 求和
2012-12-03 22:05 731Sum Problem http://acm.hdu.edu ... -
hdu1000 A+B
2012-12-03 18:37 783A + B Problem http://acm.hdu.e ... -
hdu2035 乘方取余
2012-12-02 18:02 1046人见人爱A^B http://acm.hdu.edu.cn/ ... -
hdu2034 差集
2012-12-02 17:43 817人见人爱A-B http://acm.hdu.edu.cn/ ... -
hdu2033 时间计算
2012-12-02 16:24 860人见人爱A+B http://acm.hdu.edu.cn/ ... -
hdu2081 字符串拼接
2012-12-01 14:35 780手机短号 http://acm.hdu.edu.cn/sho ... -
poj1163 树型结构动态规划和最大路径
2012-11-30 22:05 1151The Triangle http://poj.org/pr ... -
POJ1579递归函数定义
2012-11-30 21:58 814Function Run Fun http://poj.or ... -
POJ1050 最大子矩阵
2012-11-30 11:34 1161To the Maxhttp://poj.org/proble ...
相关推荐
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu2101AC代码
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
hdu1290 解题报告 献给杭电五十周年校庆的礼物 (切西瓜问题,即平面分割空间)
自己做的HDU ACM已经AC的题目
hdu 1166线段树代码
HDU最全ac代码
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu动态规划算法集锦
hdu题目分类
HDU图论题目分类
Hdu 1237 解题代码