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月23日JavaScript作业----用DIV做下拉列表

    例题二.用div做下拉列表 <title>无标题文档</title> <style type="text/css"> *{ margin:0px ...

  2. live555库中的testRTSPClient实例

    1.testRTSPClient简介 testRTSPClient是个简单的客户端实例,这个实例对rtsp数据交互作了详细的描述,其中涉及到rtsp会话的两个概念Source和Sink. Source ...

  3. Jexus-5.6.3使用详解

    一.Jexus Web Server配置 在 jexus 的工作文件夹中(一般是"/usr/jexus")有一个基本的配置文件,文件名是"jws.conf".j ...

  4. Jsp与servlet的区别 1

     Jsp与servlet的区别 2011-12-09 16:27:47 分类: Java 1.jsp经编译后就变成了Servlet.(JSP的本质就是Servlet,JVM只能识别java的类,不能识 ...

  5. centos 7.0安装花生壳

    没有wget 先下载get  命令 yum -y install wget 下载位置/usr/local/src 解压目录 /usr/local/bin/phddns-2.0.6.x86_64 1.下 ...

  6. 解决label点击事件触发两次问题

    问题描述: 通常,为了用户体验,我们点击单选框或者复选框后面文字,即可选中当前项.代码如下: <label> <input type="radio" name=& ...

  7. android-android获取navigationview 上的控件id

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); View headerView = navi ...

  8. vim黏贴代码格式混乱的解决方法

    from:http://blog.csdn.net/commshare/article/details/6215088 感谢牛人的文章.解决了我在vim使用中,很头疼的问题. 在vim新建文件的时候, ...

  9. Myeclipse中的web项目审查(jquery-2.1.1.min.js)出现错误

    前言,本来在把web项目搞得好看一些,从网上下载了一个很炫酷的模板导入web中,无奈出现了错误,如下:

  10. .net利用NPOI导入导出Excel

    NPOI在.net中的操作Excel 1.读取 using (FileStream stream = new FileStream(@"c:\客户资料.xls", FileMode ...