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. Android课程---Android Studio的一些小技巧

    APK瘦身 在Android Studio中我们可以开启混淆,和自动删除没有Resources文件,来达到给APP瘦身的目的,这对于一些维护很久的老项目比较有用,里面有很多无效的Resource, 删 ...

  2. 【iCore3 双核心板】例程十九:USBD_MSC实验——虚拟U盘

    实验指导书及代码包下载: http://pan.baidu.com/s/1i4eNbQd iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  3. NTFS 权限讲解 ACL

    节选自:Securing Windows Server 2003 4.1 Protecting Files with NTFS File Permissions The primary techniq ...

  4. GFS文件系统和在RedHat Linux下的配置

    GFS的全称是Google file System,为了满足Google迅速增长的数据处理要求,Google设计并实现的Google文件系统(GFS).Google文件系统是一个可扩展的分布式文件系统 ...

  5. jquery动态刷新select的值,后台传过来List<T>,前台解析后填充到select的option中

    jquery动态刷新select的值:将后台传来的List<T>赋值到select下的option. 第一个select选择后出发该方法refreshMerchant(params),传递 ...

  6. javascript大神修炼记(7)——OOP思想(多态)

    读者朋友们大家好,今天我们就接着前面的内容讲,前面我们已经讲到了继承,今天我们就来讲OOP目前最后一个体现,那就是多态,因为javascript语言的灵活性,所以我们是没有办法使用接口的,所以这也给j ...

  7. 移动端性能优化动态加载JS、CSS

    JS CODE (function() { /** * update: * 1.0 */ var version = "insure 1.1.0"; var Zepto = Zep ...

  8. LoadRunner11.00入门教程

    安装成功后,根据教程,有自带的应用程序供新手快速掌握Loadrunner的使用.测试应用是一个基于web的旅行社应用程序,也就是供用户在线预订机票的应用.根据教程和操作,重新总结一下测试流程以及遇到的 ...

  9. PHP检测移动设备类mobile detection使用实例

    目前,一个网站有多个版本是很正常的,如PC版,3G版,移动版等等.根据不同的浏览设备我们需要定向到不同的版本中.不仅如此,我们有时候还需要根据不同的客户端加载不同的CSS,因此我们需要能够检测浏览设备 ...

  10. ArcGIS Server,4000端口被占用

    server使用的端口:http://resources.arcgis.com/zh-cn/help/main/10.2/index.html#//015400000537000000 cmd 输入命 ...