Problem 361
Aggressive Cows
Medium · 20 pts · Array, Binary Search, Greedy
Problem
A barn has N stalls at distinct positions along a line. You must place C cows into C different stalls so that the cows are as far apart as possible: the smallest distance between any two cows should be as large as you can make it. Print that largest possible minimum distance. Binary search on the answer: for a candidate distance D, greedily place cows from left to right, each in the first stall at least D after the previous cow, and check whether all C cows fit.
Input
First line: N and C. Second line: the N stall positions.
Output
Print the largest possible minimum distance between two cows.
Constraints
2 <= C <= N <= 100000; 0 <= position <= 1000000000; positions are distinct
Example input
5 3 1 2 8 4 9
Example output
3
Explanation
With stalls at 1 2 4 8 9 and 3 cows, placing them at 1, 4 and 8 (or 1, 4 and 9) keeps every pair at least 3 apart. A minimum distance of 4 is impossible, so the answer is 3.