HDU——1003Max Sum(子序列最大和)
来源:互联网 发布:淘宝的优化建议 编辑:程序博客网 时间:2024/06/06 14:15
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 197869 Accepted Submission(s): 46229
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
看了别人的才写的代码,但是后半部分对于nowsum+t>=t时仍旧不是很理解。以后学了数据结构再来看看吧
#include<iostream>#include<cstdio>using namespace std;int main(void){int T,q;cin>>T;for (q=1; q<=T; q++){int n;int temp_sum,maxsum,temp_begin,end,temp_value,i,begin;cin>>n>>temp_value;temp_sum = maxsum = temp_value;temp_begin = end = begin = 1;for (i=2; i<=n; i++){cin>>temp_value;if(temp_sum + temp_value < temp_value)//若i-1个sum值加上第i个数还没第i个数大 {temp_begin=i;//显然临时起点要改成i temp_sum=temp_value;//临时sum也要用当前数开始 }else //反之 {temp_sum += temp_value;//显然要加上这个数,肯定是递增的 }if(temp_sum>maxsum)//临时起点能否成为答案的start就看临时和是否比max大 {maxsum=temp_sum;end=i;begin=temp_begin;}}printf("Case %d:\n%d %d %d\n",q,maxsum,begin,end);if(q!=T)printf("\n");}return 0;}
0 0
- HDU 1003 Max Sum(最大连续子序列和)
- hdu 1003 Max Sum (求最大子序列和)
- HDU 1003----Max Sum(最大连续子序列和)
- 【HDU】 1003 Max Sum(最大连续子序列和)
- HDU——1003Max Sum(子序列最大和)
- Max Sum hdu+最大子序列和
- 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
- HDU 1003 Max Sum (最大子序列的和)
- hdu 1003 Max Sum (最大连续子序列)
- hdu 1003 Max Sum (最大连续子序列)
- HDU 1003 Max Sum(求序列最大子序列和)
- 杭电1003——Max Sum(最大连续子序列和、递归分治)
- 视差特效
- idea gradle开发java工程
- unityTaidou(三)unity模型材质比较暗的解决方法
- 11573 - Ocean Currents
- bzoj2186: [Sdoi2008]沙拉公主的困惑
- HDU——1003Max Sum(子序列最大和)
- Codeforces Round #342 (Div. 2)总结
- 程序员进阶书籍介绍推荐
- HDU 3555 Bomb(数位DP)
- spring(AOP)Aop的概念
- 分隔符匹配
- 欢迎使用CSDN-markdown编辑器
- 简单聊天室的代码
- 算法练习 动态规划 硬币找零