HDU1003MAX SUM (动态规划求最大子序列的和)
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 206582 Accepted Submission(s):
48294
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.
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).
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.
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
14 1 4
Case 2:
7 1 6
- #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 (动态规划求最大子序列的和)的更多相关文章
- HDU 1003 Max Sum【动态规划求最大子序列和详解 】
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- HDU 1003 Max Sum * 最长递增子序列(求序列累加最大值)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- HDU1003MAX SUM
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 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 ...
- 动态规划:HDU1003-Max Sum(最大子序列和)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- HDU 1081 To the Max 最大子矩阵(动态规划求最大连续子序列和)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 动态规划求一个序列的最长回文子序列(Longest Palindromic Substring )
1.问题描述 给定一个字符串(序列),求该序列的最长的回文子序列. 2.分析 需要理解的几个概念: ---回文 ---子序列 ---子串 http://www.cnblogs.com/LCCRNblo ...
随机推荐
- Prolog奇怪奇妙的思考方式
今天在<七周七语言>中接触到了prolog,发现它的编程模式和思考方式的确比较奇怪,但同时也非常奇妙,值得学习一下. 1. prolog语言介绍 和SQL一样,Prolog基于数据 ...
- 一丶人生苦短,我用python【第一篇】
1 解释器 解释器(英语:Interpreter),又译为直译器,是一种电脑程序,能够把高级编程语言一行一行直接转译运行.解释器不会一次把整个程序转译出来,只像一位"中间人",每次 ...
- 第十五篇 Python之文件处理
一 文件操作 介绍 计算机系统分为:计算机硬件,操作系统,应用程序三部分. 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众所 ...
- ADB常用指令
adb 命令是adb程序自带的一些命令:adb shell则是调用Android系统的命令,Android系统特有的命令都放在Android设备的/system/bin目录中 MonkeyRunner ...
- 10-Mysql数据库----数据的增删改
本节重点: 插入数据 INSERT 更新数据 UPDATE 删除数据 DELETE 再来回顾一下之前我们练过的一些操作,相信大家都对插入数据.更新数据.删除数据有了全面的认识.那么在mysql中其实最 ...
- Android Spiner实现Key-Value
原网址:http://www.eoeandroid.com/thread-29687-1-1.html?_dsign=02d5cd6a 学习到的方法,直接上代码了: 1.定义一个class publi ...
- Visual Studio 2013安装包
点击下载
- 了解游戏编程与 AI
噫语系列... 闲话 最近在重写我的一个 QQ 群机器人项目,并尝试将它改成更通用的结构,以方便在未来加入对 Wechat 和 Telegram 的支持. 在查资料的过程中,发现很多人认为一个群内多人 ...
- 数论初步——Eratosthenes筛法
具体内容见紫书p312-p313 一.用Eratosthenes筛法构造1~n的素数表 思想:对于不超过n的每个非负整数p,删除2p,3p,4p…,当处理完所有的数后,还没有被删除的就是素数. 代码: ...
- LTE QOS
http://wenku.baidu.com/link?url=ziFIkdKaC7MU2RY-bTOp2bt87WFPw5_02bqmYs5W6w4ktOfPHEcWesK1U2T7YiyXjVSM ...