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. js计算日期增加

    <div class="time"> <i class="visa_icon prev"></i><span id=& ...

  2. opencv 学习总结 方法总结

    师者传道受业解惑也,图片识别是门学科,需要师者传教,才会较快解开谜团,解开困惑,没人引导,要学会图片识别,有点难度,因为其中的做法超出自己的想象范围. 大家都知道,在超出想象范围,或者从未想到的方式, ...

  3. fis代码压缩

    Fis代码压缩步骤 1,安装fis(http://fis.baidu.com/fis3/docs/beginning/install.html) fis安装支持的node版本:0.8x,0.10x,0 ...

  4. 看懂 Fiddler 的瀑布图

    最近准备给组内的新同学们分享下 Fiddler 这枚神器,可以讲的地方太多,我打算把一节课讲不完的内容写在博客上,大家可以随便看看.今天先介绍下 Fiddler 的瀑布图. 每个网络请求都会经历域名解 ...

  5. jmeter脚本录制的两种方式

    完成一次完整的性能测试: 1.创建用户: 2.选择协议(HTTP) 3.使用工具去模拟协议操作(1.手工编写(抓包工具):2.工具自带录制) 4.运行工具进行压力测试

  6. vue input,textarea失去焦点调用函数方法

    <input type="number" class="num" value="1" @blur.prevent="chan ...

  7. QueryRunner类的八种结果处理集

    package cn.jy.demo; import java.sql.Connection; import java.sql.SQLException; import java.util.List; ...

  8. sublime使用技巧

    引用自:https://www.cnblogs.com/xiayuhao/p/9000216.html https://www.cnblogs.com/ma-dongdong/p/7653231.ht ...

  9. 2017/2/10:Manven简介与项目管理(入门)

    1.Maven工程的创建 2.使用Manven manven配置文件主要集中在 http://m.blog.csdn.net/article/details?id=50316383

  10. 使用tensorflow下的GPU加速神经网络训练过程

    下载CUDA8.0,安装 下载cuDNN v5.1安装.放置环境变量等. 其他版本就不装了.不用找其他版本的关系. 使用tensorflow-gpu1.0版本. 使用keras2.0版本. 有提示的. ...