Description
Farmer John built a long barrier, which contains N (2 <= n <= 100,000) compartments numbered X1 ,..., xn (0 <= xi <= 1,000,000,000 ).
However, John's C (2 <= C <= N) cows do not like this layout, and when a few cows are placed in a compartment, they are about to fight. In order not to let the ox hurt each other. John decided to allocate his own compartment for the ox so that the minimum distance between any two cows is as large as possible. What is the maximum and minimum distance?
Input
There are multiple groups of test data, ending with EOF.
First line: two integers N and C separated by Spaces
The second line -- line n + 1 indicates the position of Xi respectively.
Output
Each group of test data outputs an integer that satisfies the maximum and minimum values of the question. Note the line feed.
Sample Input
5 3
1
2
8
4
9
Sample output
3
Minimum binary distance after sorting
#include<cstdio>#include<algorithm>using namespace std;const int maxn = 100000 + 10;int x[maxn];int n, c;int ok(int k) { int last = 0; int cur; for(int i=1; i<c; ++i) { cur = last + 1; while(cur<n && x[cur] - x[last] < k) cur++; if(cur==n) return false; last = cur; } return true;}int main() { scanf("%d%d",&n, &c); for(int i=0; i<n; ++i) scanf("%d", &x[i]); sort(x, x+n); int l = 0, r = (x[n-1] - x[0])/(c-1); for(int i=0; i<100; ++i) { int mid = (l+r)/ 2; if( !ok(mid) ) r = mid; else l = mid; } printf("%d\n", l); return 0;}