Input
* Line 1: Two space-separated integers: N and C * Lines 2..N+1: Line i+1 contains an integer stall location, xiOutput
* Line 1: One integer: the largest minimum distanceSample Input
5 3 1 2 8 4 9Sample Output
3Hint
OUTPUT DETAILS: FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3. Huge input data,scanf is recommended
#include <iostream> #include <algorithm> using namespace std ; int main () { int a ,b ,c [ 100004 ],i ,j ,k ,le ,ri ,mid ,n ; cin >>a >>b ; for (i = 0 ;i <a ;i ++) cin >>c [i ]; sort (c ,c +a ); le =c [ 0 ]; ri =c [a -1 ]; while (le <ri ) { n = 0 ; mid =(ri +le )/ 2 ; for (j = 0 ,i = 1 ;i <a ;) { if (c [i ]-c [j ]<=mid ) { i ++; n ++; } else { j =i ; i ++; } } if (n >a -b ) { ri =mid ; } else le =mid +1 ; } cout <<le ; }