hdu1024 Max Sum Plus Plus
动态规划,给定长度为n(≤1e6)的整数数组和整数m,选取m个连续且两两无交集的子区间,求所有方案中使得区间和最大的最大值。
dp[i][j]表示结束位置(最后一个区间最后一个元素的位置)为i且选取区间数为j的最大值。
容易得到以下状态转移方程:
又:
考虑到数组的规模和j的更新特征,使用一维滚动数组取代二维数组,最外层循环枚举j到m即可。
用dp[0][i]表示dp[i][j], dp[1][i]表示max(dp[k][j-1]) (k≤i)。
复杂度为O(n^2) 。
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
typedef __int64 LL; const int inf = 0x3f3f3f3f;
const int maxn = 1e6 + ; int s[maxn];
LL dp[][maxn];
LL ans, maxi;
int n, m; int main(){
while(~scanf("%d%d", &m, &n)){
for(int i = ; i <= n; i++) scanf("%d", &s[i]);
ans = maxi = -inf;
memset(dp, , sizeof dp);
int o = ;
for(int j = ; j <= m; j++){
maxi = -inf;
for(int i = j; i <= n; i++){
dp[][i] = s[i] + max(dp[][i - ], dp[][i - ]);
}
for(int i = j; i <= n; i++) maxi = dp[][i] = max(maxi, dp[][i]);
}
for(int i = m; i <= n; i++) ans = max(ans, dp[][i]);
printf("%I64d\n", ans);
}
return ;
}

hdu1024 Max Sum Plus Plus的更多相关文章
- HDU1024 Max Sum Plus Plus 【DP】
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu1024 Max Sum Plus Plus[降维优化好题(貌似以后可以不用单调队列了)]
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu1024 Max Sum Plus Plus 滚动dp
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU1024 Max Sum Plus Plus —— DP + 滚动数组
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS ...
- HDU1024 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 ...
- hdu1024 Max Sum Plus Plus的另一种解法
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1024 http://www.51nod.com/onlineJudge/questionCode.htm ...
- HDU-1024 Max Sum Plus Plus 动态规划 滚动数组和转移优化
题目链接:https://cn.vjudge.net/problem/HDU-1024 题意 给n, m和一个序列,找m个不重叠子串,使这几个子串内元素和的和最大. n<=1e6 例:1 3 1 ...
- HDU1024 Max Sum Plus Plus(DP)
状态:d(i,j)它代表前j划分数i部并且包括第一j最佳结果时的数.g(i,j)表示前j划分数i最好的结果时,段,g(m,n)结果,需要. 本题数据较大.需採用滚动数组.注意:这题int类型就够用了, ...
- HDU1024 Max Sum Plus Plus(dp)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 #include<iostream> #include<vector> #i ...
随机推荐
- Unity3d UGUI 通用Confirm确认对话框实现(Inventory Pro学习总结)
背景 曾几何时,在Winform中,使用MessageBox对话框是如此happy,后来还有人封装了可以选择各种图标和带隐藏详情的MessageBox,现在Unity3d UGui就没有了这样的好事情 ...
- G面经prepare: X-Straight
Define “X-Straight” as X cards with consecutive numbers (X >= 3). Determine if the deck can be fu ...
- Ruby On Rails 环境搭建MySQL数据库连接
1. 安装wamp1.7.4从而自动安装好Apache和MySQL,Apache的端口可能会被IIS服务占用,可以去控制面板里关掉 2. 修改root密码,为了能在phpMyAdmin里继续操作数 ...
- c++之路起航——指针
c++一阶指针 定义 存储类型名 数据类型 * 指针变量名: Eg:int *a://定义了一个指向整型的指针 a: 指针使用方法 int a,*b; b=&a;//表明将a的地址赋值给b: ...
- SPOJ COT3 Combat on a tree(Trie树、线段树的合并)
题目链接:http://www.spoj.com/problems/COT3/ Alice and Bob are playing a game on a tree of n nodes.Each n ...
- 安装Eclipse并配置JacORB插件
前人成果 • eclipse中开发corba完整说明(jacORB版) http://blog.csdn.net/hq0927/article/details/8129534 • ...
- spring的事务回滚
@Transactional(rollbackFor = { Exception.class }) 需要把异常抛出到带有@Transactional(rollbackFor = { Exception ...
- linux与windows的文本文件之间的转换
在CentOS中需要安装一个软件包:tofrodos 包里包含的命令可以用包管理工具列出包里的文件. 以 CentOS 的 rpm 为例: rpm -ql tofrodos 在ArchLinux中需要 ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON BinThreshold
zw版[转发·台湾nvp系列Delphi例程]HALCON BinThreshold unit Unit1;interfaceuses Windows, Messages, SysUtils, Var ...
- 【crunch bang】程序中文化
在应用程序中配置使用中文显示. # apt-get install locales # dpkg-reconfigure locales 安装文泉驿-微米黑字体: sudo apt-get insta ...