HDU   1003(A - 最大子段和)

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/A

题目:

Max Sum

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
 
 
题意:
给出一个序列,求此序列的最大子段和 及开始和结束的位置。
 
分析:
动态规划。求最大子段和。b[i]表示最大子段和。c[i]表示子段和开始的位置。b[i]=(b[i-1]+a[i])>a[i]?b[i-1]+a[i]:a[i]
 
代码:
  1. #include<cstdio>
  2. #include<iostream>
  3. using namespace std;
  4. const int maxn=;
  5.  
  6. int a[maxn],b[maxn],c[maxn];//b表示最大子段和,c表示开始的位置
  7.  
  8. int main()
  9. {
  10. int t,m=;
  11. scanf("%d",&t);
  12. while(t--)
  13. {
  14. int n;
  15. scanf("%d",&n);
  16. for(int i=;i<=n;i++)
  17. scanf("%d",&a[i]);
  18. b[]=a[];//记开始时的子段和为b[1]
  19. c[]=;//开始时的位置为1
  20. for(int i=;i<=n;i++)
  21. {
  22. if(b[i-]>=)
  23. {
  24. b[i]=b[i-]+a[i];//子段和
  25. c[i]=c[i-];
  26. }
  27. else
  28. {
  29. b[i]=a[i];
  30. c[i]=i;
  31. }
  32. }
  33. int max=b[];//令起始位置最大值为b[1]
  34. int end=;
  35. for(int i=;i<=n;i++)
  36. {
  37. if(b[i]>max)
  38. {
  39. max=b[i];
  40. end=i;//结束位置
  41. }
  42. }
  43. printf("Case %d:\n",m++);
  44. printf("%d %d %d\n",max,c[end],end);
  45. if(t)
  46. printf("\n");
  47. }
  48. return ;
  49. }

我先在杭电上做了几遍,提交一直都是WA,改了几次都是WA。第一次是因为我直接把开始位置写为1,根本没有计算c[i]。后来又出现了PE,因为我没有写if(t)。改了几次终于改成功了。

 
 

HDU 1003(A - 最大子段和)的更多相关文章

  1. HDU 1003 最大连续子段和

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

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

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

  3. dp 动态规划 hdu 1003 1087

    动态规划就是寻找最优解的过程 最重要的是找到关系式 hdu 1003 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意:求最大字序列和, ...

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

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

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

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

  6. 【ToReadList】六种姿势拿下连续子序列最大和问题,附伪代码(以HDU 1003 1231为例)(转载)

    问题描述:       连续子序列最大和,其实就是求一个序列中连续的子序列中元素和最大的那个. 比如例如给定序列: { -2, 11, -4, 13, -5, -2 } 其最大连续子序列为{ 11, ...

  7. hdu 1003 hdu 1231 最大连续子序列【dp】

    HDU1003 HDU1231 题意自明.可能是真的进步了点,记得刚开始研究这个问题时还想了好长时间,hdu 1231还手推了很长时间,今天重新写干净利落就AC了. #include<iostr ...

  8. [ACM] 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) Total Su ...

随机推荐

  1. idea修改文件名后出现main method should be static错误

    1.确保你有main方法 2.别忘了main方法里还有参数: String[] args

  2. 几本不错的开源书(to be continued)

    Linux 1.working-on-gnu-linux GNU/Linux 至今已經相當成熟並足以應付日常生活之使用,凍仁也於 2009 年開始使用它來工作至今,將藉由此書 1 來撰寫較有系統的文章 ...

  3. 【原创】JPEG图像密写研究(二) 哈夫曼树的建立

    [原创]记录自己研究的过程,仅供参考,欢迎讨论... 在根据JPEG图像文件结构读取完文件后,提取出其中DHT段,利用其中内容建立哈夫曼树,便于之后译码工作.这里需要注意的是文件中的哈夫曼表数量不固定 ...

  4. asp.net 多站点共享StateServer Session

    <sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" ...

  5. Apache OFbiz entity engine源代码解读

    简单介绍 近期一直在看Apache OFbiz entity engine的源代码.为了能够更透彻得理解,也由于之前没有看人别人写过分析它的文章,所以决定自己来写一篇. 首先,我提出一个问题,假设你有 ...

  6. [置顶] 【cocos2d-x入门实战】微信飞机大战之四:飞机登场咯

    转载请表明地址:http://blog.csdn.net/jackystudio/article/details/11757175 昨天收到了电子工业出版社寄过来的<cocos2d-x游戏开发之 ...

  7. SQL SERVER 2008 R2 自动备份并删除过期备份数据

        我们的系统维护的过程中肯定需要对数据库进行定期的备份,但是如果定时手工备份的话,不但浪费时间,也不能保证每次都可以按时备份,所以自动备份成为了我们的不二选择,但是定时备份需要定期清理备份文件, ...

  8. C#多线程及GDI(Day 23)

       又来到了总结知识的时间了,今天又学了一些新的知识,是多线程和GDI的一些运用. 理论: 在学习多线程之前,首先要了解一下什么是进程? 进程:(关键字Process)进程是一个具有一定独立功能的程 ...

  9. monkeyrunner学习--手机按键

    按下HOME键 device.press('KEYCODE_HOME','DOWN_AND_UP') 按下BACK键 device.press('KEYCODE_BACK','DOWN_AND_UP' ...

  10. ASP.NET MVC5 学习笔记-3 Model

    1. Model 1.1 添加一个模型 注意,添加属性时可以输入"prop",会自动输入代码段. public class CheckoutAccount { public int ...