题目链接

$O(n^2)$ 效率的 dp 递推式:${ dp }_{ i }=min\left( dp_{ j }+\overset { i }{ \underset { x=j+1 }{ max }  } \left( { a }_{ x } \right)  \right) $,其中 $\sum _{ x=j+1 }^{ i }{ { a }_{ i } } \le m$。

尝试着换一个角度看待这个问题,有一个序列 $a$,假设 $b_i$ 表示 $i$ 最左能扩展到 $b_i$ 位置,且 $[b_i, i]$ 的区间和不超过 $m$。

假设我们已经求得了 $dp_1$ 至 $dp_{i-1}$,现在需要求解 $dp_i$,先定义一个后缀最大值数组,$max_i$ 表示 区间 $[1, i]$ 的最大值,通过观察可以发现 $max_i$ 从后往前是非递减的,且每一次的递增位置都是出现了一个比先前所有数都大数,我们称这种递增位置为“变化位置”。

递增位置大致可以用下图表示($i$ 位置本身是第一个递增位置):

想一下,如果我们选择 2 号位置作为最后一段分割的最大值,那么上一次分割的位置会在哪儿?上一段的最后一个位置肯定是 3 号变化位置,因为 dp 值是非递减的。

同样的,如果选择 3 号位置作为最后一段分割的最大值,那么上一段的最后一个元素肯定选择 4 号变化位置。

也就是说计算 $i$ 位置的 dp 值,如果知道变化位置,我们只要枚举相邻两个变化位置,前一个的 dp 值加上后一个的 value 值,找到一个最小值就是 $dp_i$(注意最左边的变化位置可以认为是 $b_i-1$)。

可惜,最坏的情况下依旧是 $O(n^2)$ 的,由于 POJ 数据水了,上面这样的做法能水过去。

我们还是利用上述思路来求解答案,只不过增加一些优化。

事实上变化位置可以用单调队列维护出来,维护变化位置的同时,将前一个的 dp 值加上后一个的 value 值也维护出来就可以了,维护值的话可以用 set 或者线段树。

/*******************************
Judge Result : AC
*******************************/ #include <cstdio>
#include <set>
#include <algorithm>
#include <iostream>
using namespace std; const int maxn = 1e5 + 10;
const int INF = 0x7FFFFFFF; long long a[maxn], sum[maxn], f[maxn];
int b[maxn];
int n;
long long m; int q[maxn], first, last;
multiset<long long> st;
multiset<long long>::iterator it; void Insert(int x) {
last ++;
q[last] = x;
if(first == last) return;
st.insert(f[q[last - 1]] + a[q[last]]);
} void DeleteLast() {
if(first == last) {
last --;
return;
}
it = st.find(f[q[last - 1]] + a[q[last]]);
st.erase(it);
last --;
} void DeleteFirst() {
if(first == last) {
first ++;
return;
}
it = st.find(f[q[first]] + a[q[first + 1]]);
st.erase(it);
first ++;
} int main() {
#ifdef ZHOUZHENTAO
freopen("test.in", "r", stdin);
#endif scanf("%d%lld", &n, &m);
for(int i = 1; i <= n; i ++) {
scanf("%lld", &a[i]);
sum[i] = sum[i - 1] + a[i];
} for(int i = 1; i <= n; i ++) {
if(a[i] > m) {
printf("-1\n");
return 0;
}
int L = 1, R = i;
while(L <= R) {
int mid = (L + R) / 2;
if(sum[i] - sum[mid - 1] <= m) R = mid - 1, b[i] = mid;
else L = mid + 1;
}
} first = last = 0;
q[first] = 1;
f[1] = a[1]; for(int i = 2; i <= n; i ++) {
while(last - first + 1 > 0 && a[i] >= a[q[last]]) {
DeleteLast();
}
while(last - first + 1 > 0 && q[first] < b[i]) {
DeleteFirst();
}
Insert(i);
f[i] = f[b[i] - 1] + a[q[first]];
if(last - first + 1 > 1) {
f[i] = min(f[i], *st.begin());
}
}
printf("%lld\n", f[n]);
return 0;
}

POJ 3017 Cut the Sequence的更多相关文章

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

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

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

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

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

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

  4. 【以前的空间】Poj 3071 Cut the Sequence

    dp+单调性+平衡树 在看某篇论文中看到这道题,但是那篇论文不如这个http://www.cnblogs.com/staginner/archive/2012/04/02/2429850.html 大 ...

  5. 刷题总结——Cut the Sequence(POJ 3017 dp+单调队列+set)

    题目: Description Given an integer sequence { an } of length N, you are to cut the sequence into sever ...

  6. 【题解】Cut the Sequence(贪心区间覆盖)

    [题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...

  7. [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)

    DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...

  8. POJ3017 Cut the Sequence

    题意 Language:Default Cut the Sequence Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 122 ...

  9. POJ 3017 单调队列dp

    Cut the Sequence Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8764   Accepted: 2576 ...

随机推荐

  1. java.lang.UnsupportedClassVersionError: org/kie/api/KieServices$Factory : Unsupported major.minor version 52.0

    Unsupported major.minor version 52.0为JDK1.8编译器的版本,需要更换为JDK1.8的编译器与JDK1.8开发环境 idea中通过修改

  2. 转载:C++中两个类中互相包含对方对象的指针问题

    原文链接:http://www.cnblogs.com/hanxi/archive/2012/07/25/2608068.html 前几天很不爽,因为C++中两个类中互相包含对方对象的指针编译时提示某 ...

  3. Redis的持久化数据

    Redis支持两种持久化:RDB和AOF模式 一.名词解释: RDB:持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot).AOF:持久化记录服务器执行的 ...

  4. C++ map & set

    山东第六届ACM省赛B题 超时代码: #include<iostream> #include<cstdio> #include<string.h> #include ...

  5. 流媒体技术学习笔记之(六)FFmpeg官方文档先进音频编码(AAC)

    先进音频编码(AAC)的后继格式到MP3,和以MPEG-4部分3(ISO / IEC 14496-3)被定义.它通常用于MP4容器格式; 对于音乐,通常使用.m4a扩展名.第二最常见的用途是在MKV( ...

  6. shell 判断为空打印

    判断参数是否为空-空退出并打印null #!/bin/sh echo $ name=${:?"null"} echo $name

  7. expect 交互 之shell执行命令操作

    shell 执行命令操作 /usr/bin/expect -c " proc jiaohu {} { send_user expect_start expect { password { s ...

  8. Implement Queue by Two Stacks & Implement Stack using Queues

    Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...

  9. Interval Sum I && II

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

  10. DM816X 实现 USB HID Gadget 鼠标键盘功能【转】

    转自:https://my.oschina.net/renyongke/blog/410695 开发环境: 平台: DM8168 内核 :linux 2.6.32 RDK:DVRRDK_04.00.0 ...