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. js 限制input输入字节长度

    function WidthCheck(str, maxLen){ var w = 0; var tempCount = 0; //length 获取字数数,不区分汉子和英文 for (var i=0 ...

  2. c++模板类

    c++模板类 理解编译器的编译模板过程 如何组织编写模板程序 前言常遇到询问使用模板到底是否容易的问题,我的回答是:“模板的使用是容易的,但组织编写却不容易”.看看我们几乎每天都能遇到的模板类吧,如S ...

  3. mixin设计模式

    mixin可以轻松被一个子类或者一组子类继承,目的是函数复用.在js中,我们可以将继承MiXin看作为一种通过扩展收集功能的方式. e.mixin = function(t) { for (var i ...

  4. h5移动端滑动的细节

    1.获取手指滑动的长度: var hasTouch = 'ontouchstart' in window && !isTouchPad, _start:function(e){ var ...

  5. jquery特效收藏

    js网址收藏: 懒人图库:www.lanrentuku.com 懒人之家:http://www.lanrenzhijia.com/jquery/list_5_2.html 1.UI下载:http:// ...

  6. EF: Raw SQL Queries

    Raw SQL Queries Entity Framework allows you to query using LINQ with your entity classes. However, t ...

  7. Spring入门_04_注解注入

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xm ...

  8. verilog阻塞与非阻塞的初步理解(三)

    下面这段源码是因为习惯不好,出现不正确波形的例子. module pwm_division(reset,clkin,clkout); input reset,clkin; output clkout; ...

  9. 代码中access 的使用

     C++代码:if(access(strZip.c_str(), 0) == 0){...}    此处为判断strZip中文件是否存在   .c_str() 是他自身字符串名称,该名称是一个压缩文件 ...

  10. Hibernate 应用

    完善的持久化层应该达到以下目标: 1.代码可重用性高,能够完成所有的数据库访问操作. 2.如果有需要的话,能够支持多种数据库平台. 3.具有相对独立性,当持久化层的实现发生变化,不会影响上层的实现. ...