POJ 3258 River Hopscotch(二分法搜索)
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 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
Sample Input
25 5 2
2
14
11
21
17
Sample Output
4
Hint
Source Code Problem: User: blazing
Memory: 424K Time: 157MS
Language: C++ Result: Accepted
Source Code
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int MAXN = ;
int L, N, M;
int rocks[MAXN]; int binarySearch(int low, int high) {
while( low <= high ) {
int count = , lastPos = ;
int mid = ( low + high ) >> ;
//cout << "mid : " << mid << endl;
for( int i = ; i <= N+; i ++ ) {
if( rocks[i] - rocks[lastPos] < mid ) {
// move one rock to extend the distance
count += ;
}else {
lastPos = i;
}
} if (count > M)
high = mid - ;
else
low = mid + ;
}
return high;
} int main() {
//freopen("E:\\Copy\\ACM\\poj\\3258_v2\\in.txt", "r", stdin);
while( cin >> L >> N >> M ) {
int low = 0x3FFFFFFF;
memset( rocks, , sizeof(rocks));
for( int i = ; i <= N; i ++ ) {
scanf("%d", &rocks[i]);
}
rocks[] = , rocks[N+] = L;
sort( rocks, rocks+N+); for(int i = ; i <= N+; i++) {
low = min ( low, rocks[i]-rocks[i-] );
} cout << binarySearch( low, L ) << endl;
}
return ;
}
POJ 3258 River Hopscotch(二分法搜索)的更多相关文章
- POJ 3258 River Hopscotch (二分法)
Description Every year the cows hold an event featuring a peculiar version of hopscotch that involve ...
- 二分搜索 POJ 3258 River Hopscotch
题目传送门 /* 二分:搜索距离,判断时距离小于d的石头拿掉 */ #include <cstdio> #include <algorithm> #include <cs ...
- POJ 3258 River Hopscotch
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11031 Accepted: 4737 ...
- POJ 3258 River Hopscotch (binarysearch)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5193 Accepted: 2260 Descr ...
- POJ 3258 River Hopscotch(二分答案)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 21939 Accepted: 9081 Desc ...
- [ACM] POJ 3258 River Hopscotch (二分,最大化最小值)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6697 Accepted: 2893 D ...
- poj 3258 River Hopscotch 题解
[题意] 牛要到河对岸,在与河岸垂直的一条线上,河中有N块石头,给定河岸宽度L,以及每一块石头离牛所在河岸的距离, 现在去掉M块石头,要求去掉M块石头后,剩下的石头之间以及石头与河岸的最小距离的最大值 ...
- 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> ]; ...
随机推荐
- JavaScript:属性的操作
一.属性的设置和获取 1.属性的设置和获取主要有两种方式: <!DOCTYPE html> <html lang="en"> <head> &l ...
- C/C++-STL中lower_bound与upper_bound的用法以及cmp函数
转载于:http://blog.csdn.net/tjpuacm/article/details/26389441 不加比较函数的情况: int a[]={0,1,2,2,3}; printf(&qu ...
- linux下安装redis并配置
redis官网上给出了安装步骤,这里做一下总结. 1.Download, extract and compile Redis with: wget http://download.redis.io/r ...
- 实现JTextfield 的右键 复制、剪切、粘贴功能。
http://leagion.iteye.com/blog/572730 ——————————————————————————————————————————————————————————————— ...
- Sword protobuf学习二
编写protobuf消息文件 文件格式: xxx.proto //标明使用哪个版本的protobuf,默认2.0版本 syntax = "proto3"; //类似于c++中的na ...
- 自然语言交流系统 phxnet团队 创新实训 项目博客 (九)
项目技术总结: VoiceToText的具体使用方法: 语音转文本部分是调用的科大讯飞的在线语音,它的激发方式是按键,通过按钮触发开启安卓设备的录音,此部分需要在源码中写入关于安卓权限的要求,来调用安 ...
- MD2关键桢动画3D模型加载.
在看Cg教程中,看到关键桢插值来表示一个动画的物体,例如一个动物拥有站着,奔跑,下跪等动画序列,美工将这些特定的姿态称为一个关键桢.为什么要用关键桢这种来表示了,这个比较容易理解,我们知道我们看的一些 ...
- Zend Framework2 入门教程(转)
转载自: http://my.oschina.net/lai1362000/blog/201301 重申:这本书作者的截图我都放上去了,没侵权啊. 别问那么多,我只是一个安静的搬砖工. 摘要 Zend ...
- php -- php模拟浏览器访问网址
目前我所了解到的在php后台中,用php模拟浏览器访问网址的方法有两种: 第一种:模拟GET请求:file_get_contents($url) 通过php内置的 file_get_contents ...
- android中清空一个表---类似truncate table 表名 这样的功能 android sqlite 清空数据库的某个表
public void clearFeedTable(){ String sql = "DELETE FROM " + FEED_TABLE_NAME +";" ...