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. Maven的学习资料收集--(五)使用Maven构建Struts2项目

    在前两篇博客中,使用Maven构建了Web项目,在这篇博客中写一下,怎样构建一个简单的Struts2项目. 在准备过程中发现,要使用好Maven,个人觉得要好好利用这两个网站: http://mvnr ...

  2. DNS跳转

    switch (window.location.hostname) { case "www.zcom.gov.cn" ://确定域名为 www.zcom.gov.cn //wind ...

  3. 并发编程:synchronized 锁升级过程的验证

        关于synchronized关键字以及偏向锁.轻量级锁.重量级锁的介绍广大网友已经给出了太多文章和例子,这里就不再重复了,也可点击链接来回顾一下.在这里来实战操作一把,验证JVM是怎么一步一步 ...

  4. DockerSwarm 微服务部署

    一.简介 之前<服务Docker化>中,使用 docker-compose.yml 来一次配置启动多个容器,在 Swarm 集群中也可以使用 compose 文件 (docker-comp ...

  5. Node.js | 你的物联网系统,有个管家待认领

    很多时候,专业的事情都要交给专业的人来做,才会更放心. 例如买了套房,交房装修完毕,欢天喜地入住后,房子的日常养护和维护之类的事情,都由谁来负责呢? 物业呗~买了房子就自然需要房子所在小区提供的物业服 ...

  6. hadoop启动中缺少datanode

    原文链接地址:https://blog.csdn.net/islotus/article/details/78357857 本人测试有效: 首先删除hadoop下的dfs文件(注:本文件不一定在had ...

  7. Yii2 components api/controller

    When we wrote API, those controllers need to implement the following feature: 1. return JSON format ...

  8. 美国L1签证面谈的时候一般VO会问到什么问题?

    L签证:L签证签发给被其中国公司调派到美国分公司或合资公司工作的人员.申请人必须将在美国担任经理级职务或具有专业知识,且在申请签证前的三年中至少为同一雇主或公司连续工作至少一年.签证签发费将因签证的入 ...

  9. cms-静态化组件

    1.要让我们的网站性能更好,那么有的东西是需要做静态化的.做静态化步骤: 1.1在web.xml中配置监听器 1.2.创建一个bean用来实现静态化 web.xml <?xml version= ...

  10. jsop解析获得htmldome

    package com.open1111.jsoup; import org.apache.http.HttpEntity;import org.apache.http.client.methods. ...