70pts二分求条
查看原帖
70pts二分求条
1187849
FrankShix楼主2025/1/25 08:54
#include <bits/stdc++.h>
using namespace std;

int n, c, a[(int)1e5 + 5], l, r = 1e6, mid;
bool f;

bool check(int x)
{
	int now = 1, res = 1;
	for (int i = 2; i <= n; ++i)
	{
		if (a[i] - a[now] >= x)
		{
			now = i;
			res++;
		}
	}
	return res >= c;
}

int main()
{
	cin >> n >> c;
	for (int i = 1; i <= n; ++i)
		cin >> a[i];
	for (int i = 2; i <= n; ++i)
	{
		if (a[i] == a[i - 1])
			f = true;
		else 
			break;
	}
	if (f)
	{
		cout << 0;
		return 0;
	}
	sort(a + 1, a + 1 + n);
	while (l < r)
	{
		mid = (l + r) / 2;
		if (check(mid))
			l = mid + 1;
		else
			r = mid - 1;
	}
	cout << r;
	return 0;
}
2025/1/25 08:54
加载中...