Max Sum hd 1003
来源:互联网 发布:h5直播源码 编辑:程序博客网 时间:2024/05/11 04:23
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:
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
题意:给出一个数列,求出连续数列和的最大值,并且输出连续数列的起始点和终点
思路: 这道题也是很简单的,用now和before记录现在和之前的和,然后依次替换。。最后求出最大值
#include<cstdio>#include<algorithm>using namespace std;int main(){int t=1;int m,n,now,before,max,i,e,s,x;scanf("%d",&n);while(n--){scanf("%d",&m);for(i=1;i<=m;i++){scanf("%d",&now);if(i==1){max=before=now;s=e=x=1;}else{if(now>before+now){before=now;x=i;}else before=before+now;}if(max<before){max=before;s=x;e=i;}}printf("Case %d:\n%d %d %d\n",t++,max,s,e);if(n)printf("\n");}return 0;}
0 0
- Max Sum hd 1003
- HD 1003 Max Sum(贪心)
- 1408041551-hd-Max Sum.cpp
- HD 1003 Max Sum (最大字段和问题)
- 1003 Max Sum
- HDOJ 1003 Max Sum
- HDU 1003 Max Sum
- HDOJ 1003 Max Sum
- 1003:max sum
- hdoj 1003 Max Sum
- hdu 1003 Max Sum
- hdu 1003 Max Sum
- HDOJ 1003 Max Sum
- HDU-1003 max sum
- hdoj 1003Max Sum
- 1003 Max Sum
- HDU 1003 - Max Sum
- HDU 1003 Max Sum
- 杭电-1231 最大连续子序列
- LeetCode | Binary Tree Maximun Path Sum
- DRR(Digitally Reconstructured Radiograph)在医学影像处理的应用
- freescale的yocto开发环境搭建
- [note] 深度学习 tensorflow 笔记(3) cnn 卷积神经网络
- Max Sum hd 1003
- Android安全开发之WebView中的地雷
- 版本控制和版本控制系统的区别
- 段式和页式存储管理试题及答案(整理)
- Delphi中数字输入限制方法
- 《UNIX环境高级编程》七进程环境读书笔记
- poj 2151 Check the difficulty of problems
- 浅谈Android版本控制—SVN
- C语言 strtok函数使用