Flowerpot
Time limit1sMemory limit128 MB
Find the minimum width interval on the x axis that captures raindrops whose heights differ by at least D.
- Level
Medium6 of 10
- Topics
- Two pointers, Sliding window, Sorting
- Solved
- No attempts yet
Problem
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 the vertical height of the drop and represents its location along a 1D number line.
Each drop falls downward (toward the x axis) at a rate of unit per second. You would like to place Farmer John's flowerpot of width 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 (so that the flowers in the pot receive plenty of water). A drop of water that lands exactly on the edge of the flowerpot counts as hitting it.
Given the value of and the locations of the raindrops, compute the minimum possible value of .
Input
- Line 1: Two space-separated integers, and ().
- Lines 2 to : Line contains the space-separated coordinates and of raindrop . Each value is in the range to .
Output
- Print a single integer: the minimum possible width of the flowerpot. Print if it is not possible to build a flowerpot wide enough to capture rain for at least units of time.
Hint
The time it takes a raindrop to reach the x axis equals its height . So among the drops the flowerpot catches, the difference between the largest and the smallest must be at least . For example, if the raindrops are at , , , and and rain must fall on the flowerpot for at least units of time, a flowerpot of width suffices: placing it from to catches raindrops 1 and 3, for a total rain duration of .