题目:

Max Sum Plus Plus

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

Problem Description
Now I think you have got an AC in Ignatius.L's "Max
Sum" problem. To be a brave ACMer, we always challenge ourselves to more
difficult problems. Now you are faced with a more difficult
problem.

Given a consecutive number sequence S1,
S2, S3, S4 ...
Sx, ... Sn (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤
Sx ≤ 32767). We define a function sum(i, j) =
Si + ... + Sj (1 ≤ i ≤ j ≤ n).

Now
given an integer m (m > 0), your task is to find m pairs of i and j which
make sum(i1, j1) + sum(i2,
j2) + sum(i3, j3) + ... +
sum(im, jm) maximal (ix
iy ≤ jx or ix
jy ≤ jx is not allowed).

But I`m
lazy, I don't want to write a special-judge module, so you don't have to output
m pairs of i and j, just output the maximal summation of
sum(ix, jx)(1 ≤ x ≤ m) instead.
^_^

 
Input
Each test case will begin with two integers m and n,
followed by n integers S1, S2,
S3 ... Sn.
Process to the end of
file.
 
Output
Output the maximal summation described above in one
line.
 
Sample Input
1 3 1 2 3
2 6 -1 4 -2 3 -2 3
 
Sample Output
6
8

Hint

Huge input, scanf and dynamic programming is recommended.

与求最大连续子段略有不同,此题目要求求出m个连续子段的sum,且使得sum尽量大;
考虑状态转移方程:f{i,j}表示前j个元素分为i段的最大值,则有  f{i,j}=max{f{i,j-1},max{i-1,k}}+a[j],(i-1<=k<j);
两种决策:
-1-:  前j个元素分为i段切最后一段以a结尾;
-2-:aj独自占一段,剩下的i-1段选前k(i-1<=k<j)个元素的i-1段中的最大值
n为100w考虑使用滚动数组,pre[j]保存上一个i对应的j的的最大值,dp[j]为当前正在计算的i的j的最大值;
代码:

#include<bits/stdc++.h>
using namespace std;
const int inf=999999999;
int dp[1000005],a[1000005],pre[1000005];
int main()
{
int n,m,i,j,k,t;
while (cin>>m>>n){int maxn=-inf;t=0;
memset(dp,0,sizeof(dp));
memset(pre,0,sizeof(pre));
for (i=1;i<=n;i++) scanf("%d",&a[i]);

for (i=1;i<=m;i++){
maxn=-inf;                                              //每一轮开始都将maxn初始化
for (j=i;j<=n;j++){
dp[j]=max(pre[j-1],dp[j-1])+a[j];             //先使用在更新上一轮的值
pre[j-1]=maxn;                                         //此处很重要,不可写成下文注释的形式,因为一旦那样写下一个j计算时用到的j-1变成了本轮的最大值的意思,概念就变了

if (maxn<dp[j]) maxn=dp[j];
//pre[j]=maxn;
}
//for(int l=0;l<=n;l++) cout<<pre[l]<<" ";
//cout<<endl;
}
cout<<maxn<<endl;
}
return 0;
}

hdu 1024 最大M子段dp的更多相关文章

  1. hdu 1024 最大m子段和

    注:摘的老师写的 最大m子段和问题 以-1 4 -2 3 -2 3为例最大子段和是:6最大2子段和是:4+(3-2+3)=8所以,最大子段和和最大m子段和不一样,不能用比如先求一个最大子段和,从序列中 ...

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

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

  3. 怒刷DP之 HDU 1024

    Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  4. HDU 1024 max sum plus

    A - Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

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

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

  6. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  7. hdu 2829 Lawrence(斜率优化DP)

    题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...

  8. hdu 4568 Hunter 最短路+dp

    Hunter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  9. Max Sum Plus Plus HDU - 1024

    Max Sum Plus Plus     HDU - 1024 Now I think you have got an AC in Ignatius.L's "Max Sum" ...

随机推荐

  1. php header utf8 插入header("Content-type: text/html; charset=utf-8");

    PHP文件插入header("Content-type: text/html; charset=utf-8"); 相当于页面里面的<meta http-equiv=" ...

  2. pdf2swf 中文乱码问题

    准备资料: 1.xpdfbin-win-3.03.zip.xpdf-chinese-simplified.tar.gz 下载地址:http://www.foolabs.com/xpdf/downloa ...

  3. laravel 对接支付宝

    使用的库 omnipay-alipay 申请支付宝支付 这个就不说了, 不明白如何下手的伙伴让运营去和支付宝客服联系吧 composer 安装git库 将以下代码添加到 composer.json { ...

  4. html模板生成静态页面及模板分页处理

    它只让你修改页面的某一部分,当然这"某一部分"是由你来确定的.美工先做好一个页面,然后我们把这个页面当作模板(要注意的是这个模板就没必要使用EditRegion3这样的代码了,这种 ...

  5. lambda表达式学习

    Lambda 表达式是一种可用于创建 委托 或 表达式目录树 类型的 匿名函数 . 通过使用 lambda 表达式,可以写入可作为参数传递或作为函数调用值返回的本地函数. Lambda 表达式对于编写 ...

  6. |和||以及&和&&

    https://msdn.microsoft.com/en-us/library/6a71f45d.aspx Logical OR Operator 按位或 This operator has hig ...

  7. 《重构网络:SDN架构与实现》Chapter7 SDN与网络虚拟化 随笔

    参考: <重构网络:SDN架构与实现>,作者:杨泽卫.李呈. Chapter7 SDN与网络虚拟化 结构 7.1 网络虚拟化 7.1.1 为什么需要网络虚拟化技术 7.1.2 网络虚拟化 ...

  8. 【Coursera】Technology :Fifth Week(1)

    Store and Forward Networking 在长途连接中,路由的数量 取决于 最优路径(花费最少).也就是说被地理条件所限制. 所以 Store and Forward Networki ...

  9. QQ名片自动点赞

    2017-01-23 简介:QQ名片自动点赞,1秒左右可点完1屏好友的赞,每个好友10个赞. 尺寸: 720*1280 DPI:320 宿主:3.1.2.10711 系统:Android v5.11 ...

  10. UVa 1626 括号序列(矩阵连乘)

    https://vjudge.net/problem/UVA-1626 题意: 输入一个由 "(" . ")" . "[" . " ...