HDU 1003 Max Sum 最大连续子序列和
2015-09-10 21:33
477 查看
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
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
最大子序列和,输出第一个和最后一个元素的位置,有相同的需要输出第一次出现的位置和最后一次出现的位置。
既然找最大子序列和,那么最大子序列最后一个必然是存在这个序列中的位置,不妨把每一个数字假设成最大子序列的最后一个位置,dp[1] = a[1],那么从dp[2]开始,dp[2]是最后一个,和它前面的dp[1]比较,如果dp[1]是正数,则dp[2] = a[2]+dp[1],如果是负数,就不加,dp[2] = a[2]。如果dp[1]是负数,a[2]+dp[1] < a[2],所以可以写成dp[2] = max(dp[1]+a[2],a[2]); 对后面么一个元素都这么推,就可以推出状态转移方程dp[i] = max(dp[i-1]+a[i],a[i]);
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
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
最大子序列和,输出第一个和最后一个元素的位置,有相同的需要输出第一次出现的位置和最后一次出现的位置。
既然找最大子序列和,那么最大子序列最后一个必然是存在这个序列中的位置,不妨把每一个数字假设成最大子序列的最后一个位置,dp[1] = a[1],那么从dp[2]开始,dp[2]是最后一个,和它前面的dp[1]比较,如果dp[1]是正数,则dp[2] = a[2]+dp[1],如果是负数,就不加,dp[2] = a[2]。如果dp[1]是负数,a[2]+dp[1] < a[2],所以可以写成dp[2] = max(dp[1]+a[2],a[2]); 对后面么一个元素都这么推,就可以推出状态转移方程dp[i] = max(dp[i-1]+a[i],a[i]);
#include"stdio.h" #include"iostream" #include"algorithm" #include"string.h" #include"stdlib.h" #include"queue" #define inf 7777777 using namespace std; int a[100010]; int dp[100010]; int main(void) { int T; scanf("%d",&T); for(int j = 1;j <= T;j++) { int n; scanf("%d",&n); scanf("%d",&a[1]); int maf; ///最大值 maf = dp[1] = a[1]; int loc2 = 1; int loc1; for(int i = 2;i <= n;i++) { scanf("%d",&a[i]); dp[i] = max(dp[i-1]+a[i],a[i]); if(maf <= dp[i]) ///记录下最大dp值和出现位置 { maf = dp[i]; loc2 = i; } } for(int i = loc2,t = maf;i > 0;i--) { t -= a[i]; if(t == 0) ///一直循环,每一次 t==0 的时候就是一个位置,最后一次的时候就是第一次出现的位置 { loc1 = i; } } printf("Case %d:\n%d %d %d\n",j,maf,loc1,loc2); if(j != T) printf("\n"); ///最后一组数据不能输出/n } return 0; }
相关文章推荐
- zookeeper 入门指南
- 【MySQL】源码编译安装和配置MySql 5.5.32(单实例)
- 专业之外——活动负责人的体验
- linq实现随机查询
- Container With Most Water
- 微信开发如何做本地调试?
- scala学习之:scala中的单例
- confession
- 微信开发如何做本地调试?
- 微信开发如何做本地调试?
- 微信开发如何做本地调试?
- hdu1269 强连通
- javascript生成器函数function generator初探和理解
- MSCcomm控件出现ASSERT错误
- 处事变通,转动大脑
- Android程序中实现按钮按下后的界面跳转
- 3_寻找假币问题(分治法)
- Caffe之mnist demo的配置和运行
- Android 中涉及的设计模式
- java数组