警示

用数组写双端队列的话,记得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. python 读取文件1

    1.脚本 from sys import argv script,filename = argv txt = open(filename) print ("the filename is % ...

  2. R语言学习笔记——C#中如何使用R语言setwd()函数

    在R语言编译器中,设置当前工作文件夹可以用setwd()函数. > setwd("e://桌面//")> setwd("e:\桌面\")> s ...

  3. 在 alpine 中使用 NPOI

    在 alpine 中使用 NPOI Intro 在 .net 中常使用 NPOI 来做 Excel 的导入导出,NPOI 从 2.4.0 版本开始支持 .netstandard2.0,对于.net c ...

  4. 再记一次经典Net程序的逆向过程

    1.前言 上次发完,有网友问了一个问题:如果不绕过编译,而是直接编译怎么办? 记一次Net软件逆向的过程:https://www.cnblogs.com/dotnetcrazy/p/10142315. ...

  5. Codis与RedisCluster的原理详解

    背景介绍 我们先来看一下为什么要做集群,如果我们要部署一个单节点Redis,很明显会遇到单点故障的问题. 首先能想到解决单点故障的方法,就是做主从,但是当有海量存储需求时,单一的主从结构就会出问题,说 ...

  6. EnjoyingSoft之Mule ESB开发教程第六篇:Data Transform - 数据转换

    目录 1. 数据转换概念 2. 数据智能感知 - DataSense 3. 简单数据转换组件 3.1 Object to JSON 3.2 JSON to XML 3.3 JSON to Object ...

  7. maven 打包并导出 lib 第三方jar

    一. maven 导出lib 包 执行命令 mvn dependency:copy-dependencies -DoutputDirectory=target/lib 或者在 eclipse 中执行, ...

  8. [实践]activemq安全设置 设置admin的用户名和密码

    (1)打开/opt/app/amq/apache-activemq-5.9.0/conf/jetty.xml 找到 将property name为authenticate的属性value=" ...

  9. centOS 如何查看知道自己的版本号

    今天遇到一个尴尬的问题 , 竟然找不到centOS7x这个版本系统 然后我就问大佬们,大佬们1810 是哪哪哪个版本说的我还是懵逼 然后我就发挥我那不要脸的精神 问:'这是有什算发算的吗'  很是尴尬 ...

  10. Window.open使用总结

    前言 今天在项目中,突然看到window.open的使用,感觉还是很神奇,突然心血来潮查看了window.open的用法. 用途 主要用于在打开网站时弹出的其他窗口.用于通知广告一类的. 用法 win ...