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 
题意:求连续子序列和的最大值。
题解: 注意这道题要求是输出第一个符合条件的序列,而且存在负数,样例之间要用空行分隔。
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn=1e5+;
  4. int a[maxn],n;
  5. int main()
  6. {
  7. int t,cas=;
  8. scanf("%d",&t);
  9. while(t--)
  10. {
  11. scanf("%d",&n);
  12. int sum=,ans=-;
  13. int s=,e=,k=;
  14. for(int i=;i<=n;i++)
  15. {
  16. scanf("%d",&a[i]);
  17. sum+=a[i];
  18. if(sum>ans)
  19. {
  20. s=k;
  21. e=i;
  22. ans=sum;
  23. }
  24. if(sum<) //0的意义就是这段数做的是负功
  25. {
  26. sum=;
  27. k=i+;
  28. }
  29. }
  30. printf("Case %d:\n",cas++);
  31. printf("%d %d %d\n",ans,s,e);
  32. if(t>) puts("");
  33. }
  34. return ;
  35. }
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N=1e5+;
  4. int a[N],n;
  5. int main()
  6. {
  7. int t,cas=;
  8. cin>>t;
  9. while(t--)
  10. {
  11. cin>>n;
  12. int sum=,mx=-,s=,e=,ts=;
  13. for(int i=;i<=n;i++)
  14. {
  15. cin>>a[i];
  16. sum+=a[i];
  17. if(sum>mx)
  18. {
  19. mx=sum;
  20. s=ts;
  21. e=i;
  22. }
  23. if(sum<)
  24. {
  25. sum=;
  26. ts=i+;
  27. }
  28. }
  29. printf("Case %d:\n",cas++);
  30. printf("%d %d %d\n",mx,s,e);
  31. if(t) printf("\n");
  32. }
  33. return ;
  34. }
  35. /*
  36. 100
  37. 2 1 2
  38. 1 1
  39. 3 -1 1 2
  40. 2 -7 3
  41. */

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

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. Prolog奇怪奇妙的思考方式

    今天在<七周七语言>中接触到了prolog,发现它的编程模式和思考方式的确比较奇怪,但同时也非常奇妙,值得学习一下. 1. prolog语言介绍     和SQL一样,Prolog基于数据 ...

  2. 一丶人生苦短,我用python【第一篇】

    1 解释器 解释器(英语:Interpreter),又译为直译器,是一种电脑程序,能够把高级编程语言一行一行直接转译运行.解释器不会一次把整个程序转译出来,只像一位"中间人",每次 ...

  3. 第十五篇 Python之文件处理

    一 文件操作  介绍 计算机系统分为:计算机硬件,操作系统,应用程序三部分. 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众所 ...

  4. ADB常用指令

    adb 命令是adb程序自带的一些命令:adb shell则是调用Android系统的命令,Android系统特有的命令都放在Android设备的/system/bin目录中 MonkeyRunner ...

  5. 10-Mysql数据库----数据的增删改

    本节重点: 插入数据 INSERT 更新数据 UPDATE 删除数据 DELETE 再来回顾一下之前我们练过的一些操作,相信大家都对插入数据.更新数据.删除数据有了全面的认识.那么在mysql中其实最 ...

  6. Android Spiner实现Key-Value

    原网址:http://www.eoeandroid.com/thread-29687-1-1.html?_dsign=02d5cd6a 学习到的方法,直接上代码了: 1.定义一个class publi ...

  7. Visual Studio 2013安装包

    点击下载

  8. 了解游戏编程与 AI

    噫语系列... 闲话 最近在重写我的一个 QQ 群机器人项目,并尝试将它改成更通用的结构,以方便在未来加入对 Wechat 和 Telegram 的支持. 在查资料的过程中,发现很多人认为一个群内多人 ...

  9. 数论初步——Eratosthenes筛法

    具体内容见紫书p312-p313 一.用Eratosthenes筛法构造1~n的素数表 思想:对于不超过n的每个非负整数p,删除2p,3p,4p…,当处理完所有的数后,还没有被删除的就是素数. 代码: ...

  10. LTE QOS

    http://wenku.baidu.com/link?url=ziFIkdKaC7MU2RY-bTOp2bt87WFPw5_02bqmYs5W6w4ktOfPHEcWesK1U2T7YiyXjVSM ...