动态规划,给定长度为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的更多相关文章

  1. HDU1024 Max Sum Plus Plus 【DP】

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

  2. hdu1024 Max Sum Plus Plus[降维优化好题(貌似以后可以不用单调队列了)]

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

  3. hdu1024 Max Sum Plus Plus 滚动dp

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

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

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

  6. hdu1024 Max Sum Plus Plus的另一种解法

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1024 http://www.51nod.com/onlineJudge/questionCode.htm ...

  7. HDU-1024 Max Sum Plus Plus 动态规划 滚动数组和转移优化

    题目链接:https://cn.vjudge.net/problem/HDU-1024 题意 给n, m和一个序列,找m个不重叠子串,使这几个子串内元素和的和最大. n<=1e6 例:1 3 1 ...

  8. HDU1024 Max Sum Plus Plus(DP)

    状态:d(i,j)它代表前j划分数i部并且包括第一j最佳结果时的数.g(i,j)表示前j划分数i最好的结果时,段,g(m,n)结果,需要. 本题数据较大.需採用滚动数组.注意:这题int类型就够用了, ...

  9. HDU1024 Max Sum Plus Plus(dp)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 #include<iostream> #include<vector> #i ...

随机推荐

  1. jquery垂直展开折叠手风琴二级菜单

    摘要:jquery实现垂直展开二级菜单 最近新开发一个简单项目,用到左侧两级的菜单.找找了手头的文件,竟然没有现成的代码,算了,去网上找找整理下吧. 注:jquery-1.8.3.min.js需要下载 ...

  2. Velocity(1)——注释

    Velocity的单行注释,使用## 多行注释使用#* cooments *#

  3. Iterator和ListIterator主要区别(转)

    Iterator和ListIterator主要区别有: 一.ListIterator有add()方法,可以向List中添加对象,而Iterator不能. 二.ListIterator和Iterator ...

  4. MySQL: ERROR13(HY000):Can't get stat of

    在mysql中load data数据 mysql> load data infile '/home/a.txt' into table table_a;ERROR 13 (HY000): Can ...

  5. 避免硬编码你的PostgreSQL数据库密码

    一个密码文件包含了我们需要连接的五个字段,所以我们可以使用文件权限来使密码更安全. host:port:dbname:user:password such as myhost:5432:postgre ...

  6. 开篇呀,恭喜恭喜,是个好开头-----关于sort()排序

    感觉自己活了半辈子从来没写过博客,这可是头一回,而且不是记事是为了学习,先恭喜恭喜自己,有一个很好的开端,不管能不能半途而废,反正是想着为了学习做点什么. 之前有很多东西需要搬过来,循序渐进吧,反正也 ...

  7. Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  8. Lintcode: Remove Node in Binary Search Tree

    iven a root of Binary Search Tree with unique value for each node. Remove the node with given value. ...

  9. hdu 2528 Area

    2014-07-30 http://acm.hdu.edu.cn/showproblem.php?pid=2528解题思路: 求多边形被一条直线分成两部分的面积分别是多少.因为题目给的直线一定能把多边 ...

  10. [原创]spring学习笔记:关于springsource-tool-suite插件的安装

    1.首先我们得确定自己使用的eclipes的版本,具体方式:打开eclipes > help > About Eclipes > 点击eclipes的logo > 查看ecli ...