A - Max Sum Plus Plus

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Appoint description:

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 S 1, S 2, S 3, S 4 ... S x, ... S n (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ S x ≤ 32767). We define a function sum(i, j) = S i + ... + S j (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(i 1, j 1) + sum(i 2, j 2) + sum(i 3, j 3) + ... + sum(i m, j m) maximal (i x ≤ i y ≤ j x or i x ≤ j y ≤ j x 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(i x, j x)(1 ≤ x ≤ m) instead. ^_^

 

Input

Each test case will begin with two integers m and n, followed by n integers S 1, S 2, S 3 ... S n.

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,n。

m为你要求的子段个数,n为数据个数。


由于是很早的题型了,但是理解起来还是很是无力。

并于是用了三天来搞懂此类问题。发现网上大多代码无思路整个过程。


就大致讲解一下DP的整个思路。

我们可以很清楚地得,这是一个顺序DP,由ix<=iy可得。

那它在当前位置或者称为状态,又能做出几个决策才能保证到目前状态是最大的呢?

当然是逐步取其上一状态的最大值。

综上所述就可以得此模糊状态转移方程:dp[i][j] =MAX(dp[?][?],dp[??][??]) (或者是三个或者更多)。


下面我们来找它转移方程的本质:

设dp[i][j] 为前j个数字组成i段的最大和。(前提是i<=j ,这是显而易见的,当小于的时候不能组成i段)

所要达到的状态 :dp[i][j].

需要的条件:1、前j-1个数,组成i段的最大和 ;2、前j-1个数,组成的i-1段的最大和。

解释:由dp[i-1][j-1]到dp[i][j],指的是当前第j个数单独为一段,dp[i][j-1]->dp[i][j],第j个数接在第i段后面。

继续深入:我们是要求dp[i-1][j-1],dp[i][j-
1]的最大值。那么i段就是在当前这个循环更新i->n状态的,所以dp[i][j-1]时刻在更新,反之,当前dp[i-1][j-1]的状态没
有得到更新,那么我们就要加入这个j号元素进入时的更新保证dp[i-1][j-1]是当前可满足状态中最大的。

完全的动态规划转移方程dp[i][j]=MAX(dp[i][j-1],dp[i-1][k]) 其中i<=k<=j。

回头再看:n的最大值为100W,二维数组出来就需要巨额的空间。但是想象,当前需要更新的状态是不是就与当前上一状态和上一层的状态有关。

最终解决办法:采用滚动数组(或者两个一维数组)。

下面有两种转移方程的格式,其中也有一点不同,具体代码如下:

#include<cstdio>
#include<cstring>
#define MIN -(1<<30)
#define Max(a,b) ((a)>(b)?(a):(b))
#define M 1000001
int dp[][M],num[M],n,m,max; int main(){ while(~scanf("%d %d",&m,&n)){
int i=,j,t=; while(i<=n)
scanf("%d",&num[i]),dp[][i]=dp[][i]=,i++; for(i=;i<=m;i++,t=!t){ dp[t][i]=dp[!t][i-]+num[i];
dp[!t][i]=Max(dp[!t][i],dp[!t][i-]); for(j=i+;j<=n-m+i;j++){ dp[t][j]=Max(dp[!t][j-],dp[t][j-])+num[j];
dp[!t][j]=Max(dp[!t][j],dp[!t][j-]); }
}
max=MIN;
for(i=m;i<=n;i++)
max=Max(dp[m&][i],max); printf("%d\n",max); }
return ;
}

第二种方法只有一出有所不通,就是采用两个一维数组进行更新:

dp[],t[],但是更值得注意的是,只有dp[j]是当前的状态,而t[k] (i<=k<=j)是当前状态的原状态,所以t[j-1]实际上是代替了方法中的dp[i][j-1],

换言之。就是t[]中 i->j 是接在dp[j]后面的。而dp[i->j],是上一层的状态。这也是为什么t[j-1]要用max来保存,它表示的是当前层的最大值。

不知道有没有讲清楚。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 1000003
#define INF 100000000
int dp[maxn],a[maxn],pre[maxn];
int main(){
int n, m, i, j, maxx;
while(~scanf("%d%d",&m,&n)){
for(i=;i<=n;i++)
scanf("%d",&a[i]);
dp[]=;
memset(pre,,sizeof(pre));
for(i=;i<=m;i++){
maxx =-INF;
for(j=i;j<=n;j++){//对于每个i,随着j的增大,maxx越滚越大
dp[j]=max(dp[j-],pre[j-])+a[j];
pre[j-]=maxx;//把前一轮的最大值赋给pre;
printf("dp[%d]=%d pre[%d]=%d\n",j,dp[j],j-,pre[j-]);
maxx=max(maxx,dp[j]);
}
puts("");
}
printf("%d\n",maxx);//最后一轮的最大值就是答案。
//因为上一个循环中对于每个i,随着j的增大,maxx越滚越大
} //而且pre也是越滚越大的。
return ;
}
//给了一组数据,不理解就把所有DP打出来,自己手动模拟一遍,这样好理解多了 /* 4 6
2 -4 5 6 -8 10 */

 

HDU 1024 max sum plus的更多相关文章

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

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

  2. HDU 1024 Max Sum Plus Plus (动态规划)

    HDU 1024 Max Sum Plus Plus (动态规划) Description Now I think you have got an AC in Ignatius.L's "M ...

  3. HDU 1024 Max Sum Plus Plus(m个子段的最大子段和)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/ ...

  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 1024 Max Sum Plus Plus DP

    Max Sum Plus Plus Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php ...

  6. hdu 1024 Max Sum Plus Plus

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

  7. HDU 1024 Max Sum Plus Plus【DP】

    Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...

  8. HDU 1024 Max Sum Plus Plus(DP的简单优化)

    Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...

  9. hdu 1024 Max Sum Plus Plus(m段最大和)

    Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...

随机推荐

  1. 9月20日下午JavaScript函数--递归

    例题1:公园里有200个桃子,猴子每天吃掉一半以后扔掉一个,问6天以后还剩余多少桃子? var sum = 200; for(var i= 0;i<6;i++) { sum = parseInt ...

  2. Sky Box

    http://www.keithlantz.net/2011/10/rendering-a-skybox-using-a-cube-map-with-opengl-and-glsl/ http://o ...

  3. .NET获取服务器信息,如服务器版本、IIS等

    .NET获取服务器信息,如服务器版本.IIS等 .NET获取服务器信息,如服务器版本.IIS版本.数据库大小.网站系统空间占用大小.网站部署路径等等 .NET获取系统环境信息 #region 返回操作 ...

  4. Spring中ApplicationContext对事件的支持

    Spring中ApplicationContext对事件的支持   ApplicationContext具有发布事件的能力.这是因为该接口继承了ApplicationEventPublisher接口. ...

  5. PHP 短连接生成

    <?php #短连接生成算法 class Short_Url { #字符表 public static $charset = "0123456789ABCDEFGHIJKLMNOPQR ...

  6. web.config中customErrors与httpErrors的区别

    打开IIS,我们发现会有两个处理错误页的地方,见下图: 进行不同的设置之后,我们发现设定结果会反应在web.config: .NET Error Pages设定被写入system.web/custom ...

  7. ul li 下的元素内容垂直居中

    CSS: <style> * {;; } li { list-style: none; } li span { border: 1px solid red; height: 100px; ...

  8. jquery 获取和设置 checkbox radio 和 select option的值?

    ============== 获取和设置 checkbox radio 和 select的值? === val()函数, 其名字就表达了 它的意思: 他就是= value 的简写! val就是valu ...

  9. php 快速排序法

    function quicksort(array $arr = array()){ $len = count($arr); if ($len > 1) { $key = $arr[0]; $l_ ...

  10. oracle中时间运算

    Oracle两个函数相减,默认得到的是天数,按日期格式,精准到响应的精度,如用sysdate(2015/12/7 10:17:52),时间差精确到秒. 在此基础上,oracle两个时间相减默认天数*2 ...