方法:
二分枚举加贪心。
用二分来枚举最小距离的最大值。
贪心来判断此时的最小距离是否可以符合,也就是是否可以放下所有的牛。
AC代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100000+100;
int n,a[maxn],c;
bool solve(int m)
{
int shumu = 1;
int t = a[0];
for(int i=1;i<n;i++)
{
if(a[i]-t>=m)
{
shumu++;
t = a[i];
}
if(shumu>=c)
return true;
}
return false;
}
int main()
{
cin>>n>>c;
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
int low = 0;
int high = a[n-1] - a[0];
while(low<=high)
{
int mid = (low+high)/2;
if(solve(mid))
{
low = mid+1;
}
else
{
high = mid - 1;
}
}
printf("%d\n",low-1);
return 0;
}