二分搜索 POJ 3258 River Hopscotch

时间:2023-03-09 17:27:48
二分搜索 POJ 3258 River Hopscotch

题目传送门

 /*
二分:搜索距离,判断时距离小于d的石头拿掉
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std; typedef long long ll;
const int MAXN = 5e4 + ;
const int INF = 0x3f3f3f3f;
ll a[MAXN];
int n, m; bool check(ll d) {
int last = ; int cnt = ;
for (int i=; i<=n+; ++i) {
if (a[i] - a[last] <= d) cnt++;
else last = i;
}
return cnt > m;
} int main(void) { //POJ 3258 River Hopscotch
//freopen ("POJ_3258.in", "r", stdin); ll x;
while (scanf ("%I64d%d%d", &x, &n, &m) == ) {
a[] = ; a[n+] = x;
for (int i=; i<=n; ++i) {
scanf ("%I64d", &a[i]);
} sort (a, a+n+); ll l = , r = a[n+];
while (l <= r) {
ll mid = (l + r) >> ;
if (check (mid)) r = mid - ;
else l = mid + ;
}
printf ("%I64d\n", l);
} return ;
}