P1824 进击的奶牛 二分答案
作者:
多米尼克領主的致意
,
2024-05-04 13:14:22
,
所有人可见
,
阅读 2
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int n, m;
bool check(int x)
{
int st = 1, cnt = 1;
for(int i = 2;i <= n;i++)
{
if(a[st] + x <= a[i])
{
st = i;
cnt++;
}
}
if(cnt >= m)return true;
return false;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i = 1;i <= n;i++)
cin >> a[i];
sort(a + 1, a + 1 + n);
int l = 1, r = 1e9;
while(l < r)
{
int mid = (l + r + 1) >> 1;
if(check(mid))l = mid;
else r = mid - 1;
}
cout << l;
return 0;
}