二分搜索 POJ 3258 River Hopscotch

 

题目传送门

 1 /*  2  二分:搜索距离,判断时距离小于d的石头拿掉  3 */  4 #include <cstdio>  5 #include <algorithm>  6 #include <cstring>  7 #include <cmath>  8 using namespace std;  9 10 typedef long long ll; 11 const int MAXN = 5e4 + 10; 12 const int INF = 0x3f3f3f3f; 13 ll a[MAXN]; 14 int n, m; 15 16 bool check(ll d) { 17 int last = 0; int cnt = 0; 18 for (int i=1; i<=n+1; ++i) { 19 if (a[i] - a[last] <= d) cnt++; 20 else last = i; 21  } 22 return cnt > m; 23 } 24 25 int main(void) { //POJ 3258 River Hopscotch 26 //freopen ("POJ_3258.in", "r", stdin); 27 28  ll x; 29 while (scanf ("%I64d%d%d", &x, &n, &m) == 3) { 30 a[0] = 0; a[n+1] = x; 31 for (int i=1; i<=n; ++i) { 32 scanf ("%I64d", &a[i]); 33  } 34 35 sort (a, a+n+2); ll l = 0, r = a[n+1]; 36 while (l <= r) { 37 ll mid = (l + r) >> 1; 38 if (check (mid)) r = mid - 1; 39 else l = mid + 1; 40  } 41 printf ("%I64d\n", l); 42  } 43 44 return 0; 45 }

 

你可能感兴趣的:(poj)