博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu1003——Max Sum(在线算法+求起点终点)
阅读量:2343 次
发布时间:2019-05-10

本文共 1500 字,大约阅读时间需要 5 分钟。

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

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

对在线算法有了新的认识,两个if的顺序居然都不能错。。。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 110000#define Mod 10001using namespace std;int main(){ int t,cnt=1; scanf("%d",&t); while(t--) { int n,m,k=1; int ans=-INF,tmp=0,ansi,ansj; scanf("%d",&n); for(int i=1; i<=n; ++i) { scanf("%d",&m); tmp+=m; if(tmp>ans) { ansi=k; ansj=i; ans=tmp; } if(tmp<0) { tmp=0; k=i+1; } } if(cnt!=1) printf("\n"); printf("Case %d:\n",cnt++); printf("%d %d %d\n",ans,ansi,ansj); } return 0;}

转载地址:http://btcvb.baihongyu.com/

你可能感兴趣的文章
Selenium core & IDE extension functions
查看>>
MVC学习二:MVC Action Result 返回类型扩展
查看>>
MVC学习三:MVC Action Result 返回类型实例
查看>>
MVC学习四:通过FileResult向浏览器发送文件
查看>>
MVC学习五:MVC 3.0 的新特性
查看>>
MVC学习六:MVC的概念及MVC 3.0开发环境
查看>>
MVC学习七:初识MVC的Url映射潜规则Routing
查看>>
MVC学习八:MVC3的远程验证
查看>>
MVC学习九:ASP.NET MVC 2:模型验证
查看>>
MVC学习十:MVC2中自定义校验
查看>>
MVC学习十一:浅谈在ASP.NET MVC3中使用IClientValidatable接口实现客户端和服务器端同时验证
查看>>
MVC学习十二:MVC高级编程(自定义验证)
查看>>
MVC学习十三:MVC3客户端自定义验证 -Jquery: addMinMax
查看>>
MVC学习十四:ASP.NET MVC如何实现自定义验证 AgeRangeAttribute
查看>>
我要学ASP.NET MVC 3.0(四): 我要MVC潜规则之配置Routing
查看>>
MVC数据从Controller传递到View之ViewData
查看>>
MVC数据从Controller传递到View之ViewModel
查看>>
MVC数据从Controller传递到View之ViewData & ViewModel
查看>>
Rollback or Undo a Changeset in TFS 2010 Version Control
查看>>
session和cookie的最深刻理解
查看>>