Max Sum

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

Total Submission(s): 81735    Accepted Submission(s): 18797

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




解题心得:
1、这个题要求的是一个在序列中的一串连续的子序列,要求子序列的和最大,并且要求记录子序列的起始位置和终止位置。
2、可以将前面的数加在一起看作一个数,当前面的数为负数的时候则舍去,将当前这个数作为新的起点。用一个变量Max记录一下最大的值,当最大值被替换的时候记录一下终点和起点。理解了还是很容易的。



#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+100;
int num[maxn],dp[maxn];
int Start,End,now_start,Max;
int main()
{
int t;
int T;
scanf("%d",&t);
T = t;
while(t--)
{
memset(dp,0,sizeof(dp));
num[0] = -1;
now_start = 1;
Max = -1000000;
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
for(int i=1;i<=n;i++)
{
if(num[i] + dp[i-1] >= num[i])//前面的数不是负数
dp[i] = num[i] + dp[i-1];
else
{
dp[i] = num[i];//前面一个数是负数,将现在这个数作为新的起点
now_start = i;//当前起点,当出现最大值的用得到
}
if(dp[i] >= Max)//出现最大值,记录最大值,终点和起点
{
Start = now_start;
End = i;
Max = dp[i];
}
}
printf("Case %d:\n",T-t);
if(t != 0)
printf("%d %d %d\n\n",Max,Start,End);
else
printf("%d %d %d\n",Max,Start,End);
}
return 0;
}


动态规划:HDU1003-Max Sum(最大子序列和)的更多相关文章

  1. [ACM_动态规划] hdu1003 Max Sum [最大连续子串和]

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

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

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

  3. HDU-1003 Max Sum(动态规划,最长字段和问题)

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

  4. ACM学习历程—HDU1003 Max Sum(dp && 最大子序列和)

    Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...

  5. 杭电60题--part 1 HDU1003 Max Sum(DP 动态规划)

    最近想学DP,锻炼思维,记录一下自己踩到的坑,来写一波详细的结题报告,持续更新. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Problem ...

  6. HDU--1003 Max Sum(最大连续子序列和)

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

  7. hdu1003 Max Sum(经典dp )

      A - 最大子段和 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descr ...

  8. HDU1003 Max Sum(求最大字段和)

    事实上这连续发表的三篇是一模一样的思路,我就厚颜无耻的再发一篇吧! 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 -------------- ...

  9. (动态规划)Max Sum Plus Plus--hdu--1024

    http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Othe ...

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

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

随机推荐

  1. 《web-Mail服务的搭建》

    首先是搭建后台服务: 下载下面2个软件包 extmail-1.2.tar.gz extman-1.1.tar.gz 创建一个extsuite目录,固定格式 mkdir /var/www/extsuit ...

  2. 初识quartz 并分析 项目中spring整合quartz的配置【原创+转载】

    初识quartz 并分析 项目中spring整合quartz的配置[原创+转载]2018年01月29日 12:08:07 守望dfdfdf 阅读数:114 标签: quartz 更多个人分类: 工具 ...

  3. 大数据kafka视频教程 学习记录【B站尚硅谷 】

    视频地址: https://www.bilibili.com/video/av35354301/?p=1           2019/03/06 21:59 消息队列的内部实现: Kafka基础: ...

  4. 常见SQL优化方法

    SQL优化的一些方法 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否 ...

  5. Eucalyptus-利用镜像启动一个Windows Server 2008r2实例

    1.前言 使用kvm制作Eucalyptus镜像(Windows Server 2008r2为例)——http://www.cnblogs.com/gis-luq/p/3990792.html 上一篇 ...

  6. 查看mysql历史命令

    默认情况下操作mysql会在家目录下创建一个隐藏的mysql历史命令文件.mysql_history 在管理授权mysql账户时也会记录这些明文密码到这个文件,非常的不安全 [root@localho ...

  7. Vultr VPS建站攻略 – 一键安装LNMP无面板高性能WEB环境

    在"Vultr VPS建站攻略 - 一键安装宝塔面板架设LNMP/LAMP Web环境"文章中,VULTR中文网分享到我们常用的可视化面板宝塔面板安装在VULTR VPS主机中建站 ...

  8. SQL 使用触发器常见错误

    今天做代码审查时,看见以下一段触发器的创建脚本,我们一起来分析一下 create trigger [trigger_puClassRoomType] on [dbo].[puClassRoomType ...

  9. 【转】操作系统Unix、Windows、Mac OS、Linux的故事

    电脑,计算机已经成为我们生活中必不可少的一部分.无论是大型的超级计算机,还是手机般小巧的终端设备,都跑着一个操作系统.正是这些操作系统,让那些硬件和芯片得意组合起来,让那些软件得以运行,让我们的世界在 ...

  10. python_35_进度条

    import sys for i in range(50): sys.stdout.write("+")#此命令不会像print语句执行一次,换行一次\ sys.stdout.fl ...