题面:

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. QPropertyAnimation 几行代码快速制作流畅的动画效果

    QPropertyAnimation Class 官方英文文档[点击前往] QPropertyAnimation Class 中文译文文档[点击前往]   简介 QPropertyAnimation ...

  2. MySQL数据库----IDE工具介绍及数据备份

    一.IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 下载链接:https://pan.baidu.com/s/1bpo5mqj 二.MySQL数据备份 # ...

  3. tomcat应用org.apache.catalina.LifecycleException: Failed to stop component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]异常的根本原因

    早上,有个应用又挂了,客户端打开时报404,看进程是还在的.倒回第一次异常的地方,可见catalina.out中有如下信息: 08:46:56.646 [ContainerBackgroundProc ...

  4. html/jquery最实用功能与注意点

    获取某元素的父元素 通常用在根据被click的td获取tbody时. parent是指取得一个包含着所有匹配元素的唯一父元素的元素集合.parents则是取得一个包含着所有匹配元素的祖先元素的元素集合 ...

  5. PHP安装包TS和NTS的区别

    原文链接:http://blog.csdn.net/zhuifengshenku/article/details/38796555 TS指Thread Safety,即线程安全,一般在IIS以ISAP ...

  6. stm32时钟树讲解

    1.管理好时钟,功耗才能更低

  7. 'telnet'不是内部或外部命令,怎么办?

    ['telnet'不是内部或外部命令,也不是可运行的程序或批处理文件]当你想用telnet命令时,发现提示这句话怎么办?其实很简单,接下来为大家介绍下如何使用 1. 一般只有windows7才会出现这 ...

  8. 按时间间隔生成cron表达式

    cron表达式是使用任务调度经常使用的表达式了.对于通常的简单任务,我们只需要一条cron表达式就能满足.但是有的时候任务也可以很复杂. 最近我遇到了一个问题,一条任务在开始的时候要触发A方法,在结束 ...

  9. python程序转为exe文件

    python开发者向普通windows用户分享程序,要给程序加图形化的界面(传送门:这可能是最好玩的python GUI入门实例! http://www.jianshu.com/p/8abcf73ad ...

  10. zedgraph右键菜单的汉化

    http://blog.csdn.net/jeryler/article/details/7876376 修改 zedGraphControl的ContextMenuBuilder事件即可! zedG ...