HDU 1003.Max Sum【最大连续子序列和】【8月14】
来源:互联网 发布:iap 元数据丢失 编辑:程序博客网 时间:2024/06/05 11:42
Max Sum
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
思路:跟上面改变的不多,前n项和为负数的时候,就从第n+1项继续寻找最大值。代码如下:
#include<cstdio>int main(){ int T,kase=1,n,x; scanf("%d",&T); while(T--){ int sum=0,maxsum=-9999,start=0,end=0,t=1;//start记录起点,end记录终点,t记录模拟起点 scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&x); sum+=x; if(sum>maxsum){ maxsum=sum; start=t; end=i; } if(sum<0){//前i项为负数,从第i+1项重新做 sum=0; t=i+1; } } if(kase>1) printf("\n"); printf("Case %d:\n%d %d %d\n",kase++,maxsum,start,end); } return 0;}
0 0
- HDU 1003.Max Sum【最大连续子序列和】【8月14】
- 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
- 【HDU】 1003 Max Sum(最大连续子序列和)
- Max Sum 最大连续子序列和
- Max Sum hdu+最大子序列和
- hdu 1003 Max Sum (最大连续子序列)
- hdu 1003 Max Sum (最大连续子序列)
- hdu 1003 Max Sum(分而治之||最大连续子序列 dp)
- HDU 1003 Max Sum【最大连续子序列】
- hdu 1231 最大连续子序列 ,1003 Max Sum;
- Max Sum(HDU1003)最大连续子序列和
- Android关于ListView中item与控件抢夺焦点的那些事
- Ios8 Xcode6 设置Launch Image 启动图片
- curl基本知识
- keepalived+redis HA方案
- [android]onTouch事件分发
- HDU 1003.Max Sum【最大连续子序列和】【8月14】
- Maven基础(介绍及环境搭建)
- dwz在线api
- linux文件流与文件描述符之间的转换
- Using Genymotion with Charles Proxy
- Caffe源码(四):base_conv_layer 分析
- 黑马程序员 多态,抽象类与接口
- [.Net码农]C# 工程中引用出现感叹号
- Unity学习