http://acm.hdu.edu.cn/showproblem.php?pid=1024

Max Sum Plus Plus

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

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
 
http://www.cnblogs.com/lishuhuakai/archive/2012/10/13/4840323.html  (感觉写的很不错, 反正我是看懂了)
 
 
这道题在国赛前我就看了, 可是一直不知如何下手, 终于决定一定要把它给A了, 现在的我能沉下心来, 好好看解析, 好好的自己去理解, 果然还是懂了的
 
重点在递归式上
 

w[i][j]: 前 j 个数分为 i 段, 第 j 个数必须选;1. 第 j 个数单独为1段;2. 第 j 个数与前面的数连一块。
w[i][j] = max(b[i-1][j-1], w[i][j-1]) + a[j];
b[i][j]:前 j 个数分为 i 段, 第 j 个数可选可不选; 1.选第 j 个数;2.不选第 j 个数。
b[i][j] = max(b[i][j-1], w[i][k]);

/// w[j] 表示 j 个元素取 i 段, a[j] 必须取是的最大值
w[j] = max(dp[1-t][j-1], w[j-1]) + sum[j]-sum[j-1];
/// dp[t][j] 表示在a[j]可取可不取这两种情况下取得的最大值
dp[t][j] = max(dp[t][j-1], w[j]);

 

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 1000001
using namespace std; int sum[N], w[N], dp[][N]; ///sum[i] 里面存的是前 i 项和 int main()
{
int m, n; while(scanf("%d%d", &m, &n)!=EOF)
{
int i, j, x; sum[] = ;
for(i=; i<=n; i++)
{
scanf("%d", &x);
sum[i] = sum[i-] + x;
dp[][i] = ; ///从前 i 个元素中取 0 段, 最大值为 0
} /** 我们先假设a[i]中 存放该序列的第 i 个值,
w[i][j] 表示前 j 个数分为 i 段, 第 j 个数必须选这种情况下取得的最大值
b[i][j]表示在前 j 个数中取 i 段 这种情况写取得的最大值 w[i][j]: 前 j 个数分为 i 段, 第 j 个数必须选;1. 第 j 个数单独为1段;2. 第 j 个数与前面的数连一块。
w[i][j] = max(b[i-1][j-1], w[i][j-1]) + a[j];
b[i][j]:前 j 个数分为 i 段, 第 j 个数可选可不选; 1.选第 j 个数;2.不选第 j 个数。
b[i][j] = max(b[i][j-1], w[i][j]); **/ int t=;
for(i=; i<=m; i++) /// i表示取 i 段
{
for(j=i; j<=n; j++) /// 如果dp[i][j](j<i)是没有意义的
{
if(i==j)
dp[t][j] = w[j] = sum[j];
else
{
/// w[j] 表示 j 个元素取 i 段, a[j] 必须取是的最大值
w[j] = max(dp[-t][j-], w[j-]) + sum[j]-sum[j-];
/// dp[t][j] 表示在a[j]可取可不取这两种情况下取得的最大值
dp[t][j] = max(dp[t][j-], w[j]);
}
}
t = -t; ///t在 0 和 1 直间交替变换 /** 为什么要交换呢??? 这是为了要节省空间
仔细观察递归式
w[i][j] = max(b[i-1][j-1], w[i][j-1]) + a[j];
b[i][j] = max(b[i][j-1], w[i][j]);
我们发现,对于取 i 段, w[i][j] 只与 b[i-1][k-1] 和 w[i][k-1] 有关, 与之前的那一些项没有关系
因此我们的数组可以开小一点, 用更新来覆盖掉前面的值!!! **/
} printf("%d\n", dp[m%][n]);
}
return ;
}
 
 
 
 

(动态规划)Max Sum Plus Plus--hdu--1024的更多相关文章

  1. 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" ...

  2. 最大m段子段和 Day9 - E - Max Sum Plus Plus HDU - 1024

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

  3. Max Sum Plus Plus HDU - 1024 基础dp 二维变一维的过程,有点难想

    /* dp[i][j]=max(dp[i][j-1]+a[j],max(dp[i-1][k])+a[j]) (0<k<j) dp[i][j-1]+a[j]表示的是前j-1分成i组,第j个必 ...

  4. C - Max Sum Plus Plus HDU - 1024

    用二位数组dp[i][j]记录组数为i,前j个数字的最大子段和. 转移方程: dp[i][j],考虑第j个数,第j个数可以并到前面那一组,此时dp[i][j]=dp[i][j-1]+arr[j],第j ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. HDU 1024 Max Sum Plus Plus [动态规划+m子段和的最大值]

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

  9. hdu 1024 Max Sum Plus Plus (动态规划)

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

  10. 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. react-router4 第一篇

    无奈,英语4级没过,只能靠猜了.. 首先就是安装了 npm install --save-dev react npm install --save-dev react-dom npm install ...

  2. Charles基本使用

    Charles使用 查找电脑IP,菜单选项helpàLocal IP Addresses 手机连接代理 手机打开WiFi,把代理模式设置为手动,设置主机名为Charles所在机器的ip,端口号为Cha ...

  3. [转]slf4j 与log4j 日志管理

    log4j简易入门 package test.log4j; import org.apache.log4j.Logger; public class HelloLog4j { private stat ...

  4. 编程学习笔记(第四篇)面向对象技术高级课程:绪论-软件开发方法的演化与最新趋势(4)meta、元与元模型、软件方法的未来发展

    一.meta.元与元模型 1.元. ​ "元" 英语是 Meta,meta在不同的行业领域有不同的翻译,在 IT 领域一般来说 Meta 是翻译成元,主要因为在 IT 中Meta ...

  5. powerdesigner mysql逆向工程注释不显示问题

  6. 如何在Eclipse下安装SVN插件——subclipse

    如何在Eclipse下安装SVN插件——subclipse | 浏览:2799 | 更新:2014-09-20 22:39 1 2 3 4 5 6 分步阅读 版本控制是开发人员必不可少的工具,而SVN ...

  7. 安装crf++

    在这里就不提心酸的安装过程了,就把成功安装及部分问题整理出来,以供参考: 安装环境:ubuntu14 1.安装ruby包  sudo apt-get install ruby2.安装zlib包  su ...

  8. Twitter 相关APP开发

    首先要获取 Consumer Key (API Key), Consumer Secret (API Secret):最好申请Access Token 和Access Token Secret,不然验 ...

  9. 【WebService】使用JDK开发WebService(二)

    WebService的开发手段 1.使用JDK开发(1.6及以上版本) 2.使用CXF框架开发(工作中) WebService的组成 1.服务器端 2.客户端 使用JDK开发WebService a. ...

  10. canvas 实现弹跳效果

    一:创建画布 <canvas width="600" height="600" id="canvas"></canvas& ...