http://poj.org/problem?id=2456
题意:有N个排成一条线的牛棚,M头牛会互相攻击,为了减少伤害,使得最近的两头牛的距离最大,求此值。 (例题)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAX_N = 100000, INF = 1e9+1;
int N, M;
double a[MAX_N+1];
bool C(int d)
{
int last = 0;
for(int i = 1; i < M; i++)
{
int crt = last + 1;
while(crt < N && a[crt] - a[last] < d)
crt++;
if(crt == N)
return false;
last = crt;
}
return true;
}
void solve()
{
sort(a, a+N);
int l = 0, r = INF;
while(r - l > 1)
{
int mid = (l+r) / 2;
if(C(mid))
l = mid;
else
r = mid;
}
printf("%d\n", l);
}
int main()
{
//freopen("in.txt", "r", stdin);
scanf("%d %d", &N, &M);
for(int i = 0; i < N; i++)
scanf("%lf", a+i);
solve();
return 0;
}