动态规划,给定长度为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. Leetcode: Palindrome Pairs

    Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ...

  2. navicat内的主键和外键

    数据库内的一个重点是主键另一个是外键 实体完整性{ 主键的全称:主关键字    它能够进行唯一标示某一列的 主键的三大特点是:唯一  非空  排序 一个没有主键的表不是一个完整的表,只要表设置了主键那 ...

  3. Uva 11538 - Chess Queen

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  4. Java基础(45):冒泡排序的Java封装(完整可运行)

    1.冒泡排序 package lsg.ap.bubble; import java.util.*; public class BubbleSort { public static void bubbl ...

  5. c++之路进阶——codevs1286(郁闷的出纳员)

    1286 郁闷的出纳员 2004年NOI全国竞赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master       题目描述 Description OIER公司 ...

  6. C++之路起航——标准模板库(set)

    set(集合):http://baike.baidu.com/link?url=cb68AB-3qfEK8RoaGHJFClb4ZiWpJfc32lPOLtaNUrdxntFC738zCZsCiUlf ...

  7. 。。。Spring框架总结(一)。。。

    Spring框架已经学习了两遍了,第一遍基本上忘得差不多了,现在开始复习第二遍的,也复习的差不多了,比之前懂了很多东西,今天就写下来,记录一下我滴小成果! 首先,在Spring框架中有两个重要的概念: ...

  8. struts一些实用常量配置_2015.01.04

  9. 前端开发与Seo基础

    网页代码优化       1:<title>标题:强调重点,重点关键词放在前面,每个页面的title尽量不相同 2:<meta keywords>关键词:列举出几个重要关键词, ...

  10. linux-批量重命名脚本

    #!/bin/bash # rename jpg and png count= for img in *.jpg *.png do new=image-$count.${img#*.} mv > ...