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. js 限制文本框不能输入单引号

    <input onkeydown="if(event.keyCode==32||event.keyCode==188||event.keyCode==222){return false ...

  2. C# 多线程之线程控制

    方案一: 调用线程控制方法.启动:Thread.Start();停止:Thread.Abort();暂停:Thread.Suspend();继续:Thread.Resume(); private vo ...

  3. 谷歌地图自定义popup框

    谷歌地图的infowindow 不提供官方的定制化 <!DOCTYPE html> <html> <head> <meta name="viewpo ...

  4. android中开启线程

    其实Android启动线程和JAVA一样有两种方式,一种是直接Thread类的start方法,也就是一般写一个自己的类来继承Thread类.另外一种方式其实和这个差不多啊! 那就是Runnable接口 ...

  5. 从零开始的全栈工程师——js篇2.1(js开篇)

    JS开篇 一.js介绍 全称 javascript 但不是java 他是一门前台语言 而java是后台语言js作者 布兰登·艾奇 前台语言:运行在客户端的后台语言:跟数据库有关的 能干什么?    页 ...

  6. windows 下设置MTU数值

    输入:netsh interface ipv4 show subinterfaces 查询到目前系统的MTU值.再分别输入一行按一次回车键. netsh interface ipv4 set subi ...

  7. 改善WPF应用程序性能的10大方法 (转发)

    WPF(Windows Presentation Foundation)应用程序在没有图形加速设备的机器上运行速度很慢是个公开的秘密,给用户的感觉是它太吃资源了,WPF程序的性能和硬件确实有很大的关系 ...

  8. zabbix-3.4-服务监控

    服务监控 总览 服务监控(services monitoring)旨在帮助那些想要高级(业务)基础设施的监控的人.在许多情况下,我们关注的不是底层细节,比如磁盘空间不足.CPU 负载高等.我们关注的是 ...

  9. 进程加载与segment

    elf文件是一组结构体和数据的组合. elf文件是一种文件格式,这种格式定义了进程加载器如何读取elf文件的内容. elf文件的程序头或者segment对如何加载(读取)做了说明.

  10. numpy.random.shuffle(x)的用法

    numpy.random.shuffle(x) Modify a sequence in-place by shuffling its contents. Parameters: x : array_ ...