3
排序后,二分最小距离
#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; }
poj 2456 Aggressive cows,二分,最大化最小值,布布扣,bubuko.com
poj 2456 Aggressive cows,二分,最大化最小值
原文:http://blog.csdn.net/yew1eb/article/details/38725479