Flowerpot(单调队列)
描述
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(单调队列)的更多相关文章
- [USACO12MAR]花盆Flowerpot (单调队列,二分答案)
题目链接 Solution 转化一下,就是个单调队列. 可以发现就是一段区间 \([L,R]\) 使得其高度的极差不小于 \(d\) ,同时满足 \(R-L\) 最小. 然后可以考虑二分然后再 \(O ...
- P2698 [USACO12MAR]花盆Flowerpot 单调队列
https://www.luogu.org/problemnew/show/P2698 警示 用数组写双端队列的话,记得le = 1, ri = 0:le<=ri表示队列非空 题意 求一个最小的 ...
- P2698 [USACO12MAR]花盆Flowerpot——单调队列
记录每天看(抄)题解的日常: https://www.luogu.org/problem/P2698 我们可以把坐标按照x递增的顺序排个序,这样我们就只剩下纵坐标了: 如果横坐标(l,r)区间,纵坐标 ...
- luogu 2698 [USACO12MAR]花盆Flowerpot 单调队列
刷水~ Code: #include<bits/stdc++.h> using namespace std; #define setIO(s) freopen(s".in&quo ...
- P2698 [USACO12MAR]花盆Flowerpot(单调队列+二分)
P2698 [USACO12MAR]花盆Flowerpot 一看标签........十分后悔 标签告诉你单调队列+二分了............ 每次二分花盆长度,蓝后开2个单调队列维护最大最小值 蓝 ...
- 洛谷P2698 花盆Flowerpot【单调队列】
题目描述 Farmer John has been having trouble making his plants grow, and needs your help to water them p ...
- BestCoder Round #89 B题---Fxx and game(单调队列)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5945 问题描述 输入描述 输出描述 输入样例 输出样例 题意:中文题,不再赘述: 思路: B ...
- 单调队列 && 斜率优化dp 专题
首先得讲一下单调队列,顾名思义,单调队列就是队列中的每个元素具有单调性,如果是单调递增队列,那么每个元素都是单调递增的,反正,亦然. 那么如何对单调队列进行操作呢? 是这样的:对于单调队列而言,队首和 ...
- FZU 1914 单调队列
题目链接:http://acm.fzu.edu.cn/problem.php?pid=1914 题意: 给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个 ...
随机推荐
- uvm_reg_fifo——寄存器模型(十五)
当我们对寄存器register, 存储器memory, 都进行了建模,是时候对FIFO进行建模了 uvm_reg_fifo毫无旁贷底承担起了这个责任,包括:set, get, update, read ...
- Json字符串与js数组互相转换
1.Json数据格式的字符串转换成js数组: JSON.parse(str); // str 字符串格式 2.js数组转换成Json数据格式字符串: var myJSONText = JSON.s ...
- LINQ 基础语句
去全部集合 using (dat0216DataContext con = new dat0216DataContext()) { //LoList 是转换成 List集合 List<Us ...
- node执行cmd命令方法
var cmd='tasklist';//获取 子进程模块的exec方法,用于执行cmd命令var exec = require('child_process').exec; //运行 定义的cmd命 ...
- hihoCoder #1165 : 益智游戏 (挑战赛11 B题)
题意:在一个序列中找到两个数a和b,使得a*b的因子个数最多,输出最多的因子个数. 思路:数据较多,处理会很慢.对序列中每个数字进行质数分解求因子个数,然后按照因子个数降序排列,对前50个因子最多的数 ...
- This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms while caching
今天运行自己的网站时报了这样一个错误,很是纳闷,这个网站运行了这么久,怎么报这个错呢,原来是做缓存的时候用到了基于windows平台的加密算法.解决方法如下: 删除注册表下的这个节点即可.删除HKEY ...
- 如何在Sierra运行 Specials K 的patch
https://github.com/ApolloZhu/CORE-Keygen-and-Special-K-for-Sierra-Utility/blob/master/Special%20K%20 ...
- 使用Java connector消费ABAP系统的函数
Java Connector(JCO)环境的搭建:Step by step to download and configure JCO in your laptop 我的ABAP系统有个函数名叫ZDI ...
- codeforce Gym 100500F Door Lock (二分)
根据题意略推一下,其实就是问你满足(a*(a+1))/2 < m <= ((a+1)*a(a+2))/2的a和m-(a*(a+1))/2 -1是多少. 二分求解就行了 #include&l ...
- noip模拟赛#38
我打开了#39的problem...想了半个小时多发现我一道题都不会写...于是我打开了#38的problem T1:循环数字的定义为能够将该数划分为若干相同长度的段并且都相同. n=2e18. =& ...