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,通过枚举发现的规律。
 
最后是代码:
#include <iostream>
#include <cstdio> using namespace std; int main()
{
int t;
int n;
int a[];//存储序列
int sum[];//存储以每个数为结尾的子序列和
int s[];//存储开始位置
int ans;//结束位置
scanf("%d",&t);
for(int i=;i<=t;i++){
scanf("%d",&n);
for(int i1=;i1<n;i1++){
scanf("%d",&a[i1]);
}
ans=;
sum[]=a[];
s[]=;
for(int j=;j<n;j++){
if(sum[j-]>=){
sum[j]=sum[j-]+a[j];
s[j]=s[j-];
}else{
sum[j]=a[j];
s[j]=j;
}
if(sum[ans]<sum[j])
ans=j;
}
if(i<t){
printf("Case %d:\n%d %d %d\n",i,sum[ans],s[ans]+,ans+);
printf("\n");
}else{
printf("Case %d:\n%d %d %d\n",i,sum[ans],s[ans]+,ans+);
}
}
return ;
}
  

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. BZOJ-1877 晨跑 最小费用最大流+拆点

    其实我是不想做这种水题的QWQ,没办法,剧情需要 1877: [SDOI2009]晨跑 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 1704 Solve ...

  2. jquery无限级下拉框

    非原创,已修改成通用版,效果如下: 下载:http://files.cnblogs.com/files/EasonJim/jquery.menu.rar 项目相关地址 源码:https://githu ...

  3. 从HashMap透析哈希表

    ##扯数据结构 先看一下哈希表的概念: 哈希表是一种数据结构,它可以提供快速的插入操作和查找操作.第一次接触哈希表,他会让人难以置信,因为它的插入和删除.查找都接近O(1)的时间级别.用哈希表,很多操 ...

  4. nginx try_files命令

    location / { index index.html index.htm index.php l.php; autoindex on; try_files $uri $uri/ /index.p ...

  5. UML用例图总结

    用例图主要用来描述“用户.需求.系统功能单元”之间的关系.它展示了一个外部用户能够观察到的系统功能模型图. [用途]:帮助开发团队以一种可视化的方式理解系统的功能需求. 用例图所包含的元素如下: 1. ...

  6. Component creation must be done on Event Dispatch Thread错误解决方法

    在用java swing 做例子,给页面设置皮肤样式的时候出现了这个错误: org.jvnet.substance.api.UiThreadingViolationException: Compone ...

  7. fp = fopen(s, "at") 中at 是啥意思,a 是append 追加的意思

    打开一个s的stream, a表示append,就是说写入处理的时候是接着原来文件已有内容写入,不是从头写入覆盖掉, t表示打开文件的类型是文本文件, "+号表示对文件既可以读也可以写.&q ...

  8. sublime Text 3实用功能和常用快捷键收集

    下面是我通过网上视频教程或文本资料学习sublime Text3时收集的一些实用功能和常用快捷键,现在分享出来,如果还有其它的好用的功能可以在下面留言,以便互相学习. PS:ST3在Mac OX与Wi ...

  9. Eclipse启动Tomcat,45S超时问题解决

    在Eclipse中启动Tomcat服务器时,经常由于系统初始化项目多,导致出现45秒超时的Tomcat服务器启动错误.     以前我一般通过找到XML配置文件,将对应Timeout为45的值,修改为 ...

  10. Antenna Placement(匈牙利算法 ,最少路径覆盖)

    Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6991   Accepted: 3466 ...