Max Sum

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

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
 
解题心得:
  这个题是一个简单地动态规划题,题意:输入n个数,求它的最大的子序列和。首先写出状态转移方程: sum[i]=max{sum[i-1]+a[i],a[i]}。
  s数组是记录开始位置,每当sum加上一个a[i],值小于0的时候,s取用新的值。ans是记录结束位置,每当sum加上一个a[i],值大于等于0的时候,更新一下。
    我又在格式问题上出错了,以后要更加注意格式问题,最后一行不需要换行!
  也可以参考这个人的思路,http://blog.csdn.net/code_pang/article/details/7772200,通过枚举发现的规律。
 
最后是代码:
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int t;
  9. int n;
  10. int a[];//存储序列
  11. int sum[];//存储以每个数为结尾的子序列和
  12. int s[];//存储开始位置
  13. int ans;//结束位置
  14. scanf("%d",&t);
  15. for(int i=;i<=t;i++){
  16. scanf("%d",&n);
  17. for(int i1=;i1<n;i1++){
  18. scanf("%d",&a[i1]);
  19. }
  20. ans=;
  21. sum[]=a[];
  22. s[]=;
  23. for(int j=;j<n;j++){
  24. if(sum[j-]>=){
  25. sum[j]=sum[j-]+a[j];
  26. s[j]=s[j-];
  27. }else{
  28. sum[j]=a[j];
  29. s[j]=j;
  30. }
  31. if(sum[ans]<sum[j])
  32. ans=j;
  33. }
  34. if(i<t){
  35. printf("Case %d:\n%d %d %d\n",i,sum[ans],s[ans]+,ans+);
  36. printf("\n");
  37. }else{
  38. printf("Case %d:\n%d %d %d\n",i,sum[ans],s[ans]+,ans+);
  39. }
  40. }
  41. return ;
  42. }
  

HDU 1003 Max Sum的更多相关文章

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

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

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

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

  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 简单的dp,测试样例之间输出空行

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

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

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

  6. HDU 1003 Max Sum (动规)

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

  7. hdu 1003 Max sum(简单DP)

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

  8. HDU 1003 Max Sum 解题报告

    题目大意:求一串数字中,几个连续数字加起来最大值,并确定起始和最末的位置. 思路:这是一题DP题,但是可以用尺取法来做.我一开始不会,也是看了某大神的代码,然后有人告诉我这是尺取法,现在会了. //尺 ...

  9. HDU 1003 Max Sum(AC代码)

    #include <stdio.h> int main(){ int i,t,j,n,x; int start,end,temp,max,sum; scanf("%d" ...

随机推荐

  1. 【ZOJ 3502】Contest

    题 题意 n个问题,解决的顺序影响正确的概率,无论之前解决的问题是否答对,当前问题 j 答对概率为max{a[i][j]} (i为解决过的问题).求答对题目的最大期望和对应的答题顺序.T组测试,T ( ...

  2. bzoj3037 创世纪

    两种解法: 一.树状DP /*by SilverN*/ #include<iostream> #include<algorithm> #include<cstring&g ...

  3. Model1模式的学生信息增删改查

    Student.java package entity; public class Student { private int stuid; private String stuname; priva ...

  4. php中静态变量和静态方法

    1,静态变量:所有对象共享的变量成为静态变量.静态变量类似于全局变量,不过全局变量破坏对象的封装性,因此其对应于面向过程:静态变量对应于面向对象. 2,全局变量,全局变量的使用实例如下,声明全局变量时 ...

  5. Color Space

    色域(Color Space),又被称为色彩空间,它代表了一个色彩影像所能表现的色彩具体情况.我们经常用到的色彩空间主要有RGB.CMYK.Lab等,而RGB色彩 空间又有AdobeRGB.Apple ...

  6. tcp 重发 应用层重传

    采用TCP时,应用层需要超时重传吗? 需要,原因如下: 1 tcp的超时控制不是你能设置的,所有的tcp超时都是用系统的时间设定,而且这个时间很长,超时的结果就是断开连接.和你应用要达到的目的显然差很 ...

  7. ModSecurity SQL注入攻击

    ModSecurity是 一个入侵探测与阻止的引擎,它主要是用于Web应用程序所以也可以叫做Web应用程序防火墙.它可以作为Apache Web服务器的一个模块或单独的应用程序来运行.ModSecur ...

  8. udf提权方法和出现问题汇总

    一.适用条件 1.目标系统是Windows(Win2000,XP,Win2003): 2.你已经拥有MYSQL的某个用户账号,此账号必须有对mysql的insert和delete权限以创建和抛弃函数( ...

  9. 安装TFS2008最终版(转载)

    一.安装操作系统:windows server 2003 + Sp2具体步骤: 1.安装windows server 2003时选用工作组(默认为workgroup).由于在工作组环境中部署,因此使用 ...

  10. javascript单例模式的理解

    javascript单例模式的理解 阅读目录 理解单例模式 使用代理实现单例模式 理解惰性单例 编写通用的惰性单例 单例模式使用场景 回到顶部 理解单例模式 单例模式的含义是: 保证一个类只有一个实例 ...