Max Sum

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

Total Submission(s): 174588    Accepted Submission(s): 40639

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

题意:给定一个序列,求其子序列的最大和,还有最大和的子序列的起始位置和结束位置。

求和与end都没什么好说的。求start是亮点,一开始判断了很久,后来还是那个想法,对于序列或是字符串正着想想不出来就逆过来想,从左到右end容易求,那从右至左的话start就容易求了。

代码:

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#pragma warning(disable:4996)
using namespace std; int value[100005];
int dp[100005]; int main()
{
//freopen("input.txt","r",stdin);
//freopen("out.txt","w",stdout); int Test,num,i,j,ans,start,end;
value[0]=0; cin>>Test;
for(j=1;j<=Test;j++)
{
ans = -1005;
memset(dp,0,sizeof(dp)); cin>>num;
for(i=1;i<=num;i++)
{
cin>>value[i];
dp[i]=max(dp[i-1]+value[i],value[i]);
if(dp[i]>ans)
{
ans=dp[i];
end=i;
}
}
ans=-1005;
memset(dp,0,sizeof(dp));
for(i=num;i>=1;i--)
{
dp[i]=max(dp[i+1]+value[i],value[i]);
if(dp[i]>=ans)
{
ans=dp[i];
start=i;
}
}
cout<<"Case "<<j<<":"<<endl;
cout<<ans<<" "<<start<<" "<<end<<endl; if(j!=Test)
cout<<endl;
} return 0;
}

后来看discuss里其他人的思路,觉得从end开始往回倒,对每一个值都加起来,什么时候等于求出来的最大和了,什么时候就是start了,觉得这样做也很好。

版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 1003:Max Sum的更多相关文章

  1. HDU 1003:Max Sum(DP,连续子段和)

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

  2. HDU 1024:Max Sum Plus Plus(DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Problem Description Now I think you ...

  3. HDU 1024:Max Sum Plus Plus(DP,最大m子段和)

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

  4. HDU 1024:Max Sum Plus Plus 经典动态规划之最大M子段和

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

  5. 【HDU 1003】 Max Sum

    题 题意 需要在o(n)时间内,求最大连续的子序列的和,及其起点和终点. 分析 一种方法是一边读,一边维护最小的前缀和 s[i] ,然后不断更新 ans = max(ans,s[j] - s[i]), ...

  6. HDU1244:Max Sum Plus Plus Plus

    题目链接:Max Sum Plus Plus Plus 题意:在n个数中取m段数使得这m段数之和最大,段与段之间不能重叠 分析:见代码 //dp[i][j]表示前i个数取了j段的最大值 //状态转移: ...

  7. HDU3415:Max Sum of Max-K-sub-sequence(单调队列)

    Problem Description Given a circle sequence A[1],A[2],A[3]......A[n]. Circle sequence means the left ...

  8. ACM1003:Max Sum

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

  9. HDU-1003:Max Sum(优化)

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

随机推荐

  1. Android中ListView结合CheckBox判断选中项

    本文主要实现在自定义的ListView布局中加入CheckBox控件,通过判断用户是否选中CheckBox来对ListView的选中项进行相应的操作.通过一个Demo来展示该功能,选中ListView ...

  2. Linux安装nginx的环境要求

    # Linux下切记不能乱删东西!我把pcre强制删除后,什么命令都不能使用了,系统奔溃,血的教训! nginx是C语言开发,建议在linux上运行,本教程使用Centos6.4作为安装环境. 一.  ...

  3. 修剪草坪 HYSBZ - 2442

    在一年前赢得了小镇的最佳草坪比赛后,FJ变得很懒,再也没有修剪过草坪.现在,新一轮的最佳草坪比赛又开始了,FJ希望能够再次夺冠. 然而,FJ的草坪非常脏乱,因此,FJ只能够让他的奶牛来完成这项工作.F ...

  4. CSP-S 2019 复赛游记

    自闭游记 >_< Day 0 随便敲了一些板子 当然打了摆. 奶人的话写满了俩黑板啊,没人奶我可海星. 晚上没怎么打摆,随便敲了几道板子,然后很早就回去睡了. Day 1 平静地出发了.. ...

  5. 「CF650E」Clockwork Bomb

    传送门 Luogu 解题思路 显然对于两棵树共有的边,我们不会动它. 考虑第二颗树中有和第一棵树不同的边怎么处理. 我们设 \(fa_1[u],fa_2[u]\) 分别代表 \(u\) 在两棵树中的父 ...

  6. 105、Java中String类之利用indexOf()方法判断子字符串是否存在

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  7. C#中的扩展类的理解

    扩展类是一种静态的一种类的调用方法,通过实例化进行调用.利用this进行指正该类,有参数的时候直接在后面追加参数. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  8. Lesson 48 Planning a share portfolio

    How does the older investor differ in his approach to investment from the younger investor? There is ...

  9. 了解AOP以及实现方式

    AOP是什么? 面向切面编程,把那些与业务无关,却为业务模块所共同调用的逻辑封装成一个可重的模块,即切面 使用"横切"技术,AOP把软件系统分为两个部分:核心关注点和横切关注点.业 ...

  10. NSQ学习记录

    一.简介 NSQ是一个基于Go语言的开源的分布式实时消息平台,他的代码托管在GitHub上. NSQ可用于大规模系统的实时消息服务,它的设计目标是为在分布式环境下提供一个强大的去除中心化的分布式服务架 ...