思路
猜
,猜数,测试是否满足,并缩小猜测范围。
代码
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=1e6+10;
int n, cn;
int v[maxn];
bool check(int m)
{
int t=v[0];
int cnt=1;
for(int i=1; i<n ;i++)
{
if(v[i]-t>=m)
{
cnt++;
t=v[i];
}
if(cnt>=cn) return true;
}
return false;
}
int main()
{
cin >> n >> cn;
for(int i=0;i<n;i++)
scanf("%d", &v[i]);
sort(v, v+n);
int l=0, r=v[n-1]-v[0];
while(r-l>1)
{
int m=(l+r)/2;
if(check(m)) {
l=m;
}
else r=m;
}
printf("%d\n", l);
return 0;
}