POJ 3258 River Hopscotch(二分答案)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 21939 | Accepted: 9081 |
Description
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
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
has to jump after removing M rocks
Sample Input
25 5 2
2
14
11
21
17
Sample Output
4
Hint
【题意】
在起点和终点之间,有 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(二分答案)的更多相关文章
- [ACM] POJ 3258 River Hopscotch (二分,最大化最小值)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6697 Accepted: 2893 D ...
- poj 3258 River Hopscotch(二分+贪心)
题目:http://poj.org/problem?id=3258 题意: 一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L. 河中有n块石头,每块石头到S都 ...
- POJ 3258 River Hopscotch 二分枚举
题目:http://poj.org/problem?id=3258 又A一道,睡觉去了.. #include <stdio.h> #include <algorithm> ]; ...
- poj 3258 River Hopscotch 二分
/** 大意:给定n个点,删除其中的m个点,其中两点之间距离最小的最大值 思路: 二分最小值的最大值---〉t,若有距离小于t,则可以将前面的节点删除:若节点大于t,则继续往下查看 若删除的节点大于m ...
- 二分搜索 POJ 3258 River Hopscotch
题目传送门 /* 二分:搜索距离,判断时距离小于d的石头拿掉 */ #include <cstdio> #include <algorithm> #include <cs ...
- POJ 3258 River Hopscotch(二分答案)
嗯... 题目链接:http://poj.org/problem?id=3258 一道很典型的二分答案的题目,和跳石头太像了!! 这道题的题目很显然,求最小中的最大值,注意这道题石头的位置不是从小到大 ...
- POJ 3258 River Hopscotch (最大最小距离)【二分】
<题目链接> 题目大意:现在有起点和终点两个石块,这两个石块之间有N个石块,现在对这N个石块移除M个石块,使得这些石块之间的最短距离最大,注意,起点和终点这两个石块不能被移除. 解题分析: ...
- POJ 3258 River Hopscotch
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11031 Accepted: 4737 ...
- poj 3258 River Hopscotch 题解
[题意] 牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离, 现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸的最小距离的最大值 ...
随机推荐
- js判断字符串是否json格式
function isJSON(str) { if (typeof str == 'string') { try { var obj=JSON.parse(str); if(typeof obj == ...
- 浏览器兼容css
原文:https://www.cnblogs.com/shizk/p/8459362.html 1.为什么会出现浏览器兼容问题? 由于各大主流浏览器由不同的厂家开发,所用的核心架构和代码也很难重和,这 ...
- 输出到网页前台js代码中包含单引号的处理方法
描述:后台输出js到前台,如 <script type="text/javascript"> //<![CDATA[ var aStepD ...
- 周期性调度器scheduler_tick
周期性调度器由中断实现,系统定时产生一个中断,然后启动周期性调度器,周期性调度器执行过程中要关闭中断, 周期性调度器执行完毕后再打开中断(handle_IRQ_event, IRQF_DISABLE ...
- C#反射学习
http://www.cnblogs.com/landeanfen/p/4642814.html http://blog.csdn.net/lianjiangwei/article/details/4 ...
- Qt Creater中的.pro文件和.pri文件
初学Qt,使用Qt Creater打开Demo "QT 3D Basic Shapes C++ Example",发现除了pro文件外,还有一个pri文件,在此小白一下! *.pr ...
- 如何在Java 环境下使用 HTTP 协议收发 MQ 消息
1. 准备环境在工程 POM 文件添加 HTTP Java 客户端的依赖. <dependency> <groupId>org.eclipse.jetty</groupI ...
- 开源一个简易轻量的reactor网络框架
github https://github.com/sea-boat/net-reactor net-reactor it's a simple and easy net framework with ...
- Java Jdk1.8 HashMap源代码阅读笔记二
三.源代码阅读 3.元素包括containsKey(Object key) /** * Returns <tt>true</tt> if this map contains a ...
- 8 -- 深入使用Spring -- 1...3 容器后处理器
8.1.3 容器后处理器(BeanFactoryPostProcessor) 容器后处理器负责处理容器本身. 容器后处理器必须实现BeanFacotryPostProcessor接口.实现该接口必须实 ...