题目来自:洛谷P1824 进击的奶牛
题目描述:
#include <iostream>
#include <algorithm>
using namespace std;
int a[100005];
int main()
{
int n, c;
cin >> n >> c;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
sort(a, a + n);
long long int l = 0, r = 1000000000;
while (l < r)
{
int ans = 1;
long long int m = (l + r + 1) / 2;
int i = 0, j = 1;
while (i < n && j < n)
{
if (a[j] - a[i] >= m)
{
ans++;
i = j;
j = i + 1;
}
else
{
j++;
}
}
if (ans >= c)
{
l = m;
}
else
{
r = m - 1;
}
}
cout << l << endl;
//system("pause");
return 0;
}