题目描述

Farmer John has been having trouble making his plants grow, and needs your help to water them properly. You are given the locations of N raindrops (1 <= N <= 100,000) in the 2D plane, where y represents vertical height of the drop, and x represents its location over a 1D number line:

;

Each drop falls downward (towards the x axis) at a rate of 1 unit per second. You would like to place Farmer John's flowerpot of width W somewhere along the x axis so that the difference in time between the first raindrop to hit the flowerpot and the last raindrop to hit the flowerpot is at least some amount D (so that the flowers in the pot receive plenty of water). A drop of water that lands just on the edge of the flowerpot counts as hitting the flowerpot.

Given the value of D and the locations of the N raindrops, please compute the minimum possible value of W.

老板需要你帮忙浇花。给出N滴水的坐标,y表示水滴的高度,x表示它下落到x轴的位置。

每滴水以每秒1个单位长度的速度下落。你需要把花盆放在x轴上的某个位置,使得从被花盆接着的第1滴水开始,到被花盆接着的最后1滴水结束,之间的时间差至少为D。

我们认为,只要水滴落到x轴上,与花盆的边沿对齐,就认为被接住。给出N滴水的坐标和D的大小,请算出最小的花盆的宽度W。

输入输出格式

输入格式:

第一行2个整数 N 和 D。

第2.. N+1行每行2个整数,表示水滴的坐标(x,y)。

输出格式:

仅一行1个整数,表示最小的花盆的宽度。如果无法构造出足够宽的花盆,使得在D单位的时间接住满足要求的水滴,则输出-1。

输入输出样例

输入样例#1:

4 5
6 3
2 4
4 10
12 15

输出样例#1:

2

说明

【样例解释】

有4滴水, (6,3), (2,4), (4,10), (12,15).水滴必须用至少5秒时间落入花盆。花盆的宽度为2是必须且足够的。把花盆放在x=4..6的位置,它可以接到1和3水滴, 之间的时间差为10-3 = 7满足条件。

【数据范围】

40%的数据:1 ≤ N ≤ 1000,1 ≤ D ≤ 2000;

100%的数据:1 ≤ N ≤ 100000,1 ≤ D ≤ 1000000,0≤x,y≤106。

单调队列。

枚举左端点,向后扫右端点,同时用两个单调队列来维护区间中y的最大最小值,直到y满足条件为止。

——BY 牌爷 chaijing

#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
const int N=100005;
int n,d,mxy,mny,ans=1e9+7,lef[N],q_min[N],q_max[N];
struct node
{int x,y;
}a[N];
bool cmp(node c,node d)
{return c.x<d.x;
}
int main()
{scanf("%d%d",&n,&d);mny=1e9+7;for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y),mxy=max(mxy,a[i].y),mny=min(mny,a[i].y);sort(a+1,a+n+1,cmp);if(mxy-mny<d){printf("-1\n");return 0;}int h_min=1,t_min=0,h_max=1,t_max=0,j=0;for(int i=1;i<=n;i++){while(a[q_max[h_max]].y-a[q_min[h_min]].y<d){j++;if(j>n){printf("%d\n",ans);return 0;}while(h_max<=t_max&&a[j].y>=a[q_max[t_max]].y)t_max--;q_max[++t_max]=j;while(h_min<=t_min&&a[j].y<=a[q_min[t_min]].y)t_min--;q_min[++t_min]=j;}ans=min(ans,a[j].x-a[i].x);if(q_min[h_min]==i)h_min++;if(q_max[h_max]==i)h_max++;}printf("%d\n",ans);return 0;
}

洛谷 P2698 [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

    洛谷题目链接:[USACO12MAR]花盆Flowerpot 题目描述 Farmer John has been having trouble making his plants grow, and ...

  3. 洛谷 P2698 [USACO12MAR]花盆Flowerpot 单调队列

    https://www.luogu.org/problemnew/show/P2698 题意中文的不说了: 做法:就是一个滑动区间维护最大值和最小值,首先,了解一条性质,对于满足要求的两个区间 (l1 ...

  4. [洛谷P2698][USACO12MAR]花盆Flowerpot

    题目大意:$n$个坐标和时间.需要找到最小的一段区间使得这一段区间最大时间减去最小时间的差大于$d$ 题解:发现对于较优的区间$[l_i,r_i]$(即对于这个左端点,$r_i$是第一个符合条件的), ...

  5. #单调队列#洛谷 2698 [USACO12MAR]花盆Flowerpot

    题目 给出N滴水的坐标(X,Y),y表示水滴的高度,x表示它下落到x轴的位置.每滴水每秒从(x,y)到(x,y-1).你需要把花盆放在x轴上的某个位置,使得从开始接水到水滴完之间的时间差至少为D,只要 ...

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

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

  7. 洛谷P2698 [USACO12MAR]Flowerpot S

    P2698 [USACO12MAR]Flowerpot S - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 题意: 思路: 宽度太小,第一滴和最后一滴的时间差只可能会小,宽度太大,时 ...

  8. luogu P2698 [USACO12MAR]花盆Flowerpot

    背景: 集训Day3Day3Day3,单调队列开启. 其实原来就会,但是好像没做几题(除了斜率优化的). 打算用111至222天做一些题(太菜,大佬勿喷). rank=20000+rank=20000 ...

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

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

最新文章

  1. UML类图新手入门级介绍
  2. Spring Bean 中的线程安全
  3. C#中结构数据类型的使用
  4. asp.net mvc4 配置数据库连接的相关问题
  5. 课时 29:安全容器技术(王旭)
  6. 20_Android中apk安装器,通过WebView来load进一个页面,Android通知,程序退出自动杀死进程,通过输入包名的方式杀死进程
  7. Git下载与使用(Git地址由CSDN提供)
  8. 对于自绝对父相的理解
  9. 在缺乏贷后数据的情况下做好策略的调整把控
  10. DeFi 中的 De 是什么意思?这对区块链行业意味着什么?
  11. 《Modern Python Cookbook》(Python经典实例)笔记 1.13 使用元组
  12. 机器学习中的 Shapley 值怎么理解?
  13. 2020腾讯广告算法大赛——算法小白的复盘
  14. win系统连接交换机并设置固定ip地址
  15. 吴恩达机器学习18-应用实例:图片文字识别
  16. 创新实训(9)——SpringBoot整合solr
  17. CentOS7-安装防火墙
  18. Xilinx Zynq-7000嵌入式系统设计与实现 学习教程(1)
  19. (十四)从零开始学人工智能-深度学习基础及CNN
  20. acm-(好题、神题)2020-2021 Winter Petrozavodsk Camp, Day 5 B.Lockout vs tourist

热门文章

  1. FLASH使用技术提示
  2. c语言开发窗口程序,怎么用c语言做一个界面?
  3. shell下 php脚本,shell脚本--php执行普通shell命令
  4. qme---模拟安装arm--debian安装到开发板
  5. 你真的理解【函数式编程】吗?
  6. 判断一个数是否是 2、3、4的幂次方
  7. 麦田怪圈集合欣赏!(高清晰图)
  8. 【华人学者风采】何田 京东
  9. 如何快速有效的学习新领域知识
  10. 如何把文字转换成语音,这里给你答案