标签:
题意:一直青蛙过河,河宽l米,上面有n个石头,给出每个石头的位置,然后要求最多m步跳过去,问他最小最远一步可以跳多远。
题解:最小的最大值的计算用二分,二分出距离然后拿去判断,在这个距离内贪心的跳,判断能否跳到对岸。
#include <stdio.h>
#include <algorithm>
using namespace std;
const int N = 500005;
int l, n, m, sto[N];
bool judge(int x) {
int k = 0, cnt = 0;
for (int i = 1; i <= n + 1; i++) {
if (sto[i] - sto[k] > x) {
if (k == i - 1)
return false;
cnt++;
k = i - 1;
i--;
if (cnt >= m)
return false;
}
}
return true;
}
int main() {
while (scanf("%d%d%d", &l, &n, &m) == 3) {
for (int i = 1; i <= n; i++)
scanf("%d", &sto[i]);
sort(sto + 1, sto + n + 1);
sto[0] = 0;
sto[n + 1] = l;
int left = 0, right = l + 1;
while (left < right) {
int mid = (left + right) / 2;
if (judge(mid))
right = mid;
else
left = mid + 1;
}
printf("%d\n", left);
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/hyczms/article/details/45628129