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
 

输入河宽L,石头数量N,步数M,在区间【1,L】内用二分法判断,算出区间里数作为一步的最大值时到对岸(最优解)需要多少的步数,若步数大于等于m,则记下步数,令右区间减一,否则令左区间加一,直到求出最小的能力。

 #include<cstdio>
#include<algorithm>
using namespace std;
int a[+];
int l,n,m,i,le,ri,mid,ans;
bool f(int k)      //返回值为真或假
{
int num=,sum=;
if(a[] > k)
return ;
for(i = ; i <= n ; i++)
{
if(a[i] - a[i-] > k)
return ;
if((a[i]-sum) > k)
{
num++;
sum=a[i-];
}
}
return (num+) <= m;
}
int main()
{
while(scanf("%d %d %d",&l,&n,&m)!=EOF)
{
for(i = ; i < n ; i++)
{
scanf("%d",&a[i]);
}
a[n]=l;
sort(a,a+n);
le=;
ri=l;
while(le <= ri)
{
mid=(le+ri)/;
if( f(mid))
{
ans=mid;
ri=mid-;
}
else
{
le=mid+;
}
}
printf("%d\n",ans);
}
}

 #include<cstdio>
#include<algorithm>
using namespace std;
int a[+];
int l,n,m,i,le,ri,mid,ans;
int f(int k)      //返回值为最大能力为k时的步数
{
int num=,sum=;
if(a[] > k)
return m+;
for(i = ; i <= n ; i++)
{
if(a[i] - a[i-] > k)
return m+;
if((a[i]-sum) > k)
{
num++;
sum=a[i-];
}
}
return num+;
}
int main()
{
while(scanf("%d %d %d",&l,&n,&m)!=EOF)
{
for(i = ; i < n ; i++)
{
scanf("%d",&a[i]);
}
a[n]=l;
sort(a,a+n);
le=;
ri=l;
while(le <= ri)
{
mid=(le+ri)/;
if( f(mid) <= m)
{
ans=mid;
ri=mid-;
}
else
{
le=mid+;
}
}
printf("%d\n",ans);
}
}
 

杭电 4004 The Frog's Games 青蛙跳水 (二分法,贪心)的更多相关文章

  1. 杭电oj 4004---The Frog Games java解法

    import java.util.Arrays; import java.util.Scanner; //杭电oj 4004 //解题思路:利用二分法查找,即先选取跳跃距离的区间,从最大到最小, // ...

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

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

  3. 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) ...

  4. HDUOJ----4004The Frog's Games(二分+简单贪心)

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

  5. hdu 4004 The Frog's Games

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

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

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

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

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

  8. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  9. The Frog's Games

    The Frog's Games Problem Description The annual Games in frogs' kingdom started again. The most famo ...

随机推荐

  1. Nginx的location配置概述【转】

    语法规则: location [=|~|~*|^~] /uri/ { … } = 开头表示精确匹配^~ 开头表示uri以某个常规字符串开头,理解为匹配url路径即可.nginx不对url做编码,因此请 ...

  2. Hexo - 修改永久链接的默认格式

    Hexo的永久链接的默认格式是 :year/:month/:day/:title/,比如访问站点下某一篇文章时,其路径是 2018/04/12/xxxx/,如果我们的文章标题是中文的,那么该路径就会出 ...

  3. Vue项目搭建流程 以及 目录结构构建

    Vue项目搭建流程 以及 目录结构构建 一个小的Vue项目, 基于微信浏览器的移动端, 做了这么多的练习项目, 这一次准备记录下构建的过程, 以方便以后的调高效率 环境准备 操作系统 我的 windo ...

  4. NYOJ #21 三个水杯(bfs)

    描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有标识,只能根据给出的水杯体积来计算.现在要求你写出一个程序,使其输出使初始状态到达目标 ...

  5. dubbo中Hessian方法重载问题处理

    dubbo中Hessian方法重载,报出如下错误信息: 十一月 , :: 下午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Ser ...

  6. 转-eclipse管理多个workspace

    Eclipse作为Java开发中最常用的开发工具,大家都很熟悉了,但是,当你做过很多项目后你会发现你的eclipse的package explorer视图下显示的project超级多,这时你可能会关闭 ...

  7. 自動獲取外網IP并發郵件

    問題: 公司有一Web系統需開放給香港Office公司查詢資料,但最近動態域名需實名認證, 因系統較小型,非公開大範圍使用,所以再認證一域名也沒多大必要, 所以想定時生成一封能查詢外網IP的郵件發送給 ...

  8. logging模块基础

    很多程序都有记录日志的需求,日志不仅可以保存访问记录,也可以有错误,警告等信息输出. python的logging模块提供了标准的日志接口,可以通过logging存储各种格式的日志.logging模块 ...

  9. oid和节点名称

    由于单篇文档最大字限制是40000个字符,不能将OID附上,因此写出我是如何得到这些OID的. 1.安装NET-SNMP yum install net-snmp yum install net-sn ...

  10. QProcess执行带管道的shell命令

    QStringList options; options << "-c" << "ls -l | grep a | sort"; QPr ...