题意:

起始有两个石头,位置是000和nnn,在这中间有n个石头,问如何拿走m块石头后,使得他们之间的最小间隔的最大值。

题目:

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, L units 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 M 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: L, N, 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).

分析:

由于是求最小间隔的最大值,(起初我理解成求出最大间隔,那么直接连续拿,遍历一遍就好,那肯定是错的),那么如何处理每次拿走石头都有可能改变之间间隔的最小值呢?
1.首先考虑到,只要拿走石头都会使石头之间的间隔变大,那么我们只需要枚举最小的间隔,看是否满足拿走k个石头后,仍旧是最小间隔。
2.那么如何求的最大的最小间隔呢,我们用二分进行枚举最小间隔即可
3.对石头位置进行排序,为之后的遍历寻找去掉一个石头后计算之间的间隔做准备。
4.dfs(x)=xdfs(x)=xdfs(x)=x是所有石头间的最小间隔,即任意两个石头之间的间隔都不能小于xxx,我们可以拿走k块石头,那就判断,当拿走k块石头后,仍是最小间隔即可。当存在拿走大于等于k个石头后,仍旧是最小间隔时,说明此时最小间隔偏大(我们最终需要的是正好拿走k个石头,使得该枚举的最小间隔,是石头间的最小间隔)

AC代码:

#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int M=5e4+10;
int n,m,k;
int f[M];
bool dfs(int x){///假设x是所有石头间的最大最小间隔,则此时需要判断拿走k个石头后,是否仍是最小间隔。int num=0;for(int i=1;i<m-k;i++){///一共m-2-k次循环,哪怕不满足while循环pre=m-kint pre=num+1;while(pre<m&&f[pre]-f[num]<x){///当存在石头间隔小于最小间隔,拿走这个石头,使间隔变大pre++;}if(pre==m){///若假设成立,存在拿走>=k块石头让石头间隔小于最小间隔;return false;}num=pre;}return true;
}
int main(){cin>>n>>m>>k;f[0]=0;for(int i=1;i<=m;i++){cin>>f[i];}f[m+1]=n;m=m+2;sort(f,f+m);int r=0,l=n+1;//上界要稍大一些,否则mid永远取不到值nwhile(l-r>1){int mid=(r+l)>>1;if(dfs(mid))//说明此时枚举的间隔较小r=mid;else l=mid;//是否可能使得所有石头之间的距离不小于d}cout<<r<<endl;return 0;
}

二分+最大化最小值 River Hopscotch POJ - 3258相关推荐

  1. POJ_2456_Agressive_cows_(二分,最大化最小值)

    描述 http://poj.org/problem?id=2456 有n个小屋,线性排列在不同位置,m头牛,每头牛占据一个小屋,求最近的两头牛之间距离的最大值. Aggressive cows Tim ...

  2. poj 2456 Aggressive cows 【二分+最大化最小值】

    题目链接:http://poj.org/problem?id=2456 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 210 ...

  3. poj3104 Drying(二分最大化最小值 好题)

    https://vjudge.net/problem/POJ-3104 一开始思路不对,一直在想怎么贪心,或者套优先队列.. 其实是用二分法.感觉二分法求最值很常用啊,稍微有点思路的二分就是先推出公式 ...

  4. 【POJ No. 3258】 跳房子游戏 River Hopscotch

    [POJ No. 3258] 跳房子游戏 River Hopscotch POJ题目地址 [题意] 跳房子游戏指从河中的一块石头跳到另一块石头,这发生在一条又长又直的河流中,从一块石头开始,到另一块石 ...

  5. POJ 3258 River Hopscotch(二分查找答案)

    一个不错的二分,注释在代码里 #include <stdio.h> #include <cstring> #include <algorithm> #include ...

  6. POJ 3258 River Hopscotch 经典二分

    点击打开链接 River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6189   Accepted: ...

  7. POJ 3258 River Hopscotch (二分)

    题目地址:POJ 3258 水题.二分距离,判断是否可行.需要注意的是最后一个,因为最后一个是没法移除的,所以还要倒着判断一下. 代码如下: #include <iostream> #in ...

  8. bzoj 1650: [Usaco2006 Dec]River Hopscotch 跳石子(二分)

    1650: [Usaco2006 Dec]River Hopscotch 跳石子 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 721  Solved: ...

  9. River Hopscotch问题(二分)

    又是一个卡了我好久好久好久的二分题 我真的不会二分啊,有没有大佬可以给我讲讲 我就是个憨憨 题目链接River Hopscotch 题目描述: Every year the cows hold an ...

最新文章

  1. 对话云知声李霄寒:不计成本研发芯片,探索语音之外的“硬”实力
  2. U-Boot的启动信息和命令使用
  3. 微服务网关 Kong 快速上手攻略
  4. 审计 Linux 系统的操作行为的 5 种方案对比
  5. IEEE公布2.5G和5G以太网IEEE 802.3bz标准
  6. json-lib解析json之二维JSONArray
  7. python二维插值_SciPy二元样条插值
  8. hihocoder 1403 后缀数组一·重复旋律 (后缀数组 + 二分)
  9. 【win10升级】我们无法更新系统保留的分区
  10. 成为一名Java后端工程师需要掌握的技能
  11. 4字母域名价值高吗?目前值多少钱?
  12. 框架的优缺点(TP CI)
  13. 2018网易互娱秋招笔试题
  14. GitHub里的MySQL基础架构自动化测试
  15. 背光模块市场现状研究分析与发展前景预测报告
  16. Python||PyCharm||代码为什么是灰色的???(已解决)
  17. 遗传算法的特性以及在具体算法应用中的应用
  18. GM、VP、FVP、CIO都是什么职位
  19. Django 基于类的通用视图详解
  20. 自定义view2/12----Paint常用方法(主要是ColorMatrix,Xfermode)

热门文章

  1. Failed:(13: Permission denied)导致访问浏览器出现Nginx 500 Internal Server Error
  2. Android Studio之提示Unable to delete directory ‘*****\MyApplication\app\build‘
  3. LeetCode之Longest Common Prefix
  4. 【C语言简单说】四:常量
  5. pytorch forward_【Pytorch部署】TorchScript
  6. 如果边横向移动边扔球,球会怎么运动?
  7. 假如人类长出翅膀,会变成这种怪样子
  8. 出现了!豆瓣最高9.9分,2020年最值得看的美剧!你居然还没看过?【内附资源】...
  9. 史上首次!世界杯使用视频裁判
  10. python达梦数据库_Python 编程可以访问达梦数据吗?