HDOJ 1024 Max Sum Plus Plus -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024
Problem Description
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. ^_^
Process to the end of file.
1 3 1 2 3
2 6 -1 4 -2 3 -2 3
6
8
分析
设状态为 cur[i,j],表示前 j 项分为 i 段的最大和,且第 i 段必须包含 data[j],则状态转移方程如下:
cur[i,j] = max{cur[i,j − 1] + data[j],max{cur[i − 1,t] + data[j]}}, 其中i ≤ j ≤ n,i − 1 ≤ t < j
target = max{cur[m,j]}, 其中m ≤ j ≤ n
分为两种情况:
• 情况一,data[j] 包含在第 i 段之中,cur[i,j − 1] + data[j]。
• 情况二,data[j] 独立划分成为一段,max{cur[i − 1,t] + data[j]}。
观察上述两种情况可知 cur[i,j] 的值只和 cur[i,j-1] 和 cur[i-1,t] 这两个值相关,因此不需要二维数组,
可以用滚动数组,只需要两个一维数组,用 cur[j] 表示现阶段的最大值,即 cur[i,j − 1] + data[j],用
pre[j] 表示上一阶段的最大值,即 max{cur[i − 1,t] + data[j]}。
#include <stdio.h>
#include <stdlib.h>
#include <limits.h> int MaxSum(int * data, int m, int n){
int i, j, max_sum;
int * cur = (int *)calloc(n + 1, sizeof(int));
int * pre = (int *)calloc(n + 1, sizeof(int));
data = data - 1; //data下标从0开始, cur、pre下标从1开始,为使下标一致,data减1
for (i = 1; i <= m; ++i){
max_sum = INT_MIN;
for (j = i; j <= n; ++j){
if (cur[j - 1] < pre[j - 1])
cur[j] = pre[j - 1] + data[j];
else
cur[j] = cur[j - 1] + data[j];
pre[j - 1] = max_sum;
if (max_sum < cur[j])
max_sum = cur[j];
}
pre[j - 1] = max_sum;
}
free(cur);
free(pre);
return max_sum;
} int main(void){
int m, n, i, *data;
while (scanf("%d%d", &m, &n) != EOF){
data = (int *)malloc(sizeof(int) * n);
for (i=0; i<n; ++i){
scanf("%d", &data[i]);
}
printf ("%d\n", MaxSum(data, m, n));
free(data);
} return 0;
}
参考资料:ACM Cheat Sheet
HDOJ 1024 Max Sum Plus Plus -- 动态规划的更多相关文章
- 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 ...
- hdu 1024 Max Sum Plus Plus (动态规划)
Max Sum Plus PlusTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 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 ...
- 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 ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- hdu1003 1024 Max Sum&Max Sum Plus Plus【基础dp】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4302208.html ---by 墨染之樱花 dp是竞赛中常见的问题,也是我的弱项orz, ...
- 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/ ...
- 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 ...
- 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 ...
随机推荐
- maven快速入门
一.maven maven可以说是管理项目的优秀工具,管理jar包 二.mave安装 1.先安装jdk(本文不详细讲) 2.安装maven ①.maven下载 http://maven.apach ...
- jquery ajax 报交请求返回 HTTP 400 错误
提交请求的AJAX代码如下: 点击(此处)折叠或打开 $.ajax({ url: "${ctx}/selfhelp/userAttributeAnalysis/userAttributeLi ...
- 经常使用的webservice接口
Web Service 一些对外公开的网络服务接口 2011-10-29 14:12 商业和贸易: 1.股票行情数据 WEB 服务(支持香港.深圳.上海基金.债券和股票:支持多股票同一时候查询) En ...
- C#-将控件动态添加到选项卡页tablepage
tabPage1.Controls.Add(new Button()); 实例: Button cp = new Button(); cp.text="test";cp.Click ...
- Android选择系统相册或拍照上传
PhotoUtils.rar
- Android开发实例之多点触控程序
智能终端设备的多点触控操作为我们带来了种种炫酷体验,这也使得很多Android开发者都对多点触控程序的开发感兴趣.实际上多点触控程序的实现并不是那么遥不可及,而是比较容易.本文就主要通过一个实例具体讲 ...
- Lazy Load 图片延迟加载(转)
jQuery Lazy Load 图片延迟加载来源 基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. ...
- C 栈顺序存储
// seqstack.h #ifndef _MY_SEQSTACK_H_ #define _MY_SEQSTACK_H_ typedef void SeqStack; SeqStack* SeqSt ...
- 初探 MySQL 的 Binlog
https://xcoder.in/2015/08/10/mysql-binlog-try/
- 文件I/O之sync、fsync和fdatasync函数
传统的UNIX实现在内核中设有缓冲区高速缓存或页面高速缓存,大多数磁盘I/O都通过缓冲进行.当将数据写入文件时,内核通常先将数据复制到其中一个缓冲区中,如果 该缓冲区尚未写满,则并不将其排入输出队列, ...