题意:有牛要过河,河宽为l,上面有n块石头,要求拿走m块石头后,让相邻两个石头的最小距离最大(假设两岸也有石头但无法取走)。
题解:二分出距离然后拿去判断。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 50005;
int l, n, m, pos[N];
bool judge(int x) {
int cnt = 0, cur = 0;
for (int i = 1; i <= n; i++) {
while (i <= n && pos[i] - pos[cur] < x) {
i++;
if (cnt >= m)
return false;
cnt++;
}
cur = i;
}
if (l - pos[cur] < x)
return false;
return true;
}
int main() {
while (scanf("%d%d%d", &l, &n, &m) == 3) {
for (int i = 1; i <= n; i++)
scanf("%d", &pos[i]);
sort(pos + 1, pos + n + 1);
pos[0] = 0;
int left = 0, right = l;
while (left < right) {
int mid = (left + right + 1) / 2;
if (judge(mid))
left = mid;
else
right = mid - 1;
}
printf("%d\n", left);
}
return 0;
}
时间: 2024-10-06 09:46:58