Max Sum

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

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
这题是一个经典dp问题,dp思想在于把问题分解成若干个子问题,在子问题最优的情况下得出最终最优结果,所以我们每一步都是建立在前一步是最优的基础之上的。所以解决dp问题,最基本的要在某种情景下,想到当前状态的最优情况是什么样的,最优后,接下来怎么办,不是最优的该怎么变成最优的。这就是我们的状态转移方程。
对于本问题,首先明确,连续的子段! 和最大 ,一个数组给我们,第一个数肯定当前最大,毋庸置疑,那么遇到下一个数,怎么判断和是当前最大呢?我们遇到正数,那肯定直接加,因为加完肯定比不加大,如果是负数呢?加上去,原来的和肯定变小,但不加怎么办呢?
基于以上问题 可以得出状态方程 dp[i]=d[i-1]+a[i]>a[i]?dp[i-1]+a[i]:a[i]  (dp[i]表示当前i下最大的子段和,a[i]是需要处理的数字)什么意思呢,当前数字加到之前的和上面后,如果大于当前数字,那么就执行加的操作,如果小于当前数字,就把当前和最大值dp[i]设置为a[i],可能有人问为什么要设置为a[i]。。记住,如果a[i]你不加上去,意味着你需要从新开始累加和了,我们要求是连续的子段和!!!
利用dp数组的代码:
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. //#define LOCAL
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. #ifdef LOCAL
  10. freopen("d:datain.txt","r",stdin);
  11. freopen("d:dataout.txt","w",stdout);
  12. #endif
  13. int n;
  14. while(scanf("%d",&n)!=EOF)
  15. {
  16. int i,m;
  17. for(i = ; i< n;i++)
  18. {
  19. scanf("%d",&m);
  20. int dp[],a[];
  21. scanf("%d",&a[]);
  22. dp[] = a[]; //当前最大
  23. for(int j = ; j<m;j++) //生成了dp状态数组了
  24. {
  25. scanf("%d",&a[j]);
  26. if(dp[j-]+a[j]<a[j]) //状态转移方程
  27. dp[j]=a[j];
  28. else
  29. dp[j]=dp[j-]+a[j];
  30. }
  31. int Max,End;
  32. Max = dp[];
  33. End = ;
  34. for(int j = ;j<m;j++) //寻找区间
  35. if(Max<dp[j])
  36. {
  37. End = j;
  38. Max = dp[j];
  39. }
  40. int Begin = End;
  41. int temp = ;
  42. for(int j = End;j>=;j--)
  43. {
  44. temp +=a[j];
  45. if(temp==dp[End])
  46. Begin = j;
  47. }
  48. cout<<"Case "<<i+<<":"<<endl<<Max<<" "<<Begin+<<" "<<End+<<endl;
  49. if(i<n-)
  50. cout<<endl;
  51. }
  52. }
  53. return ;
  54. }

简化后不带dp数组的,因为这题在dp问题中是比较简单的。

  1. //hdu 1003
  2.  
  3. #include<stdio.h>
  4. int main()
  5. {
  6.  
  7. int n;
  8. while(scanf("%d",&n)!=EOF)
  9. {
  10. for(int i = ;i<n;i++)
  11. {
  12. int a;
  13. int Max = -;
  14. int sum = ,m;
  15. int Begin=,End=,flag=;
  16. scanf("%d",&m);
  17. scanf("%d",&a);
  18. Max = sum = a;
  19. for(int j = ;j<m ;j++)
  20. {
  21. scanf("%d",&a);
  22. if(sum<)
  23. {
  24. sum=a;
  25. flag=j;
  26. }
  27. else
  28. {
  29.  
  30. sum=sum+a;
  31. }
  32. if(Max<sum)
  33. {
  34. Max = sum ;
  35. Begin =flag;
  36. End = j;
  37. }
  38. }
  39. printf("Case %d:\n%d %d %d\n",i+,Max,Begin+,End+);
  40. if(i<n-)
  41. printf("\n");
  42. }
  43.  
  44. }
  45. return ;
  46. }

