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).
 
思路
1. 二分法
 
代码
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(二分法搜索)的更多相关文章

  1. POJ 3258 River Hopscotch (二分法)

    Description Every year the cows hold an event featuring a peculiar version of hopscotch that involve ...

  2. 二分搜索 POJ 3258 River Hopscotch

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

  3. POJ 3258 River Hopscotch

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

  4. POJ 3258 River Hopscotch (binarysearch)

    River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5193 Accepted: 2260 Descr ...

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

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

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

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

  7. poj 3258 River Hopscotch 题解

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

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

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

  9. POJ 3258 River Hopscotch 二分枚举

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

随机推荐

  1. go channel例子

    channel初步认识: package main import "fmt" import "time" func main() { c := make(cha ...

  2. aps.net MVC view 判断方法

    两种方式 1.使用Razor 视图 新建app_code文件夹(在这个目录下ShowHelper.cshtml会自动编译成类,方法必须都是静态的) @functions{ //是否显 public s ...

  3. Spring Boot war包&jar包对比

    使用 Maven对SpringBoot程序进行打包处理有两种格式:一种是war包,一种是jar包. 虽然我们将springboot应用打包成了war包,但是我们依然可以使用 java -jar的方式来 ...

  4. POST数据时400错误

    第一种解决办法是关闭Csrf public function init(){ $this->enableCsrfValidation = false; } 第二种解决办法是在form表单中加入隐 ...

  5. Python 类的多态

    #python的多态 class Dog(): def eat(self): print("i am dog , eat something . ") class Cat(): d ...

  6. HashMap的最大容量为什么是2的30次方?

    今天看HashMap的底层实现,发现HashMap的最大容量规定为: // 最大容量(必须是2的幂且小于2的30次方,传入容量过大将被这个值替换) static final int MAXIMUM_C ...

  7. Ubuntu下Ruby的下载和编译源码安装

    1.Ruby的下载 Ruby可以在Ruby 官网上下载,如果想获取更多的Ruby版本,可以到淘宝镜像网站下载. 2.Ruby的编译源码安装 解压 首先把下载下来的源码压缩包解压到自己指定的目录 编译安 ...

  8. MCMC采样理论的一点知识

    看了好多相关的知识,大致了解了一下马尔可夫链-蒙特卡罗采样理论,有必要记来下来. 蒙特卡罗积分:(来自:http://blog.csdn.net/itplus/article/details/1916 ...

  9. MySQL谨慎使用"replace into"

    From: http://blog.xupeng.me/2013/10/11/mysql-replace-into-trap/ MySQL 对 SQL 有很多扩展,有些用起来很方便,但有一些被误用之后 ...

  10. 浪漫程序员 HTML5爱心表白动画

    我们程序员在追求爱情方面也是非常浪漫的,下面是一位同学利用自己所学的HTML5知识自制的HTML5爱心表白动画,画面非常温馨甜蜜,这样的创意很容易打动女孩,如果你是单身的程序员,也赶紧来制作自己的爱心 ...