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. Django使用dwebsocket来通信,服务器报错[Error 10038]

    记录这次Django踩得最大的一次坑,没有之一.前前后后困扰了一周. 在使用Django的dwebsocket模块建立websocket时,不管是前端主动关闭,还是页面刷新,还是页面关闭.服务端均会报 ...

  2. SpringMVC中Freemarker获取项目根目录

    https://blog.csdn.net/whatlookingfor/article/details/51538995 在SpringMVC框架中使用Freemarker试图时,要获取根路径的方式 ...

  3. SpringBoot | contrller的使用

    @Controller 处理http请求 @RestController Spring4之后新加的注解,原来返回json需要@ResponseBody配合@Controller @RequestMap ...

  4. UvaLive3942(Trie + dp)

    查了半天数组越界的RE,才发现自己把ch数组放结构体里是过大的……放全局就A了. 类似区间的dp比较显然,只是用trie树做了优化,使得可以在trie树里一边走一边往上加dp值,不必枚举以前的每个位置 ...

  5. 线段树(单点更新) HDOJ 4288 Coder

    题目传送门 #include <cstdio> #include <cstring> #define lson l, m, rt << 1 #define rson ...

  6. 转 做了两款数据库监控工具(mysql and nosql),打算在近期开源

    http://www.cnblogs.com/leefreeman/p/7297549.html 监控指标:https://www.linuxidc.com/Linux/2015-08/122009. ...

  7. 基于python的request库,模拟登录csdn博客

    以前爬虫用urllib2来实现,也用过scrapy的爬虫框架,这次试试requests,刚开始用,用起来确实比urllib2好,封装的更好一些,使用起来简单方便很多. 安装requests库     ...

  8. 504 Base 7 七进制数

    给定一个整数,将其转化为7进制,并以字符串形式输出.示例 1:输入: 100输出: "202" 示例 2:输入: -7输出: "-10"注意: 输入范围是 [- ...

  9. 【转】哪个更快:Java堆还是本地内存

    译文出处: shenzhang   原文出处:原文链接 使用Java的一个好处就是你可以不用亲自来管理内存的分配和释放.当你用new关键字来实例化一个对象时,它所需的内存会自动的在Java堆中分配.堆 ...

  10. Java练习题00

    问题: 将一个数字与数组中的元素比较, 如果该数字存在数组中,给出该数字在数组中的位置: 如果该数字不在数组中,给出提示.   代码: public class Page84{     public ...