Max Sum 最大连续子序列和
来源:互联网 发布:cmd删除windows.old 编辑:程序博客网 时间:2024/05/16 10:45
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
题意很清楚,就是最大子序列和,不过需要输出最大和区间的开始和结束,start为当前起始点
#include <iostream>#include <cstdio>using namespace std;const int N = 1e5 + 5;int a[N];int main(){ int t,n; cin >> t; for(int k = 1; k <= t; k++) { cin >> n; for(int i = 1; i <= n; i++) scanf("%d",&a[i]); int maxn = -1e8,sum = 0; //maxn为当前最大值 int p,q,start = 1; //起始点一开始为1,p为起始,q为结束 for(int i = 1; i <= n; i++) { sum += a[i]; if(sum > maxn) { maxn = sum; p = start; q = i; } if(sum < 0) { sum = 0; start = i+1; //更改起始点 } } printf("Case %d:\n",k); printf("%d %d %d\n",maxn,p,q); if(k < t) printf("\n"); } return 0;}
阅读全文
0 0
- 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(最大连续子序列和)
- HDU 1003 Max Sum 最大连续子序列和
- 【最大连续子序列和dp】hdu 1003 Max Sum
- Max Sum(HDU1003)最大连续子序列和
- Max Sum 最大连续和的子序列
- 【HDU】 1003 Max Sum(最大连续子序列和)
- Max Sum(最大连续子序列)
- hdu1003 Max Sum 最大连续子序列
- Max Sum(最大连续子序列)
- Max Sum + 最大连续子序列
- Max Sum hdu+最大子序列和
- Max sum 最大子序列和
- MySQL中视图的定义、原理和如何使用、创建
- 关于推荐系统中的特征工程
- 计算机领域的所有SCI最顶级期刊
- textarea的placeholder无效问题解决
- ViewPager+布局xml(View)
- Max Sum 最大连续子序列和
- HDU 2036
- POJ 2098 Ellipse 笔记
- servlet&mvc&反射&jsp&jstl
- [主席树 Hash] Codechef JUNE17 #CLONEME Cloning
- CPictureEx和CPictureExWnd类,将GIF动画添加到MFC和ATL项目中
- QtIFW:制作程序安装包
- c++ builder 中的 XMLDocument 类详解(2)
- 【面试题】剑指offer07--两个栈实现一个队列