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. Hbase 学习(七) rowkey设计

    一直以来对rowkey的设计都比较迷茫,<hbase权威指南>倒是给出了个还算靠谱的例子. 下面这个例子有点儿像帖子表结构,它的rowkey设计是这样的,可以简单的理解为,什么人在什么时间 ...

  2. android开发(37) android使用android_serialport_api 操作串口,解决权限问题

    最近有个项目,要使用android设备操作串口的 斑马GK888T打印机,使用打印机打印二维码. 硬件设备连接方式: 安卓设备 通过 串口RS232 连接 斑马打印机的串口 那么就要解决:使用安卓设备 ...

  3. Java图形界面设计——substance皮肤

    http://jianweili007-163-com.iteye.com/blog/1141358 ————————————————————————————————————————————————— ...

  4. poj 1723 中位数

    最近在看一些中位数的东西,然后顺便也看了些题目.poj 1723不仅要求到水平位置的最短距离和,还要求水平都相邻的排成一排的最短距离和,即士兵都站成一列. 到y轴的距离好办,按y轴坐标排序,求中位数, ...

  5. 【转】MySQL分库分表数据迁移工具的设计与实现

    一.背景 MySQL作为最流行的关系型数据库产品之一,当数据规模增大遭遇性能瓶颈时,最容易想到的解决方案就是分库分表.无论是进行水平拆分还是垂直拆分,第一步必然需要数据迁移与同步.由此可以衍生出一系列 ...

  6. SQLSERVER中统计所有表的记录数

    SQLSERVER中统计所有表的记录数 利用系统索引表sysindexes中索引ID indid<1的行中的rows列存有该表的行数这一特点.    方法是利用隐藏未公开的系统存储过程sp_MS ...

  7. HTML5实现摇一摇的功能(实测后)--转

    eviceMotionEvent(设备运动事件)返回设备有关于加速度和旋转的相关信息.加速度的数据将包含三个轴:x,y和z(示意如下图所 示,x轴横向贯穿手机屏幕或者笔记本键盘,y轴纵向贯穿手机屏幕或 ...

  8. JDBC事务保存点(setSavepoint, releaseSavepoint )实例

    以下是使用事务教程中描述的setSavepoint和回滚的代码示例. 此示例代码是基于前面章节中完成的环境和数据库设置编写的. 复制并将以下示例代码保存到:JDBCSavepoint.java 中,编 ...

  9. Enigma Virtual Box:生成可执行文件。

    Enigma Virtual Box Enigma Virtual Box[1]  是软件虚拟化工具,它可以将多个文件封装到应用程序主文件,从而制作成为单执行文件的绿色软件.它支持所有类型的文件格式, ...

  10. rrnDB数据库简介-16S基因多拷贝数的证据

    16S基因作为mark gene在微生物群落结构的研究中发挥中重要作用, 但是候选的mark gene 肯定不止16S 一种,最新比较火热的功能基因,也可以作为mark gene.利用功能基因作为ma ...