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. 如何下载Oracle E-Business Suite (12.2.6) for Microsoft Windows x64 (64-bit)

    下载地址:https://edelivery.oracle.com/ 使用您的 Oracle 账户进行登录.如果您没有该账户, 请注册 Oracle 账户.     Oracle Software D ...

  2. php 实现格式化数字功能

    php 实现数字格式化功能 /** * @param $num 数字 * @param int $decimal 精度 * @param int $point_len 分隔位长度 * @return ...

  3. sql server 索引总结三

    一.非聚集索引维护 非聚集索引的行定位器值保持相同的聚集索引值,即使该聚集索引列物理上重新定位后,也是如此. 为了优化这个维护开销,SQL Server添加一个指向旧数据页的指针,以在页面分割之后指向 ...

  4. spring 中使用quartz实现定时任务

    一般开发系统,使用定时任务非常常见.当然也可以用Java实现.比如定时器.大致如下: 1: public static void main(String[] args) { 2: Timer time ...

  5. ADO.net数据访问方法

    ADO.NET是一组用于和数据源进行交互的面向对象的类库. 核心组件有两个: DataSet 是 ADO.NET 的非连接(断开)结构的核心组件.DataSet 的设计目的很明确:为了实现独立于任何数 ...

  6. git记录

    2017-3-30:git常用命令:1.$ git init:初始化git仓库2.$ git add *.c:跟踪文件3.$ git commit -m 'initial project versio ...

  7. fpga 状态机 检测1011序列

    1011 可以使用4个状态:s0,s1,s2,s3. 即:(1)s0有0或1两个状态,当s0位0时,进入s0状态,自身打圈.为1那么进入下个状态s1来检测0. (2)s1有0或1两种情况,s1为1时s ...

  8. Sentinel spring-cloud-gateway adapter(1.6)异常错误之@EnableCircuitBreaker

    sentinal 大坑 使用gateway adaper包出现@EnableCircuitBreaker did you forget include starter的异常 这时候千万不要学我引入cl ...

  9. IOS segue(跳转页面处理)

    ● Storyboard上每一根用来界面跳转的线,都是一个UIStoryboardSegue对象(简称Segue) Segue的属性 ● 每一个Segue对象,都有3个属性 ➢ 唯一标识 @prope ...

  10. python_73_pickle序列化(接72)

    # json(为字符串形式)用于不同语言之间的数据交互,只适用于简单的数据交互,字典之类可以,函数就不行了,如下例 ''' import json def say(name):print('Hi!', ...