#题面
#题目描述
Farmer John has been having trouble making his plants grow, and needs your help to water them properly. You are given the locations of raindrops () in the 2D plane, where represents vertical height of the drop, and represents its location over a 1D number line:
Each drop falls downward (towards the -axis) at a rate of unit per second. You would like to place Farmer John’s flowerpot of width somewhere along the 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 (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 and the locations of the raindrops, please compute the minimum possible value of .
#输入格式
- Line : Two space-separated integers, and . ()
- Lines : Line contains the space-separated coordinates of raindrop , each value in the range .
#输出格式
- Line : A single integer, giving the minimum possible width of the flowerpot. Output if it is not possible to build a flowerpot wide enough to capture rain for at least units of time.
#输入输出样例
样例输入 #1
4 5
6 3
2 4
4 10
12 15
样例输出 #1
2
样例解释 #1
There are raindrops, at , , , and . Rain must fall on the flowerpot for at least units of time.
A flowerpot of width is necessary and sufficient, since if we place it from , then it captures raindrops and , for a total rain duration of .
#思路
考场上想出来的做法,题解里好像没有一样的思路。
本题需要注意的点是,第一滴水和最后一滴水之间间隔的时间不一定恰好为 ,而是 ,一定要看清楚题,场上因为这个浪费了一个小时的时间。
将所有点按 值排序之后,开一个 set 记录 的对应的 值。因为题目中没有对第一滴水和最后一滴水之间接水数量之间做要求,所以直接在 set 中查询 的前驱和后继,取两个方向上长度的最小值更新答案即可。
#代码
1 |
|