Strip CodeForces - 487B (单调队列)
题面:
Alexandra has a paper strip with n numbers on it. Let's call them ai from left to right.
Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy:
- Each piece should contain at least l numbers.
- The difference between the maximal and the minimal number on the piece should be at most s.
Please help Alexandra to find the minimal number of pieces meeting the condition above.
一个显然的思路是ST表求一下最值, 由于最值单调性可以双指针处理出以每个数为右端点时, 左端点的最小值, 然后再dp就行了, 但这样复杂度是$O(nlogn)$的. 若用单调队列处理的话可以达到$O(n)$的, 单调队列还是不太会写啊, 写了1个多小时才A
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std; const int N = 1e6+10, INF = 0x3f3f3f3f;
int n, s, l;
int a[N], dp[N], L[N]; int main() {
scanf("%d%d%d", &n, &s, &l);
REP(i,1,n) scanf("%d", a+i);
deque<int> q;
int pos = 1;
REP(i,1,n) {
while (q.size()&&a[i]-a[q.front()]>s) pos=q.front()+1,q.pop_front();
L[i] = pos;
while (q.size()&&a[i]<a[q.back()]) q.pop_back();
q.push_back(i);
}
pos = 1, q.clear();
REP(i,1,n) {
while (q.size()&&a[q.front()]-a[i]>s) pos=q.front()+1,q.pop_front();
L[i] = max(L[i], pos);
while (q.size()&&a[i]>a[q.back()]) q.pop_back();
q.push_back(i);
}
REP(i,1,n) dp[i]=INF;
q.clear();
q.push_back(0);
REP(i,1,n) {
while (q.size()&&q.front()<L[i]-1) q.pop_front();
if (q.size()&&q.front()<=i-l) dp[i]=dp[q.front()]+1;
while (q.size()&&dp[i]<dp[q.back()]) q.pop_back();
q.push_back(i);
}
printf("%d\n", dp[n]>=INF?-1:dp[n]);
}
Strip CodeForces - 487B (单调队列)的更多相关文章
- CodeForces - 91B单调队列
有一个数列,对于每一个数,求比它小的在他右边距离他最远的那个数和他的距离 用单调队列做,维护单调队列时可采用如下方法,对于每一个数,如果队列中没有数,则加入队列,如果队列头的数比当前数大,则舍弃该数 ...
- codeforces 939F 单调队列优化dp
F. Cutlet time limit per test 4 seconds memory limit per test 256 megabytes input standard input out ...
- Codeforces 487B Strip (ST表+线段树维护DP 或 单调队列优化DP)
题目链接 Strip 题意 把一个数列分成连续的$k$段,要求满足每一段内的元素最大值和最小值的差值不超过$s$, 同时每一段内的元素个数要大于等于$l$, 求$k$的最小值. 考虑$DP$ 设$ ...
- Codeforces Round #278 (Div. 1) B - Strip dp+st表+单调队列
B - Strip 思路:简单dp,用st表+单调队列维护一下. #include<bits/stdc++.h> #define LL long long #define fi first ...
- CodeForces 164 B. Ancient Berland Hieroglyphs 单调队列
B. Ancient Berland Hieroglyphs 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co ...
- Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列
B. Psychos in a Line Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/p ...
- Codeforces Beta Round #6 (Div. 2 Only) 单调队列
题目链接: http://codeforces.com/contest/6/problem/E E. Exposition time limit per test 1.5 secondsmemory ...
- Codeforces 445A Boredom(DP+单调队列优化)
题目链接:http://codeforces.com/problemset/problem/455/A 题目大意:有n个数,每次可以选择删除一个值为x的数,然后值为x-1,x+1的数也都会被删除,你可 ...
- Codeforces 1029B. Creating the Contest 动态规划O(nlogn)解法 及 单调队列O(n)解法
题目链接:http://codeforces.com/problemset/problem/1029/B 题目大意:从数组a中选出一些数组成数组b,要求 b[i+1]<=b[i]*2 . 一开始 ...
随机推荐
- linux常用命令:chkconfig 命令
chkconfig命令用来安装,查看或修改 services随系统启动的启动选项的设置.是Red Hat公司遵循GPL规则所开发的程序,它可查询操作系统在每一个执行等级中会执行哪些系统服务,其中包括各 ...
- php检查是否是数字和字母
/* 检查是否是数字和字母* php内置函数ctype_alnum检查字符串是否是数字和字母,或者两者混合* $string*/ public function is_numandlitter($st ...
- label语句、break语句和continue语句
label语句 可以在代码中添加标签,以便使用.以下是label语句的语法: label:statement 示例: start: for (var i = 0; i < count; i++) ...
- Lombok让pojo变得更优雅
Lombok 采取注解的形式,标记在pojo上面,在编译后,自动生成相应的方法,像get.set.构造方法等都可以注解一键生成. 引入jar包: <dependency> <grou ...
- shell分析http日志
http状态码1字头----信息,服务器收到请求,需要请求者继续执行操作2字头----成功,操作被成功接收并处理3字头----重定向,需要进一步的操作以完成请求4字头----客户端错误,请求包含语法错 ...
- SIFT在OpenCV中的调用和具体实现(HELU版)
前面我们对sift算法的流程进行简要研究,那么在OpenCV中,sift是如何被调用的?又是如何被实现出来的了? 特别是到了3.0以后,OpenCV对特征点提取这个方面进行了系统重构,那么整个代码结构 ...
- 三步搞定 opencv 初始环境设定
一.设定bin的初始位置:比如我的电脑 D:\安装程序\opencv\build\x86\vc10\bin H:\生产力工具\opencv\build\x86\vc10\bin D:\安装程 ...
- 20145332 《网络攻防》 逆向与Bof实验
20145332 <网络攻防>逆向与Bof实验 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用 ...
- <OFFER05> 05_ReplaceSpaces
void ReplaceBlank(char str[], int length) // length >= the real length of string { ) { return; } ...
- POJ 2352 Stars(树状数组)题解
Language:Default Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 52268 Accepted: 22 ...