DP + 单调队列优化 + 平衡树 好题


Description

Given an integer sequence { an } of length N, you are to cut the sequence into several parts every one of which is a consecutive subsequence of the original sequence. Every part must satisfy that the sum of the integers in the part is not greater than a given integer M. You are to find a cutting that minimizes the sum of the maximum integer of each part.

Input

The first line of input contains two integer N (0 < N ≤ 100 000), M. The following line contains N integers describes the integer sequence. Every integer in the sequence is between 0 and 1 000 000 inclusively.

Output

Output one integer which is the minimum sum of the maximum integer of each part. If no such cuttings exist, output −1.

Sample Input

8 17

2 2 2 8 1 8 2 1

Sample Output

12

Hint

Use 64-bit integer type to hold M.


题目大意

给你一个长度为 n 序列,要你把它分成不同块,使得每块最大值的和最小,每块内数字的和不超过 m。题意很简单。

题解

这道题是一道非常好的单调队列优化 DP 的题。首先,我们忽略掉 m 这个条件,我们可以很直接地看出,可以设 dp[i] 为前 i 个数可以得到的最优答案。转移方程为:

dp[i] = min{ dp[j] + max{a[j+1], a[j+2] ... a[i]} };

朴素的转移时间复杂度为\(O(n^2)\), 所以我们考虑用某种方法来优化掉多出来的这个\(n\)。

首先我们来看怎样优化求区间内最大值,我们可以想到用rmq,但很快就被否定,再一想,单调队列!!

很明显,dp[j] 是单调不下降的,所以,当当前块内最大值确定时,j 越小越好,我们可以模拟边界 j 由后向前推进时的情况,如果 a[j] 大于了当前的最大值,那么,当前块内的max值会增大,否则不变。

这个不变就为我们提供了优化的前提,我们可以根据把一段不变的区间压缩成只更新一次,这样,我们就去掉了多余的决策。

于是,我们维护一个单调队列 q[], q[] 中维护的是单调下降的下标,

如样例 对a[4~8] {8,1,8,2,1},i = 8, 单调队列中存的值是{6,7,8},返回a[]中的值为{8,2,1}。

当 4 <= j <= 6 时,max{a[j] ... a[i]} 都等于 8;

当7 <= j <= 7 时,max{...}等于 2;

当8 <= j <= 8 时,max{...}等于 1;

然后我们加上限制条件 m,我们只用从单调队列头部删除不满足要求的值即可。

然后我们只用从这几个点来更新,是不是优化了很多?

等一等,如果数组a[] 本身就是单调下降的怎么办,在这种情况下,时间复杂度依然可以高达\(O(n^2)\)。不用急,我们再继续往下探究,看是否可以在\(O(1)\)或\(O(log_n)\)的复杂度内快速找到使答案最小的那个 j 。

对于一个 j,如果其在更新 dp[i - 1] 时就已经出现,并且在更新 dp[i] 时没有被删除,那么可以得到:

dp[j-1] + max{a[j] ... a[i-1]} == dp[j-1] + max{a[j] ... a[i]}

即,我们可以用本来是更新 dp[i - 1] 的 j 来继续更新 dp[i]。

所以我们可以将 dp[j-1] + max{a[j] ... a[i]} 用一颗平衡树来维护,每次直接取出最小值来更新 dp[i] 即可。

于是这道题就完美解决了,时间复杂度为\(O(nlog_n)\)。

但是因为数据太弱,我们用 STL 中的 set 就可以通过,set 不仅常数太大,并且erase是直接在树中删掉某个数,而不是使值减一,这样,如果出现两个相同的数同时出现在 set 中,在删除时就会导致答案错误。所以,正解应该手写一颗平衡树。

更有甚者,理论复杂度为\(O(n^2)\)的不用平衡树的算法居然比手写平衡树还要快。。。poj你数据是有多水 2333


