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> ]; ...
随机推荐
- PCL点云配准(1)
在逆向工程,计算机视觉,文物数字化等领域中,由于点云的不完整,旋转错位,平移错位等,使得要得到的完整的点云就需要对局部点云进行配准,为了得到被测物体的完整数据模型,需要确定一个合适的坐标系,将从各个视 ...
- github fork 同步
git remote -v git remote add upstream url git fetch upstream git checkout master git merge upstream/ ...
- node学习笔记7——npm安装包
npm:Nodejs Package Manager(Nodejs包管理器).它有什么作用呢? 1.包统一下载途径: 2.自动下载依赖. 如何安装呢? 命令:npm install *** 比如我们要 ...
- 第一个shell程序
前言:我为什么又来学习shell呢?因为这个轻量级的编程小脚本语言能够帮我处理一些基于linux的复杂手工工作.真是一言难尽,学会一门又来一门!! 看了2天这个教程,试着写了一个小脚本,没啥技术含量, ...
- qualcomm compile instructions
qualcomm编译指令 Compile the Entire Android Software source build/envsetup.sh lunch msm8909-userdebug ma ...
- Maven构建自动化
构建自动化定义相关工程项目构建过程中,在当项目构建成功完成启动的情况下,来确保所依赖的项目是稳定的. 实例 考虑一个团队正在开发一个项目总线核心API上的其他两个项目的应用程序:网页UI和应用程序的桌 ...
- C#或者.NET下的强制垃圾回收办法
转载 2011年03月16日 17:21:00 标签: c# / .net / button / object / stream / class 8185 今天来谈谈C#的GC,也就是垃圾回收机制,非 ...
- repo sync出现“fatal: '../platform/abi/cpp.git' does not appear to be a git repository”的解决方案
在我下载Android源代码时,在最后一步调用repo sync,出现“fatal: '../platform/abi/cpp.git' does not appear to be a git rep ...
- C# 异常和异常处理
C# 语言的异常处理功能可帮助您处理程序运行时出现的任何意外或异常情况. 异常处理使用 try.catch 和 finally 关键字尝试某些操作,以处理失败情况,尽管这些操作有可能失败,但如果您确定 ...
- 轻松利用WayOs修正版配合推广EasyRadius用户微信公众自助平台
各大平台争相推出微信公共平台服务,EasyRadius也不会OUT!!! EasyRadius已推出微信公共平台自助服务,用户只需要把公众平台设置为开发者模式,并设置专用的地址,就可以实现旗下宽带用户 ...