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 安装软件的几种方法
一. 解析Linux应用软件安装包: 通常Linux应用软件的安装包有三种: 1) tar包,如software-1.2.3-1.tar.gz.它是使用UNIX系统的打包工具tar打包的. 2) rp ...
- linux常用命令:chown 命令
chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷贝 ...
- python之路----hashlib模块
在平时生活中,有很多情况下,你在不知不觉中,就用到了hashlib模块,比如:注册和登录认证注册和登录认真过程,就是把注册用的账户密码进行:加密 --> 解密 的过程,在加密.解密过程中,用的了 ...
- Contiki源码+原理+功能+编程+移植+驱动+网络(转)
源:Contiki源码+原理+功能+编程+移植+驱动+网络 请链接:http://www.rimelink.com/nd.jsp? id=31&_np=105_315 假设您对于用Contik ...
- ORM 关系对象映射 基础知识点
优点: 1.ORM使我们通用的数据库变得更加的简单便捷. 2.可避免新手程序员写sql语句带来的性能问题. 1. 创建单表 2. 创建关键表 1). 一对一 2). 一对多 3). 多对多 创建表的语 ...
- 20145307陈俊达_安卓逆向分析_Xposed的hook技术研究
20145307陈俊达_安卓逆向分析_Xposed的hook技术研究 引言 其实这份我早就想写了,xposed这个东西我在安卓SDK 4.4.4的时候就在玩了,root后安装架构,起初是为了实现一些屌 ...
- IDEA中使用.ignore插件忽略不必要提交的文件
使用的IDE是IntelliJ IDEA,发现IDEA在提交项目到本地仓库的时候,会把.idea文件夹中的内容也提交上去,这里面放的是一些项目的配置信息,包括历史记录,版本控制信息等.可以不传到Git ...
- python的回收机制
1,我们为什么要启用变量 因为我也不确定用户扔给我的数据是什么,有些同学在写程序的时候就已经将变量限定死了,我就说你这样不好,你只想测试一次吗,如果你想确保你的程序万无一失,通俗点说的话,就是“抗揍” ...
- git如何生成指定两个commit之间的补丁
答:git format-patch <base commit id>..<latest commit id> 如git log输出以下内容: commit 2222222 y ...
- linux下去掉pdf的密码(前提:知道密码)
一.背景 Linux jello 4.16.3 SMP Thu Apr 19 07:32:02 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux 二.去掉密码 2.1 先 ...