Max Sum

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

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

 
Author
Ignatius.L
 
Recommend
We have carefully selected several similar problems for
you:  1176 1087 1069 1058 1159 
题意:求连续子序列和的最大值。
题解: 注意这道题要求是输出第一个符合条件的序列,而且存在负数,样例之间要用空行分隔。
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
int a[maxn],n;
int main()
{
int t,cas=;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int sum=,ans=-;
int s=,e=,k=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
if(sum>ans)
{
s=k;
e=i;
ans=sum;
}
if(sum<) //0的意义就是这段数做的是负功
{
sum=;
k=i+;
}
}
printf("Case %d:\n",cas++);
printf("%d %d %d\n",ans,s,e);
if(t>) puts("");
}
return ;
}
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+;
int a[N],n;
int main()
{
int t,cas=;
cin>>t;
while(t--)
{
cin>>n;
int sum=,mx=-,s=,e=,ts=;
for(int i=;i<=n;i++)
{
cin>>a[i];
sum+=a[i];
if(sum>mx)
{
mx=sum;
s=ts;
e=i;
}
if(sum<)
{
sum=;
ts=i+;
}
}
printf("Case %d:\n",cas++);
printf("%d %d %d\n",mx,s,e);
if(t) printf("\n");
}
return ;
}
/*
100
2 1 2
1 1
3 -1 1 2
2 -7 3
*/

牢记顺序是 加 大于 小于!!

HDU1003MAX SUM (动态规划求最大子序列的和)的更多相关文章

  1. HDU 1003 Max Sum【动态规划求最大子序列和详解 】

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  2. HDU 1003 Max Sum * 最长递增子序列(求序列累加最大值)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  3. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  4. HDU1003MAX SUM

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  5. ACM学习历程—HDU1003 Max Sum(dp && 最大子序列和)

    Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...

  6. 动态规划:HDU1003-Max Sum(最大子序列和)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  7. HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)

    Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...

  8. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  9. 动态规划求一个序列的最长回文子序列(Longest Palindromic Substring )

    1.问题描述 给定一个字符串(序列),求该序列的最长的回文子序列. 2.分析 需要理解的几个概念: ---回文 ---子序列 ---子串 http://www.cnblogs.com/LCCRNblo ...

随机推荐

  1. Smart Framework:轻量级 Java Web 框架

    Smart Framework:轻量级 Java Web 框架 收藏 黄勇   工作闲暇之余,我开发了一款轻量级 Java Web 框架 —— Smart Framework. 开发该框架是为了: 加 ...

  2. JavaScript RegExp 身份证、账号密码、email正则

    什么是正则表达式 正则表达式是构成搜索模式. 在文本中搜索数据时,可以使用此搜索模式来描述正在搜索的内容. 正则表达式可以是单个字符,也可以是更复杂的模式. 正则表达式可用于执行所有类型的文本搜索和文 ...

  3. #Spring实战第二章学习笔记————装配Bean

    Spring实战第二章学习笔记----装配Bean 创建应用对象之间协作关系的行为通常称为装配(wiring).这也是依赖注入(DI)的本质. Spring配置的可选方案 当描述bean如何被装配时, ...

  4. libvirt保持虚拟机运行情况下修改名称

    通过virsh list命令能看到虚拟机的列表: [root@compute2 ~]# virsh list Id 名称 状态 ------------------------------------ ...

  5. 0.爬虫 urlib库讲解 urlopen()与Request()

    # 注意一下 是import urllib.request 还是 form urllib import request 0. urlopen() 语法:urllib.request.urlopen(u ...

  6. 今日头条 2018 AI Camp 视频面试

    1. 本次面试是在牛客网平台进行的,没有涉及到技术细节,面试官也说仅仅是聊天.但是,不知道是网络卡顿还是平台缘故,连接非常不稳定,经常听不到声音,对方那边噪音也特别大,面试体验不是很好. 2. 面试时 ...

  7. 深夜浅谈我理解的DIV对SEO的影响

    又到了夜深人静的时候,对于以前的我来说每天的这个时候都是在敲一下代码啊或者看一会书,但是今夜突然间又一次心血来潮,想写一篇博文来记录一下这一段时间做SEO优化所遇到的问题. 其实对于我来说SEO并不是 ...

  8. akka与slf4j导致jvm直接crash的诡异

    流程很简单,创建actorSystem,通过actorSystem获取AkkaQueryServiceRetriever,进而通过传递path获得的Gateway进行通信. 之前在主项目里跑的很稳定, ...

  9. chrome & dark theme

    chrome & dark theme Dark Reader Extensions https://darkreader.org/help/en/ https://chrome.google ...

  10. Struts1文件上传

    package org.zln.struts.domain; import org.apache.struts.upload.FormFile; /** * Created by sherry on ...