描述

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. 小白学phoneGap《构建跨平台APP:phoneGap移动应用实战》连载三(通过实例来体验生命周期)

    4.1.2  通过实例来亲身体验Activity的生命周期 上一小节介绍了Activity生命周期中的各个过程,本小节将以一个简单的实例来使读者亲身体验到Activity生命周期中的各个事件. 在Ec ...

  2. Android(java)学习笔记125:保存数据到SD卡 (附加:保存数据到内存)

    1. 如果我们要想读写数据到SD卡中,首先必须知道SD的路径: File file = new File(Environment.getExternalStorageDirectory()," ...

  3. FreeRTOS笔记

    任务的创建和删除(静态方法) 任务创建后要开启调度器. FreeRTOSConfig.h 1. 改宏 使能静态创建函数. 会出现,有两个函数未定义. Cortex-M中断管理(上) NVIC:嵌套向量 ...

  4. Problem A: 文件操作--二进制文件读入

    Problem A: 文件操作--二进制文件读入 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1952  Solved: 524[Submit][St ...

  5. Python——函数入门(一)

    一.理解函数 举一个例子,当我们需要重复使用一个功能的时候,不可能每次都去复制一次代码,这个时候就需要用到函数了,所谓的函数,简单来说就是给函数取一个名字,当需要用到这个功能的时候,就可以通过这个名字 ...

  6. PAT (Basic Level) Practise (中文)- 1011. A+B和C (15)

    http://www.patest.cn/contests/pat-b-practise/1011 给定区间[-231, 231]内的3个整数A.B和C,请判断A+B是否大于C. 输入格式: 输入第1 ...

  7. Maven搭建Struts2+Spring3+Hibernate4框架

    做了三年多的JavaEE开发了,在平时的JavaEE开发中,为了能够用最快的速度开发项目,一般都会选择使用Struts2,SpringMVC,Spring,Hibernate,MyBatis这些开源框 ...

  8. Mutations-freecodecamp算法题目

    Mutations(比较字符串) 要求 如果数组第一个字符串元素包含了第二个字符串元素的所有字符,函数返回true. 不用考虑大小写和字符顺序 思路 将数组中的两个字符串小写化 将第二个数组元素(第二 ...

  9. 科技庄园(背包dp)---对于蒟蒻来说死了一大片的奇题

    题目描述: Life种了一块田,里面种了一些桃树. Life对PFT说:“我给你一定的时间去摘桃,你必须在规定的时间之内回到我面前,否则你摘的桃都要归我吃!” PFT思考了一会,最终答应了! 由于PF ...

  10. Linux基础学习-NFS网络文件系统实时文件共享

    NFS网络文件系统 如果大家觉得Samba服务程序的配置太麻烦了,那么你共享文件的主机都是Linux系统,那么推荐大家在客户端部署nfs服务来共享文件.nfs(网络文件系统)服务可以将远程Linux系 ...