题面:

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 (单调队列)的更多相关文章

  1. CodeForces - 91B单调队列

    有一个数列,对于每一个数,求比它小的在他右边距离他最远的那个数和他的距离 用单调队列做,维护单调队列时可采用如下方法,对于每一个数,如果队列中没有数,则加入队列,如果队列头的数比当前数大,则舍弃该数 ...

  2. codeforces 939F 单调队列优化dp

    F. Cutlet time limit per test 4 seconds memory limit per test 256 megabytes input standard input out ...

  3. Codeforces 487B Strip (ST表+线段树维护DP 或 单调队列优化DP)

    题目链接 Strip 题意   把一个数列分成连续的$k$段,要求满足每一段内的元素最大值和最小值的差值不超过$s$, 同时每一段内的元素个数要大于等于$l$, 求$k$的最小值. 考虑$DP$ 设$ ...

  4. Codeforces Round #278 (Div. 1) B - Strip dp+st表+单调队列

    B - Strip 思路:简单dp,用st表+单调队列维护一下. #include<bits/stdc++.h> #define LL long long #define fi first ...

  5. CodeForces 164 B. Ancient Berland Hieroglyphs 单调队列

    B. Ancient Berland Hieroglyphs 题目连接: http://codeforces.com/problemset/problem/164/B Descriptionww.co ...

  6. 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 ...

  7. Codeforces Beta Round #6 (Div. 2 Only) 单调队列

    题目链接: http://codeforces.com/contest/6/problem/E E. Exposition time limit per test 1.5 secondsmemory ...

  8. Codeforces 445A Boredom(DP+单调队列优化)

    题目链接:http://codeforces.com/problemset/problem/455/A 题目大意:有n个数,每次可以选择删除一个值为x的数,然后值为x-1,x+1的数也都会被删除,你可 ...

  9. Codeforces 1029B. Creating the Contest 动态规划O(nlogn)解法 及 单调队列O(n)解法

    题目链接:http://codeforces.com/problemset/problem/1029/B 题目大意:从数组a中选出一些数组成数组b,要求 b[i+1]<=b[i]*2 . 一开始 ...

随机推荐

  1. js 数组操作

    toString():把数组转换成一个字符串 toLocaleString():把数组转换成一个字符串 join():把数组转换成一个用符号连接的字符串 shift():将数组头部的一个元素移出 un ...

  2. Linux中Postfix邮件安装Maildrop(八)

    Postfix使用maildrop投递邮件 Maildrop是本地邮件投递代理(MDA), 支持过滤(/etc/maildroprc).投递和磁盘限额(Quota)功能. Maildrop是一个使用C ...

  3. iOS原生的AVFoundation扫描二维码/条形码

    #import <AVFoundation/AVFoundation.h> @interface ViewController ()<AVCaptureMetadataOutputO ...

  4. 实用的4~20mA输入/0~5V输出的I/V转换电路(转)

    源: 实用的4~20mA输入/0~5V输出的I/V转换电路

  5. 为自己的网站添加Markdown功能 markedjs

    Markdown几个简单的标记可以实现轻量级的代替Word方案 不多说,引入开源库js https://github.com/chjj/marked使用方式简单,如下实例代码: <!DOCTYP ...

  6. Java实现Sybase数据库连接

    Java实现Sybase数据库连接 需要的jar包:jconn4.jar: Java代码: /** * @Title: getConnSybase * @Description: * @param * ...

  7. [省选模拟]Rhyme

    考的时候脑子各种短路,用个SAM瞎搞了半天没搞出来,最后中午火急火燎的打了个SPFA才混了点分. 其实这个可以把每个模式串长度为$K-1$的字符串看作一个状态,这个用字符串Hash实现,然后我们发现这 ...

  8. 20145333茹翔 Exp5 利用nmap扫描

    20145333茹翔 Exp5 利用nmap扫描 实验过程 首先使用命令创建一个msf所需的数据库 service postgresql start msfdb start 使用命令msfconsol ...

  9. BIOS、MBR、UEFI和GPT关系

    很多用户在新买电脑,或是给已有电脑重装系统时都出现过怎么都无法引导U盘安装的情况.究其原因,还是没能搞清楚BIOS.MBR.UEFI和GPT的复杂关系.所以,今天小编就和大家分享一下它们之间的爱恨情仇 ...

  10. redhat7 防火墙设置

    查看防火墙的状态# firewall-cmd --staterunning # systemctl stop firewalld   //关闭防火墙服务# systemctl start firewa ...