题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003

Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 178388    Accepted Submission(s): 41628

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

 
Author
Ignatius.L
 
Recommend
We have carefully selected several similar problems for you:  1176 1087 1069 2084 1058
题意 : 给出一个序列,输出最大子序列和,简单的dp
dp数组储存的是以这个值结尾的最长的子序列和
dp[i] = max(dp[i-1]+num[i] , num[i]);
但是因为要保存起始和终止点的位置,所以可以用结构体来储存dp;
下面是代码
 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100005
#define ll long long
struct DP{
ll sum;
int l;
int r;
bool operator < (const DP d) const
{
if(sum!=d.sum) return d.sum<sum;
else if(l!=d.l) return l<d.l;
else return r<d.r;
}
}dp[N];
ll num[N];
int main()
{
int T;
scanf("%d",&T);
for(int cnt = ; cnt < T ; cnt++)
{
int n;
scanf("%d",&n);
for(int i = ;i < n ;i++)
dp[i].sum = , dp[i].l = i,dp[i].r = i;
for(int i = ;i < n ;i++)
{
scanf("%lld",&num[i]);
if(i==) dp[i].sum = num[],dp[i].l = ,dp[i].r = ; else
{
if(dp[i-].sum+num[i]>=num[i])
{
dp[i].sum = dp[i-].sum+num[i];
dp[i].l = dp[i-].l;
dp[i].r = i;
}
else
{
dp[i].sum = num[i];
dp[i].l = i;
dp[i].r = i;
}
}
}
sort(dp,dp+n);
if(cnt!=) puts("");
printf("Case %d:\n",cnt+);
printf("%lld %d %d\n",dp[].sum,dp[].l+,dp[].r+);
}
return ;
}

Max Sum(dp)的更多相关文章

  1. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  2. hdu 1003 Max Sum (DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)   ...

  3. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

  4. hdu 1003 Max sum(简单DP)

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

  5. 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 ...

  6. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  7. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  8. HDU 1003 Max Sum && HDU 1231 最大连续子序列 (DP)

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

  9. HDU 1024 Max Sum Plus Plus 简单DP

    这题的意思就是取m个连续的区间,使它们的和最大,下面就是建立状态转移方程 dp[i][j]表示已经有 i 个区间,最后一个区间的末尾是a[j] 那么dp[i][j]=max(dp[i][j-1]+a[ ...

随机推荐

  1. canvas学习api

    1.canvas.getContext():获取渲染上下文和绘画功能: 一.绘制矩形 2.ctx.fillRect(x,y,width,height):绘制矩形: 3.ctx.strokeRect(x ...

  2. javascript字符串与数组转换汇总

    本文给大家分享的是Js中字符串转换成数组,数组转换成字符串的函数,十分的简单实用,有需要的小伙伴可以参考下. 数组转字符串 1.join()方法 ? 1 2 3 4 var s= ["a&q ...

  3. Keep Mind Working

    想找一个这样的地方,可以让脑袋持续运转着.不会像游戏一样让人着迷,不会像有色电视一样让人想错地方,也不会像工作一样充满太多严密.就是让脑袋继续转着,适意地思考些什么. 之前会跑去游戏里,至少没有太污. ...

  4. Python入门-数据类型

    一.变量 1)变量定义 name = 100(name是变量名 = 号是赋值号100是变量的值) 2)变量赋值 直接赋值 a=1 链式赋值  a=b=c=1 序列解包赋值  a,b,c = 1,2,3 ...

  5. php require、require_once和include、include_once的区别

    一.引入php文件路径的方法require '文件路径'; require ('文件路径');require_once '文件路径'; require_once ('文件路径');include 同 ...

  6. button的padding属性在i8下和chrome下表现不一致

    button的padding属性在i8下和chrome下表现不一致 在ie8下会撑破很多像素,撑破布局 padding: 10px 48px; padding: 1px 35px \0; /* pro ...

  7. IBM的websphere MQ的c#使用(一)

    接上篇的MQ配置.利用C#实现MQ消息的收发.源码 1.需要引入的dll是amqmdnet.dll 2.app.config配置 <?xml version="1.0" en ...

  8. Mac 安装python ,anaconda。彻底卸载anaconda的方法

    To uninstall Anaconda open a terminal window and remove the entire anaconda install directory: rm -r ...

  9. 【树状数组】BZOJ3132 上帝造题的七分钟

    3132: 上帝造题的七分钟 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1004  Solved: 445[Submit][Status][Dis ...

  10. I/O模型详细解析

    内核空间和用户空间:由于操作系统都包括内核空间和用户空间(或者说内核态和用户态),内核空间主要存放的是内核代码和数据,是供系统进程使用的空间.而用户空间主要存放的是用户代码和数据,是供用户进程使用的空 ...