Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 135262    Accepted Submission(s): 31311

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
  1. 2
  2. 5 6 -1 5 4 -7
  3. 7 0 6 -1 1 -6 7 -5
 
Sample Output
  1. Case 1:
  2. 14 1 4
  3. Case 2:
  4. 7 1 6
 
Author
Ignatius.L

解题思路:

当全部数都为负数时,最大子段和为0.

  1. int MaxSum(int num[],int n)
  2. {
  3. int sum=0,b=0;
  4. int i;
  5. for (i=1;i<=n;i++)
  6. {
  7. if(b>0)
  8. b+=num[i];
  9. else
  10. b=num[i];
  11. if(b>sum)
  12. sum=b;
  13. }
  14. return sum;
  15. }

仅仅求最大和,没有保存位置。

保存位置:start为起 end为末

  1. int start=1,end=1,s=1,e=1;
  2. int sum=0,max=num[1];//不能让max=0
  3. for(int i=1;i<=n;i++)
  4. {
  5. e=i;
  6. sum=sum+num[i];
  7. if(max<sum)
  8. {
  9. max=sum;
  10. start=s;
  11. end=e;
  12. }
  13. if(sum<0)
  14. {
  15. s=i+1;
  16. sum=0;
  17. }
  18. }

本题须要保存起始位置。以下代码假设全是负数,输出最小的那个位置

代码:

  1. #include <iostream>
  2. using namespace std;
  3. const int maxn=100002;
  4. int num[maxn];
  5. int n;
  6. int main()
  7. {
  8. int t;cin>>t;int c=1;
  9. while(t--)
  10. {
  11. cin>>n;
  12. for(int i=1;i<=n;i++)
  13. cin>>num[i];
  14. int start=1,end=1,s=1,e=1;//这里start end一定要赋值为1
  15. int sum=0,max=num[1];//不能让max=0
  16. for(int i=1;i<=n;i++)
  17. {
  18. e=i;
  19. sum=sum+num[i];
  20. if(max<sum)
  21. {
  22. max=sum;
  23. start=s;
  24. end=e;
  25. }
  26. if(sum<0)
  27. {
  28. s=i+1;
  29. sum=0;
  30. }
  31. }
  32. cout<<"Case "<<c++<<":"<<endl;
  33. cout<<max<<" "<<start<<" "<<end<<endl;
  34. if(t)
  35. cout<<endl;
  36. }
  37. return 0;
  38. }

[ACM] hdu 1003 Max Sum(最大子段和模型)的更多相关文章

  1. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  2. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  3. hdu 1003 Max Sum (DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)   ...

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

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

  5. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

  6. hdu 1003 Max Sum (动态规划)

    转载于acm之家http://www.acmerblog.com/hdu-1003-Max-Sum-1258.html Max Sum Time Limit: 2000/1000 MS (Java/O ...

  7. HDU - 1003 Max Sum 【DP】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1003 题意 给出一个序列 要求找出一个和最大的子序列 思路 O(N)的做法 但是要标记 子序列的头部位 ...

  8. HDU 1003 Max Sum (动规)

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

  9. hdu 1003 Max sum(简单DP)

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

随机推荐

  1. 非自定义和自定义Dialog的介绍!!!

    一.非自定义Dialog的几种形式介绍 转自:http://www.kwstu.com/ArticleView/kwstu_20139682354515 前言 对话框对于应用也是必不可少的一个组件,在 ...

  2. 不是技术牛人,如何拿到国内IT巨头的Offer

    原地址:http://blog.csdn.net/lsldd/article/details/13506263 不久前,byvoid面阿里星计划的面试结果截图泄漏,引起无数IT屌丝的羡慕敬仰.看看这些 ...

  3. 【LeetCode练习题】Copy List with Random Pointer

    Copy List with Random Pointer A linked list is given such that each node contains an additional rand ...

  4. OAuth2.0开发指南

    OAuth2.0开发指南 1.认证与登录 来往开放平台支持3种不同的OAuth 2.0验证与授权流程: 服务端流程(协议中Authorization Code Flow): 此流程适用于在Web服务端 ...

  5. HTML5开发 BUG解决

    1.点透Q:元素A上定位另外一个元素B,点击元素B,如果元素A有事件或链接,会触发元素A上的事件或链接,即点透A:在元素B的touchend中增加ev.preventDefault();阻止默认事件即 ...

  6. Activity(二)

    多个Activity之间的调用 建立一个Activity 配置layout文件夹下fragment_main.xml文件 在layout下新建other.xml文件 xml文件创建的id需要编译才能生 ...

  7. C#软件winform程序安装包制作及卸载程序制作

    使用vs2010 winform程序开发的软件的人比较多,程序的开发是为了在不同的人不同的机器使用,为了使不同的机器能使用该软件就需要在制作程序安装包,安装包里必须包含该软件运行所选的所有环境,下面就 ...

  8. css3文本效果

    CSS3 包含多个新的文本特性. 在本章中,您将学到如下文本属性: 1. text-shadow 2. word-wrap 浏览器支持 Internet Explorer 10.Firefox.Chr ...

  9. android系统将普通应用升级为系统应用

    作为一名程序员,有的时候并不是使用软件,而是去改造软件,不仅仅只是会编程而已,还要满足客户的需求.这样,才能开发出符合客户需求的应用,在关于到涉及到android底层的应用的时候,手机就需要root了 ...

  10. C#6 冒泡 折半查找 二维数组

    人类思维--计算机逻辑思维 逻辑思维--代码实现 写书法: 描红--临摹--碑贴--自成一体--草 章节复习: 数组:一维,二维,多维 一维:豆角.连续,同一类型. 定义:数据类型[] 数组名=new ...