博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
River Hopscotch-[二分查找、贪心]
阅读量:5128 次
发布时间:2019-06-13

本文共 3055 字,大约阅读时间需要 10 分钟。

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: 
L
N, 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 2214112117

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
#include 
#include
#include
#include
using namespace std;#define print_time_ printf("%f\n",double(clock())/CLOCKS_PER_SEC);#define maxn 50000int s[maxn+5];int L,N,M;bool judge(int mid){ int cnt=0; int i=0; while(1){ i=lower_bound(s+i, s+N+1, s[i]+mid)-s; if(i
=mid) cnt++; else break; } return cnt-(N-M)>=0;}int Bsearch(int a,int b){ if(!N) return b; int mid=(a+b)/2; int ans=0; while(a<=b){ bool flag=judge(mid); if(flag){ if(mid>ans) ans=mid; a=mid+1; mid=(a+b)/2; } else { b=mid-1; mid=(a+b)/2; } } return ans;}int main() { scanf("%d%d%d",&L,&N,&M); for(int i=0;i

转载于:https://www.cnblogs.com/Kiraa/p/5313103.html

你可能感兴趣的文章
jvm的工作流程
查看>>
有关按位DP
查看>>
物联网通讯与普通短信通讯的区别和要注意的地方
查看>>
MySQL table_id原理及风险分析
查看>>
Clone Graph
查看>>
人生最值得你去做的30件事
查看>>
Asp.Net Core中利用Seq组件展示结构化日志功能
查看>>
动态SQL现实一个表中求多列的和
查看>>
android编译openssl静态库.a
查看>>
maven依赖非maven库中jar的两种方法
查看>>
前端开发工程师面试题之综合篇
查看>>
fastjson 的使用
查看>>
UIScrollview使用
查看>>
CAniamtion 基本使用
查看>>
新型的Hbb项目目录结构
查看>>
Spring容器使用中出现 Access denied for user 'Administrator'@'localhost' (using password: YES)
查看>>
阅读思考作业2
查看>>
项目:jSon和Ajax登录功能
查看>>
最值得阅读学习的 10 个 C 语言开源项目代码
查看>>
基于Go语言构建区块链:part4
查看>>