UPD: 之前有一点错了, set确实是不行, 但是STL中还提供了multiset多元集合,支持多个相同元素。用法和set几乎一样。

下面是\(O(n^2)\)和\(O(nlog_n)\)两份代码。

\(O(n^2)\)代码

#include <iostream>
#include <set>
#include <cstdio>
using namespace std;
typedef long long LL; const int maxn = 1e5 + 5;
int n;
LL m;
LL a[maxn],sum[maxn];
int q[maxn];
LL dp[maxn]; int main() {
ios::sync_with_stdio(false); cin.tie(0);
while(scanf("%d%lld",&n,&m) != EOF) {
int ok = 0;
for(int i = 1;i <= n;i++) {
scanf("%lld",a+i);if(a[i] > m) ok = 1;
sum[i] = a[i] + sum[i-1];
}
int p = 1,front = 0,tail = -1;
for(int i = 1;i <= n;i++) {
if(ok)break;
while(sum[i] - sum[p-1] > m)p++; while(front <= tail && a[q[tail]] <= a[i]) tail--;
q[++tail] = i; while(q[front] < p) front++; dp[i] = dp[p-1] + a[q[front]];
for(int j = front;j < tail;j++) dp[i] = min(dp[i],dp[q[j]] + a[q[j+1]]);
}
if(ok) dp[n] = -1;
printf("%lld\n",dp[n]);
} return 0;
}

\(O(nlog_n)\)代码

#include <iostream>
#include <set>
#include <cstdio>
using namespace std;
typedef long long LL; const int maxn = 1e5 + 5;
int n;
LL m;
LL a[maxn],sum[maxn];
int q[maxn];
LL dp[maxn]; multiset <LL> s; int main() {
ios::sync_with_stdio(false); cin.tie(0);
while(scanf("%d%lld",&n,&m) != EOF) {
int ok = 0;
for(int i = 1;i <= n;i++) {
scanf("%lld",a+i);if(a[i] > m) ok = 1;
sum[i] = a[i] + sum[i-1];
}
int p = 1,front = 0,tail = -1;
s.clear();
for(int i = 1;i <= n;i++) {
if(ok)break;
while(sum[i] - sum[p-1] > m)p++; while(front <= tail && a[q[tail]] <= a[i]) {
if(front < tail) s.erase(dp[q[tail-1]] + a[q[tail]]);
tail--;
}
q[++tail] = i; if(front < tail) s.insert(dp[q[tail-1]] + a[i]); while(q[front] < p) {
if(front < tail) s.erase(dp[q[front]] + a[q[front+1]]);
front++;
} dp[i] = dp[p-1] + a[q[front]];
if(front < tail) dp[i] = min(dp[i], *s.begin());
}
if(ok) dp[n] = -1;
printf("%lld\n",dp[n]);
} return 0;
}

[poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)的更多相关文章

  1. POJ-3017 Cut the Sequence DP+单调队列+堆

    题目链接:http://poj.org/problem?id=3017 这题的DP方程是容易想到的,f[i]=Min{ f[j]+Max(num[j+1],num[j+2],......,num[i] ...

  2. poj 3017 Cut the Sequence(单调队列优化DP)

    Cut the Sequence \(solution:\) 这道题出的真的很好,奈何数据水啊! 这道题当时看得一脸懵逼,说二分也不像二分,说贪心也不像贪心,说搜索吧这题数据范围怎么这么大?而且这题看 ...

  3. POJ 3017 Cut the Sequence (单调队列优化DP)

    题意: 给定含有n个元素的数列a,要求将其划分为若干个连续子序列,使得每个序列的元素之和小于等于m,问最小化所有序列中的最大元素之和为多少?(n<=105.例:n=8, m=17,8个数分别为2 ...

  4. poj 3017 Cut the Sequence(单调队列优化 )

    题目链接:http://poj.org/problem?id=3017 题意:给你一个长度为n的数列,要求把这个数列划分为任意块,每块的元素和小于m,使得所有块的最大值的和最小 分析:这题很快就能想到 ...

  5. 3622 假期(DP+单调队列优化)

    3622 假期 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 黄金 Gold 题目描述 Description 经过几个月辛勤的工作,FJ决定让奶牛放假.假期可以在1-N天内任意选择 ...

  6. DP+单调队列 codevs 1748 瑰丽华尔兹(还不是很懂具体的代码实现)

    codevs 1748 瑰丽华尔兹 2005年NOI全国竞赛  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解       题目描述 Descripti ...

  7. APIO2010特别行动队(单调队列、斜率优化)

    其实这题一看知道应该是DP,再一看数据范围肯定就是单调队列了. 不过我还不太懂神马单调队列.斜率优化…… 附上天牛的题解:http://www.cnblogs.com/neverforget/arch ...

  8. hdu4374One hundred layer (DP+单调队列)

    http://acm.hdu.edu.cn/showproblem.php?pid=4374 去年多校的题 今年才做 不知道这一年都干嘛去了.. DP的思路很好想 dp[i][j] = max(dp[ ...

  9. 习题:烽火传递(DP+单调队列)

    烽火传递[题目描述]烽火台又称烽燧,是重要的防御设施,一般建在险要处或交通要道上.一旦有敌情发生,白天燃烧柴草,通过浓烟表达信息:夜晚燃烧干柴,以火光传递军情.在某两座城市之间有n个烽火台,每个烽火台 ...

随机推荐

  1. 下载包含src,tgz,zip的文件

    下载哪一个文件? 含src的是源码文件,含tgz和zip的分别是linux和windows系统下jar包(java文件包) asc,md5,sha1是三种加密文件,可用于判断文件是否被修改. Ente ...

  2. Color Space: Ycc

    在进行图像扫描时,有一种重要的扫描输入设备PhotoCd,由于PhotoCd在存储图像的时候要经过一种模式压缩,所以PhotoCd采用了Ycc颜色空间,此空间将亮度作由它的主要组件,具有两个单独的颜色 ...

  3. oracle课堂笔记

    1.DOS登录1.1.sqlplus 输入用户名.密码1.2.sqlplus /nolog conn 用户名/密码@ip地址/数据库名称 [如果是sys登录则必须加上as sysdba ,as sys ...

  4. jQuery 控制表单和表格

    这里总结了jQuery中对表格和表单的一些常用操作, 通过这里的实例和操作肯定对jQuery的掌握有一个新得提高, 希望大家耐心看完, 多实践. <!DOCTYPE html PUBLIC &q ...

  5. php5-fpm.sock failed (13: Permission denied) error

    In order to fix the php5-fpm.sock failed error follow these instructions 1) Make sure your virtual h ...

  6. P1941 飞扬的小鸟

    此题很容易写出方程,由以前的知识可以迁移得,本题可以用完全背包的方法进行优化,使用滚动数组即可得到答案. //莫名奇妙60分.不知道什么细节出了错. #include <bits/stdc++. ...

  7. python 内存泄漏调试

    Python应用程序内存泄漏的调试 Quake Lee quakelee@geekcn.org 新浪网技术(中国)有限公司 Sina Research & Development Python ...

  8. angularJs自定义指令.directive==类似自定义标签

    创建自定义的指令 除了 AngularJS 内置的指令外,我们还可以创建自定义指令. 你可以使用 .directive 函数来添加自定义的指令. 要调用自定义指令,HTML 元素上需要添加自定义指令名 ...

  9. LeetCode Power of Three

    原题链接在这里:https://leetcode.com/problems/power-of-three/ 与Power of Two类似.检查能否被3整除,然后整除,再重复检查结果. Time Co ...

  10. LeetCode Zigzag Iterator

    原题链接在这里:https://leetcode.com/problems/zigzag-iterator/ 题目: Given two 1d vectors, implement an iterat ...