描述

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.

输入

* Line 1: Two space-separated integers, N and D. (1 <= D <= 1,000,000)

* Lines 2..1+N: Line i+1 contains the space-separated (x,y) coordinates of raindrop i, each value in the range 0...1,000,000.

输出

* Line 1: A single integer, giving the minimum possible width of the flowerpot. Output -1 if it is not possible to build a flowerpot wide enough to capture rain for at least D units of time.

样例输入

4 5
6 3
2 4
4 10
12 15

样例输出

2

提示

INPUT DETAILS:

There are 4 raindrops, at (6,3), (2,4), (4,10), and (12,15). Rain must fall on the flowerpot for at least 5 units of time.

OUTPUT DETAILS:

A flowerpot of width 2 is necessary and sufficient, since if we place it from x=4..6, then it captures raindrops #1 and #3, for a total rain duration of 10-3 = 7.

题目大意:

给定n个水滴的坐标,每滴下落速度每秒1个单位,在x轴放一个花盆,使第一个落在花盆和最后一个落在花盆的时间差大于D,求符合的最小花盆宽度,若不符合就输出-1。

先按x排序,然后用单调队列维护。

#include <bits/stdc++.h>
using namespace std;
struct point
{int x,y;bool operator<(const point &tmp)const{return x<tmp.x;}
}a[100005],qu[100005];
int main()
{int n,m;scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y);sort(a+1,a+n+1);int head=1,tail=0,ans=0x3f3f3f3f;qu[1]=a[++tail];for(int i=2;i<=n;i++){while(tail>=head&&qu[tail].y>a[i].y){if(qu[tail].y-a[i].y>=m) ans=min(ans,a[i].x-qu[tail].x);tail--;}qu[++tail]=a[i];while(tail>=head&&qu[tail].y-qu[head].y>=m)ans=min(ans,qu[tail].x-qu[head].x),head++;}printf("%d\n",ans==0x3f3f3f3f?-1:ans);return 0;
}

转载于:https://www.cnblogs.com/zdragon1104/p/9561178.html

Flowerpot(单调队列)相关推荐

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

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

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

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

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

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

  4. 洛谷P2698 花盆Flowerpot【单调队列】

    题目描述 Farmer John has been having trouble making his plants grow, and needs your help to water them p ...

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

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

  6. P2698-花盆Flowerpot【单调队列】

    正题 链接 https://www.luogu.org/record/show?rid=7934370 大意 有n滴水,给出坐标,水每一个时间单位会往下掉一格,花盆可以随意摆放,要求在宽度最小的情况下 ...

  7. [USACO12MAR]花盆Flowerpot 二分答案+单调队列

    题意: 给出N滴水的坐标,y表示水滴的高度,x表示它下落到x轴的位置. 每滴水以每秒1个单位长度的速度下落.你需要把花盆放在x轴上的某个位置,使得从被花盆接着的第1滴水开始,到被花盆接着的最后1滴水结 ...

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

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

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

    传送门 题意:直接看题意即可 题解:首先可以通过二分枚举区间大小,然后通过单调队列求出每个区间的最大值和最小值进行比较,最后得出答案即可. 附上代码: #include<bits/stdc++. ...

最新文章

  1. Apache FileUpload介绍
  2. 学习ribbon,进来看看吧
  3. 最新翻译的官方PyTorch简易入门教程(PyTorch1.0版本)
  4. vcruntime140.dll 丢失64位系统(mysql8安装失败提示)
  5. 为什么计算机硬盘要从c盘开始,电脑分区为何从C盘开始?英特尔科普
  6. C# 读取EXCEL文件的三种经典方法
  7. ofstream 向文件写数据
  8. python os 常用方法_【Python 库】os 模块常用方法简介
  9. Thrift IDL使用方式
  10. 2022安徽合肥经济技术开发区招聘社区工作者冲刺试题及答案
  11. Tensorflow 释放内存
  12. Photoshop教程_ps中怎么载入图案?PS图案如何导入?
  13. IT培训班有用吗?IT培训包就业是真的吗?
  14. vue 在线访问word,excel,pdf 文件以及打印
  15. 计算机储存文档丢失怎么找,电脑文件丢失如何找回,文件数据丢失恢复方法
  16. DOM JDOM DOM4J
  17. python pandas excel 排序_python – Pandas – 使用datetimeindex对数据帧进行排序
  18. 如何检查房间里隐藏的摄像机 六种方法解决你的烦恼
  19. 用python制作飞机大战_Python制作AI且mini版飞机大战
  20. jaeger php,全链路监控Jaeger搭建实战

热门文章

  1. sourcetree神操作
  2. python编程计算器_Python设计实现的计算器功能完整实例
  3. python0表示剪刀_简化Python代码(石头、纸、剪刀)
  4. 《趣学Python——教孩子学编程》——第1部分 学习编程 第1章 Python不是大蟒蛇 1.1 关于计算机语言...
  5. Linux chromium弹出your preferences can not be read
  6. 用键盘打开计算机管理,windows10系统使用键盘打开设备管理器的三种方法
  7. 有道少儿词典正式上线,CEO周枫发朋友圈:“是时候让小学生词典进入互联网时代了”...
  8. 微软小娜关闭服务器,请问微软小娜 (Cortana) 要退出中国了吗?
  9. PHP开源 | ysKit(ys工具包) - 微型Web框架
  10. service mysql k8s_Kubernetes/K8S基础使用方法总结【五】——Service