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

解题思路:
  一开始题意并不太好理解。稍微转换一下思路,给定N+2个石头,从中选出N+2-M个石头使他们之间最小的距离最大。
经过分析容易发现,无论如何增减,石头之间的距离是有限的,最小为当前石头之间最小距离min,最大为L。那么问题可以转换成这样:
  在[min,L]范围内搜索满足下列条件的距离dm:
  1)有且仅有N+2-M个石头(包括起始和终点处的两块石头),他们之间的最小距离为di。
    (即:任意增减一个石头,他们之间的最小距离就不是di)
  2)dm=max{di};
 注意:条件(1)(2)保证dm的唯一性。
那么我们可以二分查找di,以di为间隔依次挑选石头,数量记为cnt,
如果cnt>N+2-M,说明di选小了;
如果cnt<N+2-M,说明di选大了;
如果cnt=N+2-M,不一定满足条件(2),需要继续向上(增大)查找。 代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <ctime>
using namespace std;
#define print_time_ printf("%f\n",double(clock())/CLOCKS_PER_SEC);
#define maxn 50000
int s[maxn+];
int L,N,M; bool judge(int mid){
int cnt=;
int i=; while(){
i=lower_bound(s+i, s+N+, s[i]+mid)-s;
if(i<N+&&s[N+]-s[i]>=mid) cnt++;
else break; }
return cnt-(N-M)>=;
} int Bsearch(int a,int b){
if(!N) return b;
int mid=(a+b)/;
int ans=;
while(a<=b){
bool flag=judge(mid);
if(flag){
if(mid>ans) ans=mid;
a=mid+;
mid=(a+b)/;
}
else {
b=mid-;
mid=(a+b)/;
}
}
return ans;
}
int main() {
scanf("%d%d%d",&L,&N,&M);
for(int i=;i<N;i++)
scanf("%d",&s[i+]);
s[]=;s[N+]=L;
sort(s, s+N+);
int low_bound=(<<)-;
for(int i = ; i <= N+; i++)
{
low_bound = min(low_bound, s[i]-s[i-]);
}
printf("%d\n",Bsearch(low_bound, L));
//print_time_
return ;
}

River Hopscotch-[二分查找、贪心]的更多相关文章

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

    一个不错的二分,注释在代码里 #include <stdio.h> #include <cstring> #include <algorithm> #include ...

  2. Can you find it?——[二分查找]

    Description Give you three sequences of numbers A, B, C, then we give you a number X. Now you need t ...

  3. River Hopscotch(二分最大化最小值)

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9923   Accepted: 4252 D ...

  4. C. Tavas and Karafs 二分查找+贪心

    C. Tavas and Karafs #include <iostream> #include <cstdio> #include <cstring> #incl ...

  5. poj 3258 River Hopscotch 【二分】

    题目真是不好读,大意例如以下(知道题意就非常好解了) 大致题意: 一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L. 河中有n块石头,每块石头到S都有唯一的距 ...

  6. 【POJ - 3258】River Hopscotch(二分)

    River Hopscotch 直接中文 Descriptions 每年奶牛们都要举办各种特殊版本的跳房子比赛,包括在河里从一块岩石跳到另一块岩石.这项激动人心的活动在一条长长的笔直河道中进行,在起点 ...

  7. bzoj 1650: [Usaco2006 Dec]River Hopscotch 跳石子【贪心+二分】

    脑子一抽写了个堆,发现不对才想起来最值用二分 然后判断的时候贪心的把不合mid的区间打通,看打通次数是否小于等于m即可 #include<iostream> #include<cst ...

  8. poj3258 River Hopscotch(二分最小值,好题)

    https://vjudge.net/problem/POJ-3258 二分最小值,判断需要删去的点的个数,如果大于给定,则直接return 0,则说明该数需要再小. 最后注意,起点是0终点是l,起点 ...

  9. POJ3258 River Hopscotch(二分最大化最小值)

    题目链接:http://poj.org/problem?id=3258 题意:给n个石头,起点和终点也是两个石头,去掉这石头中的m个,使得石头间距的最小值最大. 思路:二分石头间的最短距离,每次贪心地 ...

随机推荐

  1. re模块相关

    一.正则表达式中的转义: "\" 表示转义符 [()+*?/$.] 在字符组中一些特殊的字符会现出原形 所有的\w \d \s (\n,\t) \W \D \S 都表示它原本的意义 ...

  2. QT获取主机名称

    //获取主机名 QString localHost = QHostInfo::localHostName();

  3. elastic search book [ ElasticSearch book es book]

    谁在使用ELK 维基百科, github都使用 ELK (ElasticSearch es book) ElasticSearch入门 Elasticsearch入门,这一篇就够了==>http ...

  4. Directx11教程(58) 鼠标控制摄像机

    原文:Directx11教程(58) 鼠标控制摄像机        本篇教程我们实现鼠标旋转摄像机的操作.主要就是按下鼠标左键的时候,根据鼠标的移动对摄像机进行pitch, raw的组合旋转.具体修改 ...

  5. jdbc知识点(连接mysql)

    jdbc连接mysql 1.JDBC简介 JDBC: 指 Java 数据库连接,是一种标准Java应用编程接口( JAVA API),用来连接 Java 编程语言和广泛的数据库.从根本上来说,JDBC ...

  6. AtCoder Regular Contest 090 D - People on a Line

    D - People on a Line Problem Statement There are N people standing on the x-axis. Let the coordinate ...

  7. hdu1503 LCS

    题意:如果有相同的字母,这些字母只输出一次.其余的都输出. 先做一次LCS,标记相同的字母,然后回溯输出. #include<stdio.h> #include<string.h&g ...

  8. UVA_10071:Back to High School Physics

    Language:C++ 4.8.2 #include<stdio.h> int main(void) { int v, t; while(scanf("%d%d", ...

  9. 实用的cmd命令

    1.打开iis服务器:inetmgr 2.远程连接:mstsc 3.注册表:regedit.exe

  10. idea java内存分析工具

    https://blog.csdn.net/qq_22194659/article/details/83829891 https://www.ej-technologies.com/products/ ...