描述

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[],qu[];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
sort(a+,a+n+);
int head=,tail=,ans=0x3f3f3f3f;
qu[]=a[++tail];
for(int i=;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?-:ans);
return ;
}

Flowerpot(单调队列)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. BestCoder Round #89 B题---Fxx and game(单调队列)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5945     问题描述 输入描述 输出描述 输入样例 输出样例 题意:中文题,不再赘述: 思路:  B ...

  8. 单调队列 && 斜率优化dp 专题

    首先得讲一下单调队列,顾名思义,单调队列就是队列中的每个元素具有单调性,如果是单调递增队列,那么每个元素都是单调递增的,反正,亦然. 那么如何对单调队列进行操作呢? 是这样的:对于单调队列而言,队首和 ...

  9. FZU 1914 单调队列

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...

随机推荐

  1. C-C++字符输出时遇到字符'\n','\0'区别

    #include "iostream" #include "stdio.h" #include "stdio_ext.h" #include ...

  2. freopen()函数

    freopen函数通过实现标准I/O重定向功能来访问文件,而fopen函数则通过文件I/O来访问文件. freopen函数在算法竞赛中常被使用.在算法竞赛中,参赛者的数据一般需要多次输入,而为避免重复 ...

  3. 【虚拟机-磁盘管理】理解及快速测定 Azure 虚拟机的磁盘性能

    随着越来越多的用户将生产系统迁移到 Azure 平台的虚拟机服务中,Azure 虚拟机的性能愈发被关注.传统的数据中心中,我们通常使用 CPU,内存,存储和网络的性能来衡量生产压力.特别是对于 IO ...

  4. window对象的几个重要方法

    <!DOCTYPE html><html><head><meta charset="UTF-8"><title>Java ...

  5. 利用jieba第三方库对文件进行关键字提取

    已经爬取到的斗破苍穹文本以TXT形式存储 代码 import jieba.analyse path = 'C:/Users/Administrator/Desktop/bishe/doupo.text ...

  6. mininet安装,使用

    http://mininet.org/download/ http://sdnhub.cn/index.php/mininet-walkthrough-chinese/ --------------- ...

  7. Java 集合框架_下

    Map接口 特点: [1]Map接口称为键值对集合或者映射集合,其中的元素(entry)是以键值对(key-value)的形式存在. [2]Map 容器接口中提供了增.删.改.查的方式对集合进行操作. ...

  8. [numpy] 基础练习 (一)

    Numpy常用总结 基础要打牢,恩. 基础 # 0 - 9 arr = np.arange(10) # 3*3 bool np.full((3,3),true,dtype = bool) np.one ...

  9. hd - MFM/IDE 硬盘设备

    描述 DESCRIPTION hd* 开头的设备是以裸模式(raw mode)访问MFM/IDE类型硬盘的块设备. 第一个IDE驱动控制器上的主盘(主设备号3)是 hda ;从盘是 hdb. 第二个I ...

  10. for...in、for...of、forEach()有什么区别

    本文原链接:https://cloud.tencent.com/developer/article/1360074 for of 和 for in 循环 循环遍历数组的时候,你还在用 for 语句走天 ...