The Frog's Games

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

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
 久违的惨案,错误tm竟然是因为没排序就去找最大差值只能怪自己傻逼了,很水的一个二分调到结束=  =
#include<bits/stdc++.h>
using namespace std;
int a[500005]={0},L,M,maxn,N;
int solve(int k)
{
   if(maxn>k) return 0;
   int s=1,x=0,i,j;
   for(i=1;i<=N;++i)
       if(a[i]-a[x]<=k&&a[i+1]-a[x]>k) {x=i;s++;}
   return s<=M;
}
int main()
{

int i,j,k;
    while(cin>>L>>N>>M){maxn=0;
        for(i=1;i<=N;++i) {
                scanf("%d",&a[i]);
        }a[N+1]=L;
        sort(a+1,a+1+N);   //由于输入时的无序所以要排序
        for(i=1;i<=N+1;++i) maxn=max(maxn,a[i]-a[i-1]);   //第一次在输入时做的这一步导致整个的失败哎,以后要注意!
        int l=0,r=L,mid;
        while(l<r){
            mid=l+(r-l)/2;
            if(solve(mid)){
              r=mid;
            }
            else{
              l=mid+1;
            }
        }
        cout<<l<<endl;
    }
    return 0;

}

HDU 4004 二分的更多相关文章

  1. hdu 4004 (二分加贪心) 青蛙过河

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4004 题目意思是青蛙要过河,现在给你河的宽度,河中石头的个数(青蛙要从石头上跳过河,这些石头都是在垂 ...

  2. hdu 4004 二分 2011大连赛区网络赛D

    题意:一个长为L的河,中间有n个石子,小青蛙需要跳少于m次过河,判断小青蛙每次跳跃最大距离的最小值 最大值最小,用二分 Sample Input 6 1 2 2 25 3 3 11 2 18 Samp ...

  3. hdu 4024 二分

    转自:http://www.cnblogs.com/kuangbin/archive/2012/08/23/2653003.html   一种是直接根据公式计算的,另外一种是二分算出来的.两种方法速度 ...

  4. HDU 4004 The 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(二分+小思维+用到了lower_bound)

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

  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. HDU 4004

    http://acm.hdu.edu.cn/showproblem.php?pid=4004 题意:青蛙过长L的河,只能落在石头上,石头数量是n,给出n个坐标,至多跳m次,求在可以过河的条件下,青蛙跳 ...

  9. hdu 4004 The Frog's Games

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

随机推荐

  1. redis缓存数据架构实战

    redis命令参考:http://redisdoc.com/ 与memcache对比 redis安装配置 yum安装 yum -y install redis 源码安装 PS:make报错**问题:* ...

  2. Javascript计算星座

    今天看群里一哥们折腾得挺热乎,手痒随便写了一个DEMO,供初学者参考. 重点,写程序先定注释,明确思路后再写具体代码. //星座定义 var constellations = [ {"Sta ...

  3. Day20 javaWeb监听器和国际化

    day20 JavaWeb监听器 三大组件: Servlet Listener Filter   Listener:监听器 初次相见:AWT 二次相见:SAX   监听器: 它是一个接口,内容由我们来 ...

  4. PHP实现返回JSON和XML的类分享

    PHP实现返回JSON和XML的类分享 <?php     class Reponse{         //private $result = array('code'=null,'messa ...

  5. C语言——stdio.h

        int fgetc(FILE * stream); get character from stream 返回流中的一个字符,并以int的类型返回,如果碰到文件的结尾,或者一个错误发生,函数返回 ...

  6. Redis在Linux下的安装与配置

    Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis是 NoSQL技术阵营中的一员. 说到NoSQL, ...

  7. 《FontForge常见问题FAQ》字王翻译版

    <FontForge常见问题FAQ> 字王翻译版 原文: http://fontforge.github.io/en-US/faq/ 翻译: 字王·中国   blog: http://bl ...

  8. ng-深度学习-课程笔记-9: 机器学习策略1(Week1)

    1 为什么要应用机器学习策略( Why is machine learning strategy ) 当你想优化一个问题的时候,通常可以有很多尝试(比如收集更多数据,增加迭代次数,改用adam,改变网 ...

  9. python 手动遍历迭代器

    想遍历一个可迭代对象中的所有元素,但是却不想使用for 循环 为了手动的遍历可迭代对象,使用next() 函数并在代码中捕获StopIteration 异常.比如,下面的例子手动读取一个文件中的所有行 ...

  10. scp命令简单应用

    实例1:从远处复制文件到本地目录 $scp root@10.6.159.147:/opt/soft/demo.tar /opt/soft/ 说明: 从10.6.159.147机器上的/opt/soft ...