警示

用数组写双端队列的话,记得le = 1, ri = 0;
le<=ri表示队列非空

题意

求一个最小的区间长度,使得区间中的最大值和最小值的差>=D.

思路

一开始二分加线段树强行做,多了一个log。用ST表可能会优秀。做到nlogn。
但是如果用单调队列的话,除去排序,就可以做到O(n)
具体来说,对于一个L,合法的最小的右区间若为R,那么L+1的最小合法右区间一定>=R.

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> /* ⊂_ヽ
  \\ Λ_Λ 来了老弟
   \('ㅅ')
    > ⌒ヽ
   /   へ\
   /  / \\
   レ ノ   ヽ_つ
  / /
  / /|
 ( (ヽ
 | |、\
 | 丿 \ ⌒)
 | |  ) /
'ノ )  Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
struct FastIO {
static const int S = 4e6;
int wpos;
char wbuf[S];
FastIO() : wpos() {}
inline int xchar() {
static char buf[S];
static int len = , pos = ;
if (pos == len)
pos = , len = fread(buf, , S, stdin);
if (pos == len) exit();
return buf[pos++];
}
inline int xuint() {
int c = xchar(), x = ;
while (c <= ) c = xchar();
for (; '' <= c && c <= ''; c = xchar()) x = x * + c - '';
return x;
}
inline int xint()
{
int s = , c = xchar(), x = ;
while (c <= ) c = xchar();
if (c == '-') s = -, c = xchar();
for (; '' <= c && c <= ''; c = xchar()) x = x * + c - '';
return x * s;
}
inline void xstring(char *s)
{
int c = xchar();
while (c <= ) c = xchar();
for (; c > ; c = xchar()) * s++ = c;
*s = ;
}
inline void wchar(int x)
{
if (wpos == S) fwrite(wbuf, , S, stdout), wpos = ;
wbuf[wpos++] = x;
}
inline void wint(int x)
{
if (x < ) wchar('-'), x = -x;
char s[];
int n = ;
while (x || !n) s[n++] = '' + x % , x /= ;
while (n--) wchar(s[n]);
wchar('\n');
}
inline void wstring(const char *s)
{
while (*s) wchar(*s++);
}
~FastIO()
{
if (wpos) fwrite(wbuf, , wpos, stdout), wpos = ;
}
} io;
inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/
const int maxn = ;
struct node{
int x,y;
}a[maxn];
bool cmp(node a,node b){
return a.x < b.x;
}
int dq1[maxn*],dq2[maxn*];
int main(){
int n,D;
scanf("%d%d", &n, &D);
rep(i, , n){
scanf("%d%d", &a[i].x, &a[i].y);
}
sort(a+, a++n, cmp);
int ans = inf; int l1=,r1=,l2=,r2=;
for(int i=,r=; i<=n; i++){
while(l1 <= r1 && dq1[l1] < i) l1++;
while(l2 <= r2 && dq2[l2] < i) l2++; while(a[dq1[l1]].y - a[dq2[l2]].y < D && r < n){
r++;
while(a[dq1[r1]].y <= a[r].y && l1 <= r1) r1--; dq1[++r1] = r;
while(a[dq2[r2]].y >= a[r].y && l2 <= r2) r2--; dq2[++r2] = r;
} if( l1 <= r1 && l2 <= r2 && a[dq1[l1]].y - a[dq2[l2]].y >= D) ans = min(ans, a[r].x - a[i].x);
}
if(ans >= inf) puts("-1");
else
printf("%d\n", ans);
return ;
}

P2698 [USACO12MAR]花盆Flowerpot 单调队列的更多相关文章

  1. P2698 [USACO12MAR]花盆Flowerpot——单调队列

    记录每天看(抄)题解的日常: https://www.luogu.org/problem/P2698 我们可以把坐标按照x递增的顺序排个序,这样我们就只剩下纵坐标了: 如果横坐标(l,r)区间,纵坐标 ...

  2. [USACO12MAR]花盆Flowerpot (单调队列,二分答案)

    题目链接 Solution 转化一下,就是个单调队列. 可以发现就是一段区间 \([L,R]\) 使得其高度的极差不小于 \(d\) ,同时满足 \(R-L\) 最小. 然后可以考虑二分然后再 \(O ...

  3. luogu 2698 [USACO12MAR]花盆Flowerpot 单调队列

    刷水~ Code: #include<bits/stdc++.h> using namespace std; #define setIO(s) freopen(s".in&quo ...

  4. P2698 [USACO12MAR]花盆Flowerpot(单调队列+二分)

    P2698 [USACO12MAR]花盆Flowerpot 一看标签........十分后悔 标签告诉你单调队列+二分了............ 每次二分花盆长度,蓝后开2个单调队列维护最大最小值 蓝 ...

  5. 洛谷P2698 [USACO12MAR]花盆Flowerpot

    P2698 [USACO12MAR]花盆Flowerpot 题目描述 Farmer John has been having trouble making his plants grow, and n ...

  6. [USACO12MAR]花盆 二分 单调队列

    [USACO12MAR]花盆 二分 单调队列 存在一个长度为\(x\)的区间\([l,r]\),使得区间中最大值与最小值差至少为\(w\),求这个最小的\(x\) \(n\le 100000\),\( ...

  7. [P2698][USACO12MAR]花盆Flowerpot

    Link: P2698 传送门 Solution: 对于可行区间$[L,R]$,随着$L$的递增$R$不会递减 因此可以使用尺取法来解决此题:不断向右移动左右指针,复杂度保持线性 同时为了维护区间内的 ...

  8. [USACO12MAR] 花盆Flowerpot

    类型:二分+单调队列 传送门:>Here< 题意:给出$N$个点的坐标,要求根据$x$轴选定一段区间$[L,R]$,使得其中的点的最大与最小的$y$值之差$\geq D$.求$Min\{R ...

  9. luogu2698 [USACO12MAR]花盆Flowerpot

    单调队列+二分答案 #include <algorithm> #include <iostream> #include <cstring> #include < ...

随机推荐

  1. http状态码 400-499

    类比 服务器:便利店 客户端:客人 http报文:中文语言+钱 400-499 客户的错误 400 :服务器不理解客服端请求的意思是什么,如请求报文损坏 举例: 客户端:@#!3&* 服务器: ...

  2. Android 开发使用自定义字体

    有时候,系统自带的字体并不能满足我们特殊的需求,这时候就需要引用其他的字体了,可以把下载的字体文件放在 assets 目录下. 自定义字体文件不能使用xml代码读取而应该使用java代码: publi ...

  3. 数据类型之Integer与int

    数据类型之Integer与int Java入门  基本数据类型 众所周知,Java是面向对象的语言,一切皆对象.但是为了兼容人类根深蒂固的数据处理习惯,加快常规数据的处理速度,提供了9种基本数据类型, ...

  4. Filebeat6.3文档—Log input配置

    Filebeat6.3文档-Log input配置 paths 日志加载的路径.例如加载某一子目录级别下面路径的日志:/var/log/*/*.log.这表示会去加载以.log结尾的/var/log下 ...

  5. WPF ContextMenu+VisualTreeHelper实现删除控件操作

    <UserControl  MouseRightButtonDown="UserControl_MouseRightButtonDown" >    <UserC ...

  6. 微信JSSDK签名

    微信JS-SDK说明文档 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115 生成签名 1.签名规则 参与签名的 ...

  7. 【POJ - 3176】牛保龄球 (简单dp)

    牛保龄球 直接中文了 Descriptions 奶牛打保龄球时不使用实际的保龄球.它们各自取一个数字(在0..99范围内),然后排成一个标准的保龄球状三角形,如下所示: 7 3 8 8 1 0 2 7 ...

  8. css实现左边高度自适应右边高度

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  9. nodeJs跨域设置(express,koa2,eggJs)

    原生跨域 var http=require('http'); var server = http.createServer(function (req,res) { res.setHeader('Ac ...

  10. studio无限轮播

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...