NYOJ-619 青蛙过桥(二分 + 贪心)

 

青蛙过桥

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 3
 
描述
The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The width of the river is L (1<= L <= 1000000000). There are n (0<= n <= 500000) stones lined up in a straight line from one side to the other side of the river. The frogs can only jump through the river, but they can land on the stones. If they fall into the river, they are out. The frogs was asked to jump at most m (1<= m <= n+1) times. Now the frogs want to know if they want to jump across the river, at least what ability should they have. (That is the frog's longest jump distance).
 
输入
The input contains several cases. The first line of each case contains three positive integer L, n, and m.
Then n lines follow. Each stands for the distance from the starting banks to the nth stone, two stone appear in one place is impossible.
输出
For each case, output a integer standing for the frog's ability at least they should have.
样例输入
6 1 2
2
25 3 3
11 
2
18
样例输出
4
11
来源
hdu
题意:
    简单的说就是给你一段长度,在这一段中给出m个点,然后在这m个点中选出k个点,
让这k个点之间相邻两个点的之间距离的最大值最小(与上一题的不同点)
思路:通过二分枚举这个最小值,然后通过贪心的思想找出满足要求的最小的这个最大值
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 int len, n, m;
 8 int pos[500005];
 9 
10 bool judge(int k)
11 {
12     int st = 0;
13     int cnt = 0;
14     for(int i = 0; i <= n; ++i)
15     {
16         /*
17         if(pos[i] - st > k)
18         {
19             ++cnt;
20             if(cnt > m)
21                 return false;
22             st = pos[i-1];
23         }*/
24         if(pos[i] - st <= k)
25         {
26             while(pos[i] - st <= k && i <= n)
27                 ++i;
28             st = pos[--i];
29             ++cnt;
30         }    
31     }
32     if(cnt <= m)
33         return true;
34     return false;
35 }
36 
37 int binary_search()
38 {
39     
40     int left = 0;
41     int right = len;
42     
43     for(int i = 1; i <= n; ++i)
44         if(pos[i] - pos[i-1] > left)
45             left = pos[i] - pos[i-1];
46     
47     
48     int mid = (left+right)>>1;
49     while(left <= right)
50     {
51         if(judge(mid)) 
52             right = mid-1;
53         else           // mid 太小 
54             left = mid+1;
55         mid = (left+right)>>1;
56     }
57     return right + 1; 
58 }
59 
60 int main()
61 {
62     while(~scanf("%d%d%d", &len, &n, &m))
63     {
64         for(int i = 0; i < n; ++i)
65             scanf("%d", &pos[i]);
66         pos[n] = len;
67         sort(pos, pos+n);
68         printf("%d\n", binary_search());
69     }    
70     return 0;
71 }

 

 

转载于:https://www.cnblogs.com/dongsheng/archive/2013/05/29/3107091.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值