码迷,mamicode.com
首页 > 其他好文 > 详细

二分搜索 POJ 3258 River Hopscotch

时间:2015-07-25 19:49:23      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

 

题目传送门

 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 3258 River Hopscotch

标签:

原文地址:http://www.cnblogs.com/Running-Time/p/4676377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!