单调队列+二分答案

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
struct Node{
int x, y;
}nd[100005];
int n, d, ans, q1[100005], q2[100005], l1, r1, l2, r2;
bool cmp(Node u, Node v){
return u.x<v.x;
}
bool check(int lim){
l1 = 1, r1 = 0;
l2 = 1, r2 = 0;
for(int i=1; i<=n; i++){
while(l1<=r1 && nd[q1[l1]].x<nd[i].x-lim) l1++;
while(l1<=r1 && nd[q1[r1]].y>nd[i].y) r1--;
q1[++r1] = i;
while(l2<=r2 && nd[q2[l2]].x<nd[i].x-lim) l2++;
while(l2<=r2 && nd[q2[r2]].y<nd[i].y) r2--;
q2[++r2] = i;
if(nd[q2[l2]].y-nd[q1[l1]].y>=d) return true;
}
return false;
}
int main(){
cin>>n>>d;
for(int i=1; i<=n; i++)
scanf("%d %d", &nd[i].x, &nd[i].y);
sort(nd+1, nd+1+n, cmp);
int l=0, r=nd[n].x-nd[1].x, mid;
while(l<=r){
mid = (l + r) >> 1;
if(check(mid)){
ans = mid;
r = mid - 1;
}
else l = mid + 1;
}
if(ans==0) cout<<"-1"<<endl;
else cout<<ans<<endl;
return 0;
}

luogu2698 [USACO12MAR]花盆Flowerpot的更多相关文章

  1. 洛谷P2698 [USACO12MAR]花盆Flowerpot

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

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

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

  3. [USACO12MAR] 花盆Flowerpot

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

  4. [P2698][USACO12MAR]花盆Flowerpot

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

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

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

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

    https://www.luogu.org/problemnew/show/P2698 警示 用数组写双端队列的话,记得le = 1, ri = 0:le<=ri表示队列非空 题意 求一个最小的 ...

  7. LuoguP2698 【[USACO12MAR]花盆Flowerpot】

    题目描述 首先我们简化一下题意: 要找一段区间[L,R],使区间[L,R]内元素最大值减最小值大于等于D. 做法: 首先很容易想到采用二分,分什么呢? 我们二分区间长度为mid 这个时候,检验就成为了 ...

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

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

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

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

随机推荐

  1. bjut193E 吃饭时的怪癖

    题目: http://bjutacm.openjudge.cn/lianxi/193E/ 思路: n的所有质因数之和等于phi(n) * n / 2, phi(n)为欧拉函数. 实现: #includ ...

  2. Linux命令-4类

    一.系统管理与维护   1. pwd:print working directory    打印工作目录   2. cd:  change directory    改变或进入路径       ● c ...

  3. git如何强制用远程分支更新本地

    git本地即使有修改如何强制更新: 本地有修改和提交,如何强制用远程的库更新本地.我尝试过用git pull -f,总是提示 You have not concluded your merge. (M ...

  4. Beginning Python Chapter 3 Notes

    变量(variable)是储存数据的实体,在Python中也被称为"名称"(name). 1.Python"名称"基本命名法则 1.1) "名称&qu ...

  5. [转载]—Health Check Reports Problem: Dependency$ p_timestamp mismatch for VALID objects (文档 ID 781959.1)

    Health Check Reports Problem: Dependency$ p_timestamp mismatch for VALID objects (文档 ID 781959.1) AP ...

  6. 用指针的方式实现,重写strrchr函数的功能

    char *strchrTest(char * ptr,char c); Action(){ char str[]={"thisisadog"}; char c='s'; lr_o ...

  7. 设置DataGridView单元格的文本对齐方式

    实现效果: 知识运用: DataGridViewCellStyle类的Alignment属性     //获取或设置DataGridView单元格内的单元格内容的位置 public DataGridV ...

  8. 2018.5.14 PHP基础学习

    1.使用PHP输出HTML 使用PHP输出一个表格,并且通过style标签改变字体 <!--思考与练习--> <style type="text/css"> ...

  9. a survey for RL

    • A finite set of states St summarizing the information the agent senses from the environment at eve ...

  10. centos7中文显示为小方块~~啊啊啊 求大佬们解答

    这个问题困扰我很久了,刚好前几天注册了博客园,就想问问大佬们是怎么解决中文显示小方块的? 我试了很多办法,包括但不限于修改i18n配置文件,locale.conf,添加中文字体库等等等... 但都没有 ...