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

Description

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 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).

【题意】

在起点和终点之间,有 N 块岩石(不含起点和终点的岩石),至多可以从起点和终点之间移走 M 块岩石(不能移走起点和终点的岩石),求移走岩石后最短跳跃距离的最大值

【分析】

[NOIP2015跳石头(原题)]

直接二分,具体详见代码

【代码】

#include<cstdio>
#include<algorithm>
#include<iostream>
#define debug(x) cerr<<#x<<" "<<x<<'\n';
using namespace std;
inline int read(){
int x=,f=;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*+ch-'0';ch=getchar();}
return x*f;
}
const int N=1e5+;
int ans,n,m,L,d[N];
inline bool check(int now){
int res=,p=;//p为前一个石头到指向石头的距离
for(int i=;i<=n;i++){
if(d[i]-p<now){//如果第i个石头到前一个石头的距离小于假设的答案
res++;//该石头可以搬走
}
else{//如果大于(即满足条件)
p=d[i];//则保留该石头,并将p记录 下一个石头到该石头的距离
}
}
return res<=m;
}
int main(){
L=read(),n=read(),m=read();
for(int i=;i<=n;i++) d[i]=read();d[++n]=L;//我们假设在终点处有第n+1个石头,则他到起点的距离为L
sort(d+,d+n+);//保证石头有序
int l=,r=L,mid;
while(l<=r){
mid=l+r>>;
if(check(mid)){//说明二分结果小了,要向右二分
ans=mid;//记录答案
l=mid+;
}
else{
r=mid-;//说明结果大了,要向左二分
}
}
printf("%d\n",ans);
return ;
}

 

 

POJ 3258 River Hopscotch(二分答案)的更多相关文章

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

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

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

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

  3. POJ 3258 River Hopscotch 二分枚举

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

  4. poj 3258 River Hopscotch 二分

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

  5. 二分搜索 POJ 3258 River Hopscotch

    题目传送门 /* 二分:搜索距离,判断时距离小于d的石头拿掉 */ #include <cstdio> #include <algorithm> #include <cs ...

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

    嗯... 题目链接:http://poj.org/problem?id=3258 一道很典型的二分答案的题目,和跳石头太像了!! 这道题的题目很显然,求最小中的最大值,注意这道题石头的位置不是从小到大 ...

  7. POJ 3258 River Hopscotch (最大最小距离)【二分】

    <题目链接> 题目大意:现在有起点和终点两个石块,这两个石块之间有N个石块,现在对这N个石块移除M个石块,使得这些石块之间的最短距离最大,注意,起点和终点这两个石块不能被移除. 解题分析: ...

  8. POJ 3258 River Hopscotch

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11031   Accepted: 4737 ...

  9. poj 3258 River Hopscotch 题解

    [题意] 牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离, 现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸的最小距离的最大值 ...

随机推荐

  1. Easyradius 1.699更新,增加用户设备绑定、桥接用户管理功能

    最近几天,由于IDC机房设备的问题,导致OA连接3天一天挂2次,真是把我抑闷着,最事一个烦事特别多 好不容易等了一个五一假期,也不得空,把最近一些网友反馈的OA的一些功能及BUG进行修复,主要是以下几 ...

  2. shiro+redis多次调用doReadSession方法的解决方案

    Web 项目使用shiro,针对这个问题可以重写DefaultWebSessionManager,将缓存数据存放到request中,这样可以保证每次请求(可能会多次调用doReadSession方法) ...

  3. iOS : 判断运行设备类型是否是iPad

    以下代码由 CocoaChina 版主 “cclv” 分享,可用于判断应用运行的设备是否是 iPad #define isPad (UI_USER_INTERFACE_IDIOM() == UIUse ...

  4. AOP-配合slf4j打印日志

    基本思想 凡在目标实例上或在目标实例方法(非静态方法)上标注自定义注解@AutoLog,其方法执行时将触发AOP操作: @AutoLog只有一个参数,用来控制是否打印该方法的参数和返回结果的json字 ...

  5. js 浅拷贝和深拷贝

    传值与传址 了解了基本数据类型与引用类型的区别之后,我们就应该能明白传值与传址的区别了.在我们进行赋值操作的时候,基本数据类型的赋值(=)是在内存中新开辟一段栈内存,然后再把再将值赋值到新的栈中.例如 ...

  6. [Learn AF3]第四章 App framework组件之Button

    Button    组件名称:Button     是否js控件:否     使用说明:如果说panel组件是af3的“核心(heart of the ui)”,那么Button就是af中的五虎上将之 ...

  7. GCT之数学公式(三角函数)

  8. mac ssh中文乱码解决

    网上有如下解决法,至少我没有成功过: vim ~/.bash_profile export LC_ALL='zh_CN.utf8' 来源:http://www.liuhuadong.com/archi ...

  9. 小程序中监听textarea或者input输入的值动态改变data中数组的对象的值

    Page({ data: { todoLists:[ { detail:"", date:"", location:"", priority ...

  10. 文字描边css

    -webkit-text-stroke: 3.3px #2A75BF; -webkit-text-fill-color:#fff; 该方法在安卓端貌似不支持,显示为一团 或: -webkit-text ...