洛谷 P2698 [USACO12MAR]花盆Flowerpot
来源:互联网 发布:安卓刷机工具 for mac 编辑:程序博客网 时间:2024/04/29 16:48
题目描述
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满足条件为止。
【代码】
//P2698 [USACO12MAR]花盆Flowerpot#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;int n,d,lef[100005];int q_min[100005],q_max[100005];struct node{ int x,y;}a[100005];inline bool comp(const node &u,const node &v) {return u.x<v.x;}inline int read(){ int x=0,f=1;char ch=getchar(); while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}int main(){ int i,j=0,ans=1e8,mxy=0,mny=1e8; n=read();d=read(); fo(i,1,n) a[i].x=read(),a[i].y=read(), mxy=max(mxy,a[i].y),mny=min(mny,a[i].y); sort(a+1,a+n+1,comp); if(mxy-mny<d) {printf("-1\n");return 0;} int h_min=1,t_min=0,h_max=1,t_max=0; fo(i,1,n) { 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
- 洛谷 P2698 [USACO12MAR]花盆Flowerpot(抄)
- 洛谷 P2698 [USACO12MAR]花盆Flowerpot (二分+单调队列)
- 洛谷p2698花盆
- usaco12 mar flowerpot浇花 p2698
- Flowerpot
- 洛谷P3049 [USACO12MAR]园林绿化Landscaping
- SSL P2698 打包
- 巧移花盆
- 【idea】智能花盆
- TOJ 3875 Flowerpot -- RMQ + 二分
- 玻璃钢花盆的一些说明
- 震惊!恐怖的血吸虫居然出现在花盆里!
- Bloombox:iPhone陶瓷底座,还能当花盆和扩音器
- 仿古石花盆你所不知道的
- 我不要百花齐放,给我一个花盆就好
- 有3n个花盆,红色、蓝色和黄色的各n个。开始时排列的顺序是混乱的,如黄、红、蓝、黄、黄、蓝、黄、红、红……
- 洛谷
- 获取当前日期是星期几
- noteforW3schoolsHtml
- windows 平台下的 CRT 函数
- JavaScript属性的添加,删除和查找
- 晶片成本和晶片面积函数(matlab)
- 洛谷 P2698 [USACO12MAR]花盆Flowerpot
- HELLO WORLD!!!!
- noteforW3schoolsCSS
- glusterfs 安装
- HDU 5884 Sort (哈夫曼全树+队列优化)
- c语言初学
- noteforW3schoolsAJAX
- 给整个布局文件设置背景,而不用给布局加id
- 关于handler的内存泄漏的总结