River Hopscotch
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9326   Accepted: 4016

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, Lunits
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).

题意是给了n个石头,给了它们到起点的距离,还有一个终点。问要去掉这n个石头中的m个,使得其间距的最小值最大。

一开始在想,这个题怎么二分。最后也是看了别人的思路才反应过来,二分最小值,再比较最小值是mid时去掉的石头个数来改动left还是right。这种想法真是很好,尤其是遍历一遍就知道当最短值是mid时去掉的石头个数,那处的想法很nice,涨姿势~

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int L, N, M;
int dis[50005]; bool check(int mid)
{
int i,before = 0,cnt = 0;
for (i = 1; i <= N + 1; )
{
if (dis[i] - dis[before] >= mid)//这块用于判断是否去掉石头
{
i++;
before = i - 1;
}
else
{
i++;
cnt++;
}
}
if (cnt <= M)
{
return true;
}
else
{
return false;
}
} int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int i,left,right,mid;
cin >> L >> N >> M; dis[0] = 0;
dis[N + 1] = L; for (i = 1; i <= N; i++)
{
cin >> dis[i];
} sort(dis, dis + 1 + N); left = 0;
right = 2*L;//可能要把所有的石头都去掉,所以可能取到最大值L,这里索性将右端点扩大了很多 while (right - left > 1)
{
mid = (right + left) / 2;
if (check(mid))
{
left = mid;
}
else
{
right = mid;
}
}
cout << left << endl;
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 3258:River Hopscotch 二分的好想法的更多相关文章

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

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

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

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

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

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

  4. POJ 3258 River Hopscotch 二分枚举

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

  5. poj 3258 River Hopscotch 二分

    /** 大意:给定n个点,删除其中的m个点,其中两点之间距离最小的最大值 思路: 二分最小值的最大值---〉t,若有距离小于t,则可以将前面的节点删除:若节点大于t,则继续往下查看 若删除的节点大于m ...

  6. 二分搜索 POJ 3258 River Hopscotch

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

  7. POJ 3258 River Hopscotch

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

  8. poj 3258 River Hopscotch 题解

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

  9. POJ 3258 River Hopscotch (binarysearch)

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

随机推荐

  1. QT5安装

    Windows+Qt5.3.1+VS2013安装教程 https://blog.csdn.net/two_ye/article/details/96109876 (已成功)windows下,VS201 ...

  2. maven加载ojdbc14报错

    问题复现步骤: 1.在pom.xml里面添加ojdbc14的依赖,代码如下: <dependency> <groupId>com.oracle</groupId> ...

  3. 026、MySQL取字符串左边,取字符串右边,取字符串中间,取文本开始位置

    #取文本左边 ); #田 ); #田攀 ); #田攀5 #取文本右边 ); # ); # ); #攀52 #取文本中间 '); #田攀 '); #攀5 #从字符串s中获取s1的开始位置 不忘初心,如果 ...

  4. python——字符输出ASCII码

    总是忘记事,赶紧记下来,Python字符转成ASCII需要用到一个函数ord # 用户输入字符 ch = input("请输入一个字符: ") # 用户输入ASCII码,并将输入的 ...

  5. 指定盘符获取u盘PID、VID、序列号等信息

    最近学习scsi和DeviceIoControl,下载了微软WDK一些例子,以下代码精简自Windows-driver-samples-master\storage\tools\spti\src\sp ...

  6. 微信群API接口

    安卓微信的api,个人微信开发API协议,微信 ipad sdk,微信ipad协议,微信web版接口api,微信网页版接口,微信电脑版sdk,微信开发sdk,微信开发API,微信协议,微信接口文档sd ...

  7. JavaWeb开发校园二手平台项目 源码

    开发环境: Windows操作系统开发工具:MyEclipse/Eclipse + JDK+ Tomcat + MySQL 数据库 项目简介: JAVAWEB校园二手平台项目,基本功能包括:个人信息. ...

  8. 六十三、SAP中的逻辑运算符

    一.SAP中逻辑运算符包括AND, NOT, OR 二.输出如下

  9. 108-PHP类成员protected和private成员属性不能被查看数值

    <?php class mao{ //定义猫类 public $age; //定义多个成员属性 protected $weight; private $color; } $mao1=new ma ...

  10. Java的包装类

    一.概述 因为基本数据类型的变量身上没有任何的方法和属性,所以针对基本数据类型提供了对应的类形式--包装类. 利用这个类产生对象,调用对象身上的方法来操作这个数据. 二.分类 包装类分为以下几种: 基 ...