The Frog's Games

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 10307    Accepted Submission(s): 4686

 

Problem Description
The annual Games in frogs' kingdom started again. The most famous game is
the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This
project requires the frog athletes to jump over the river. The width of the
river is L (1<= L <= 1000000000). There are n (0<= n <= 500000) stones lined
up in a straight line from one side to the other side of the river. The
frogs can only jump through the river, but they can land on the stones. If
they fall into the river, they 
are out. The frogs was asked to jump at most m (1<= m <= n+1) times. Now the
frogs want to know if they want to jump across the river, at least what
ability should they have. (That is the frog's longest jump distance).
 

 

Input
The input contains several cases. The first line of each case contains three
positive integer L, n, and m. 
Then n lines follow. Each stands for the distance from the starting banks to
the nth stone, two stone appear in one place is impossible.
 

 

Output
For each case, output a integer standing for the frog's ability at least
they should have.
 

 

Sample Input
 
6 1 2 2 25 3 3 11 2 18
 
 

 

Sample Output
 
4 11
 
 

 

Source
 

 

Recommend
lcy

 


【题意】

有一条长度为L和河流,中间穿插n个石墩,青蛙跳m次经过石凳后到达对岸,求青蛙每次跳跃的最大距离的最小值


【分析】

1.二分左边界left=1,右边界right=L

2判断mid=(left+right)/2是否可以在跳跃m次以内(包括m)到达对岸,具体做法通过贪心来完成,尽量每一次跳跃跨过的石凳最远,这样可以使跳跃的总次数最小

3不断将区间二分,最终left==right时,left就是要求的值


【代码】

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=5e5+5;
int n,m,len,dis[N];
bool judge(int now){
if(dis[1]>now) return 0;
int sp=0,tot=0;
for(int i=1;i<=n;){
if(dis[i]-sp<=now){
if(dis[i]-sp==now||i==n){
tot++;
sp=dis[i];
}
i++;
}
else{
tot++;
sp=dis[i-1];
if(dis[i]-sp>now) return 0;
}
}
return tot<=m;
}
int main(){
while(scanf("%d%d%d",&len,&n,&m)==3){
for(int i=1;i<=n;i++) scanf("%d",dis+i);dis[++n]=len;
sort(dis+1,dis+n+1);
int l=1,r=len,mid,ans=0;
while(l<=r){
int mid=l+r>>1;
if(judge(mid)){
ans=mid;
r=mid-1;
}
else l=mid+1;
}
printf("%d\n",ans);
}
return 0;
}
 

HDU 4004 The Frog's Games(二分答案)的更多相关文章

  1. HDU 4004 The Frog's Games(二分+小思维+用到了lower_bound)

    The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) ...

  2. HDU 4004 The Frog's Games(二分)

    题目链接 题意理解的有些问题. #include <iostream> #include<cstdio> #include<cstring> #include< ...

  3. HDU 4004 The Frog's Games(2011年大连网络赛 D 二分+贪心)

    其实这个题呢,大白书上面有经典解法  题意是青蛙要跳过长为L的河,河上有n块石头,青蛙最多只能跳m次且只能跳到石头或者对面.问你青蛙可以跳的最远距离的最小值是多大 典型的最大值最小化问题,解法就是贪心 ...

  4. hdu 4004 The Frog's Games

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4004 The annual Games in frogs' kingdom started again ...

  5. HDU 5649 DZY Loves Sorting(二分答案+线段树/线段树合并+线段树分割)

    题意 一个 \(1\) 到 \(n\) 的全排列,\(m\) 种操作,每次将一段区间 \([l,r]\) 按升序或降序排列,求 \(m\) 次操作后的第 \(k\) 位. \(1 \leq n \le ...

  6. HDU - 3586 Information Disturbing 树形dp二分答案

    HDU - 3586 Information Disturbing 题目大意:从敌人司令部(1号节点)到前线(叶子节点)的通信路径是一个树形结构,切断每条边的联系都需要花费w权值,现在需要你切断前线和 ...

  7. 杭电 4004 The Frog's Games 青蛙跳水 (二分法,贪心)

    Description The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog T ...

  8. D - The Frog's Games (二分)

    The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. On ...

  9. HDU 4190 Distributing Ballot Boxes【二分答案】

    题意:给出n个城市,n个城市分别的居民,m个盒子,为了让每个人都投上票,问每个盒子应该装多少张票 二分盒子装的票数, 如果mid<=m,说明偏大了,r应该向下逼近 ,r=mid 如果mid> ...

随机推荐

  1. 学习TensorFlow的tf.concat使用

    https://www.tensorflow.org/api_docs/python/tf/concat

  2. Dominoserver 安装

    domino安装及语言包安装 http://wenku.baidu.com/view/f473600d581b6bd97f19ea9b.html dominoserver 安装后启动配置: http: ...

  3. webGL 光照

    1.着色(shading) 在三维图形学术语“着色”的真正含义就是,根据光照条件重建“物体各表面明暗不一的效果”的过程.明白着色过程,需要考虑两件事:    1.发出光线的光源类型.    2.物体表 ...

  4. 使用T4模板调用Sqlserver链接生成自己的模板

    <#@ template debug="false" hostspecific="false" language="C#" #> ...

  5. chrome插件开发之调试

    https://blog.csdn.net/qustdong/article/details/46046553

  6. 从Python学习中得到的一点启发 - Java逆向索引ArrayList

    看了几天Python,感觉记忆力不行了,很多东西记不住了.但是终归是得到了一点知识:重写一个ArrayList,允许从负值的索引得到指定的项.然后写一个得到斐波拉契数组的方法,这种方法要比递归调用的方 ...

  7. jsoup Cookbook(中文版)-Jsoup解析HTML

    jsoup Cookbook(中文版) 入门 1.      解析和遍历一个html文档 如何解析一个HTML文档: String html = "<html><head& ...

  8. Linux应急响应(一):SSH暴力破解

    0x00 前言 ​ SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议,主要用于给远程登录会话数据进行加密,保证数据传输的安全.SSH口令长度太短或者复杂度不够,如仅包含数字,或仅包 ...

  9. X-WAF简单测试体验

    X-WAF 最近才关注到的一款云WAF,花了一些时间搭建了一个环境,并做了一些测试,感觉比较适合新手来练习WAF Bypass. X-WAF是一款适用中.小企业的云WAF系统,让中.小企业也可以非常方 ...

  10. 如何在 Ubuntu 中安装 Node.js

    在终端中执行以下命令: sudo apt-get install python-software-properties python g++ make sudo add-apt-repository ...