Social Distancing
Time limit1sMemory limit512 MB
Place N cows on distinct integer grass points across M disjoint intervals on a line so that the minimum pairwise distance D is as large as possible, and output the largest achievable D.
- Level
Hard8 of 10
- Topics
- Binary search, Greedy, Intervals, Sorting
- Solved
- No attempts yet
Problem
Farmer John is worried for the health of his cows after an outbreak of the highly contagious bovine disease COWVID-19.
In order to limit transmission of the disease, Farmer John's cows () have decided to practice "social distancing" and spread themselves out across the farm. The farm is shaped like a 1D number line, with mutually-disjoint intervals () in which there is grass for grazing. The cows want to locate themselves at distinct integer points, each covered in grass, so as to maximize the value of , where represents the distance between the closest pair of cows. Please help the cows determine the largest possible value of .
Input
The first line of input contains and . The next lines each describe an interval in terms of two integers and , where . No two intervals overlap or touch at their endpoints. A cow standing on the endpoint of an interval counts as standing on grass.
Output
Print the largest possible value of such that all pairs of cows are units apart. A solution with is guaranteed to exist.
Hint
One way to achieve is to have cows at positions , , , and .