注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Minary_Acdream

http://f10.moe/

 
 
 

日志

 
 

[hdu]1003 Max Sum  

2012-07-29 16:21:39|  分类: HDU |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
感觉是来水博客的...

Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 82404    Accepted Submission(s): 18977


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
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
 

Author
Ignatius.L


就是求子序列的最大和,开始和结束值。
从第一个开始若sum小于0了,那么就记当前位置的后一位为开始,继续向下。
下面是代码:


#include<iostream>
using namespace std;

int main()
{
int T;
cin>>T;
for(int i = 1;i <= T;i ++)
{
if(i != 1)cout<<endl;
cout<<"Case "<<i<<":"<<endl;
int N,inter;
int max = -1001,sum = 0,start = 0,end = 0,temp = 1;
cin>>N;
for(int j = 1;j <= N;j ++)
{
scanf("%d",&inter);
sum += inter;
if(sum > max)
{
max = sum;
end = j;
start = temp;
}
if(sum < 0)
{
sum = 0;
temp = j + 1;
}

}
cout<<max<<" "<<start<<" "<<end<<endl;
}
}



  评论这张
 
阅读(88)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018