Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at the end, Lunits away from the start (1 ≤ L ≤ 1,000,000,000). Along the river between the starting and ending rocks, N (0 ≤ N ≤ 50,000) more rocks appear, each at an integral distance Di from the start (0 < Di < L).

To play the game, each cow in turn starts at the starting rock and tries to reach the finish at the ending rock, jumping only from rock to rock. Of course, less agile cows never make it to the final rock, ending up instead in the river.

Farmer John is proud of his cows and watches this event each year. But as time goes by, he tires of watching the timid cows of the other farmers limp across the short distances between rocks placed too closely together. He plans to remove several rocks in order to increase the shortest distance a cow will have to jump to reach the end. He knows he cannot remove the starting and ending rocks, but he calculates that he has enough resources to remove up to rocks (0 ≤ M ≤ N).

FJ wants to know exactly how much he can increase the shortest distance *before* he starts removing the rocks. Help Farmer John determine the greatest possible shortest distance a cow has to jump after removing the optimal set of M rocks.

Input

Line 1: Three space-separated integers: LN, and M 
Lines 2.. N+1: Each line contains a single integer indicating how far some rock is away from the starting rock. No two rocks share the same position.

Output

Line 1: A single integer that is the maximum of the shortest distance a cow has to jump after removing M rocks

Sample Input

25 5 2
2
14
11
21
17

Sample Output

4

Hint

Before removing any rocks, the shortest jump was a jump of 2 from 0 (the start) to 2. After removing the rocks at 2 and 14, the shortest required jump is a jump of 4 (from 17 to 21 or from 21 to 25).

题意:小牛过河,可以移除M块石头,寻找所跳最小距离的最大值

题解:1 —— L之间进行二分查找,如果存在当前位置last+mid > a[i] 说明此种方案存在更小的距离,要想保证此时的mid为最小距离就要将当前的石块去除,用cnt记录已经去除石块的个数,如果cnt > M(所给的能够去除石块的个数),则更新 right = mid - 1,否则 left = mid + 1;

AC代码

 1 #include<stdio.h>
2 #include<algorithm>
3
4 using namespace std;
5
6
7 int l, n, m;
8 int a[50005];
9 int solve(int x)
10 {
11 int cnt = 0;
12 int last = 0;
13 for(int i = 1; i <= n+1; i++)
14 {
15 if(a[i] < last + x) //当前的位置加上跳的距离能够到达下一个石头
16 cnt++; //存在更小的跳跃距离
17 else
18 last = a[i]; //更新当前位置
19 }
20 return cnt;
21 }
22
23 int main()
24 {
25 int right, mid, left;
26 while(~scanf("%d%d%d", &l, &n, &m))
27 {
28 for(int i = 1; i <= n; i++)
29 scanf("%d", &a[i]);
30 sort(a+1, a+n+1);
31 a[n+1] = l;
32 int ans = 0;
33 right = l;
34 left = 1;
35 while(left <= right)
36 {
37 mid = (left + right) / 2;
38 if(solve(mid) > m)
39 right = mid - 1;
40 else
41 left = mid + 1;
42 }
43 printf("%d\n", left - 1);
44 }
45 return 0;
46 }

G - River Hopscotch(二分)的更多相关文章

  1. River Hopscotch(二分POJ3258)

    River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9263 Accepted: 3994 Descr ...

  2. POJ 3258 River Hopscotch(二分答案)

    River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21939 Accepted: 9081 Desc ...

  3. [ACM] POJ 3258 River Hopscotch (二分,最大化最小值)

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6697   Accepted: 2893 D ...

  4. POJ3258 River Hopscotch —— 二分

    题目链接:http://poj.org/problem?id=3258 River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total ...

  5. POJ 3258:River Hopscotch 二分的好想法

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9326   Accepted: 4016 D ...

  6. River Hopscotch(二分)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5473   Accepted: 2379 Description Every ...

  7. poj 3258 River Hopscotch(二分+贪心)

    题目:http://poj.org/problem?id=3258 题意: 一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L. 河中有n块石头,每块石头到S都 ...

  8. poj 3258 River Hopscotch 二分

    /** 大意:给定n个点,删除其中的m个点,其中两点之间距离最小的最大值 思路: 二分最小值的最大值---〉t,若有距离小于t,则可以将前面的节点删除:若节点大于t,则继续往下查看 若删除的节点大于m ...

  9. POJ 3258 River Hopscotch 二分枚举

    题目:http://poj.org/problem?id=3258 又A一道,睡觉去了.. #include <stdio.h> #include <algorithm> ]; ...

随机推荐

  1. 零基础学Python:数据容器

    1.常用操作 列表常用操作 在 ipython 中定义一个 列表,例如: l= list() 输入 l. 按下 TAB 键, ipython 会提示 字典 能够使用的函数如下: 可以到官方网址查询使用 ...

  2. android上实现0.5px线条

    转: android上实现0.5px线条 由于安卓手机无法识别border: 0.5px,因此我们要用0.5px的话必须要借助css3中的-webkit-transform:scale缩放来实现. 原 ...

  3. HDOJ-6665(离散化+DFS求连通分量)

    Calabash and Landlord HDOJ-6665 这里考察的是离散化的知识. 首先将所有的x坐标和y坐标放入两个数组中,然后对这两个数组进行排序.因为总共的坐标数就5个所以这两个数组的大 ...

  4. pytorch(14)权值初始化

    权值的方差过大导致梯度爆炸的原因 方差一致性原则分析Xavier方法与Kaiming初始化方法 饱和激活函数tanh,非饱和激活函数relu pytorch提供的十种初始化方法 梯度消失与爆炸 \[H ...

  5. Slenium详解

    Slenium介绍 Selenium 是一个 Web 应用的自动化框架. 通过它,我们可以写出自动化程序,像人一样在浏览器里操作web界面. 比如点击界面按钮,在文本框中输入文字 等操作. 而且还能从 ...

  6. .NET 5学习笔记(11)—— Host Blazor WebAssembly in a Windows Service

    实在是被某软忽悠瘸了,愤而写此一篇.希望能让同样需求的同学们少走弯路.某软在<在 Windows 服务中托管 ASP.NET Core>中,介绍了通过创建Worker Service工程, ...

  7. @MockBean 注解后 bean成员对象为 null?

    笔者在写自测的时候遇到的问题: 我想模拟一个Bean,并在之后使用Mockito打桩,于是使用了 @MockBean 注解(spring集成mockito的产物),但代码编写好了后启动测试却报Null ...

  8. redhat配置问题

    redhat开机自动连接网络配置 vim  /etc/sysconfig/network-scripts/ifcfg-eth0 将  ONBOOT=no 更改为 yes 即可 redhat配置 yum ...

  9. VSCode 微信小程序扩展开发

    写在前面 为什么要开发这个扩展呢,是因为微信开发者工具自身不支持页面引入组件的跳转,人工根据引入组件路径查看对应代码的方式,效率偏低.就形如这样的json文件,引入了多个组件,比如要查看 " ...

  10. android分析之mutex

    Android的锁是对Linux锁的一种包装: // ------------------------------------------------------------------------- ...