题目链接: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. 视觉SLAM中相机详解

    视觉SLAM中,通常是指使用相机来解决定位和建图问题. SLAM中使用的相机往往更加简单,不携带昂贵的镜头,以一定的速率拍摄周围的环境,形成一个连续的视频流. 相机分类: 单目相机:只是用一个摄像头进 ...

  2. 轻量级quill富文本编辑器

    因为公司产品需要在移动端编辑文本,所以发现了这个轻量级的好东西,网上也没找到比较好的案例,就自己总结了下,有兴趣的直接复制代码运行看看就知道啦! 下面是quill.js的CDN加速地址: <!- ...

  3. ubuntu14 搭建单机版hadoop2.6

    1. 如果你的集群尚未安装所需软件,你得首先安装它们. 以Ubuntu Linux为例: $ sudo apt-get install ssh $ sudo apt-get install rsync ...

  4. css中使用if条件在各大浏览器(IE6\IE7\IE8)中hack方法解决教程

    一个滚动代码,其他浏览器都滚的好好的,就IE出现错误,DIV+CSS if条件hack,这里DIVCSS5为大家介绍针对各大浏览器(IE6\IE7\IE8)中使用if条件hack方法教程,DIV CS ...

  5. JavaScript的DOM编程--10--删除节点

    1). removeChild(): 从一个给定元素里删除一个子节点 var reference = element.removeChild(node); 返回值是一个指向已被删除的子节点的引用指针. ...

  6. alpha rarefaction using qiime

    shannon菌群多样性指数 H=-∑(Pi)(㏑Pi) Pi=样品中属于第i种的个体的比例,如样品总个体数为N,第i种个体数为ni,则Pi=ni/N: 各种之间,个体分配越均匀,H值就越大.如果每一 ...

  7. jquery.cookie的path坑

    在使用jquery.cookie设置cookie的时候,通常都是直接设置,没有针对path,domain和expires等进行具体的设置,这会导致,同一个cookie的key对应多个value. 1. ...

  8. Robot Framework学习笔记(一)------环境搭建

    Robot Framework是一款python编写的功能自动化测试框架.具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可以进行分布式测试执行. 所需环境 一.安装pyth ...

  9. AFNetWorking 对汉字部分UTF-8编码

    随笔记一下 好用的小技巧 1.将字典数据拼接成url的参数... AFQueryStringFromParameters NSString *query = AFQueryStringFromPara ...

  10. JS中金额转换以及格式化

    abs = function(val){ //金额转换 分->元 保留2位小数 并每隔3位用逗号分开 1,234.56 var str = (val/100).toFixed(2) + ''; ...