hdu1003的更多相关文章

  1. hdu1000,hdu1001,hdu1002,hdu1003

    hdu1000 仅仅是为了纪念 #include <cstdio> int main() { int a,b; while (scanf("%d%d",&a,& ...

  2. hdu1003 1024 Max Sum&Max Sum Plus Plus【基础dp】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4302208.html   ---by 墨染之樱花 dp是竞赛中常见的问题,也是我的弱项orz, ...

  3. hdu1003 Max Sum(最大子串)

    https://vjudge.net/problem/HDU-1003 注意考虑如果全为负的情况,特判. 还有输出格式,最后一个输出不用再空行. #include<iostream> #i ...

  4. hdu1003 Max Sum【最大连续子序列之和】

    题目链接:https://vjudge.net/problem/HDU-1003 题目大意:给出一段序列,求出最大连续子序列之和,以及给出这段子序列的起点和终点. 解题思路:最长连续子序列之和问题其实 ...

  5. 解题报告:hdu1003 Max Sum - 最大连续区间和 - 计算开头和结尾

    2017-09-06 21:32:22 writer:pprp 可以作为一个模板 /* @theme: hdu1003 Max Sum @writer:pprp @end:21:26 @declare ...

  6. HDU1003 简单DP

    Max Sum Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the ...

  7. HDu1003(maxn sum)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABBcAAAMDCAYAAAD5XP0yAAAgAElEQVR4nOy97a8c133n2X+H3xjIC4

  8. hdu1003 dp

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1003 #include<cstdio> #include<algorit ...

  9. hdu1003 dp(最大子段和)

    题意:给出一列数,求其中的最大子段和以及该子段的开头和结尾位置. 因为刚学过DP没几天,所以还会这题,我开了一个 dp[100002][2],其中 dp[i][0] 记录以 i 为结尾的最大子段的和, ...

  10. HDU1003前导和

    简单维护前导和 #include<stdio.h> int main() { ],cas,key=; scanf("%d",&cas); while(cas-- ...

随机推荐

  1. perl 爬取数据<1>

    use LWP::UserAgent; use POSIX; use DBI; $user="root"; $passwd="11111111"; $dbh=& ...

  2. 大量客户反映wordpress的网站打开巨慢,经分析发现,这些网站大都使用了google的字体服务,由于最近google的服务已经被大陆屏蔽,所以wordpress的网站打开时,会卡在字体加载上。

     一会你安装完wp,发现打开巨卡的话,看看这个帖子:http://bbs.myhostcn.com/thread-1026-1-1.html最近一段时间,大量客户反映wordpress的网站打开巨慢, ...

  3. ERROR: HHH000388: Unsuccessful: create table

    做SSH整合的时候,总是出现错误信息: 类似这样: : HHH000388: Unsuccessful: create table right (right_code varchar(255) not ...

  4. 格而知之3:Core Data的基本使用

    最近准备做一个随手笔记类的app给自己用,考虑到从未使用过Core Data,就决定用Core Data来做数据存储.在网上参考了一些Core Data的资料后,用一天的时间写了这个demo,主要测试 ...

  5. Can you find it?(二分 二分+STL set map)

    Can you find it? Time Limit : 10000/3000ms (Java/Other)   Memory Limit : 32768/10000K (Java/Other) T ...

  6. 一种基于Qt的可伸缩的全异步C/S架构server实现(二) 网络传输

    二.网络传输模块 模块相应代码命名空间    (namespace ZPNetwork) 模块相应代码存储目录    (\ZoomPipeline_FuncSvr\network) 2.1 模块结构 ...

  7. Oracle SQL函数之聚组函数

    AVG([distinct|all]x) [功能]统计数据表选中行x列的平均值. [参数]all表示对所有的值求平均值,distinct只对不同的值求平均值,默认为all 如果有参数distinct或 ...

  8. C#事件、委托简单示例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. Hibernate 查询:HQL查询(Hibernate Query Languge)

    HQL是一种面向对象的查询语言,其中没有表和字段的概念,只有类,对象和属性的概念. 使用HQL查询所有学生: public static void main(String[] args) { Sess ...

  10. stuts1:(Struts)Action类及其相关类

    org.apache.struts.action.Action类是Struts的心脏,也是客户请求和业务操作间的桥梁.每个Action类通常设计为代替客户完成某种操作.一旦正确的Action实例确定, ...