zoukankan      html  css  js  c++  java
  • ACM HDU 4004 The Frog's Games(2011ACM大连赛区第四题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4004

    本文作者 :kuangbin   @SHU

    博客:www.cnblogs.com/kuangbin

    题意:一条长为l的河上有n个石头,告诉每个石头的位置,一个青蛙从头跳,求最少每次跳多少距离才能跳到对面。最多跳m次

     

    解题策略:二分答案。。。其实二分的效率是很高的噢~~~~很常用的算法。。

     

    直接贴代码了:

    /*HDU 4004
    二分
    */
    #include
    <stdio.h>
    #include
    <algorithm>
    using namespace std;
    #define MAXN 500010
    const int INF=0x7ffffff;
    int d[MAXN];
    int L;
    int calc(int x)//这个函数是计算对于跳跃能力是x的青蛙,最少跳几次过河
    {
    int now=0;
    int cnt=0;
    int ps=0;
    while(now<L)
    {
    now
    +=x;
    while(now>=d[ps+1]) ps++;
    now
    =d[ps];
    cnt
    ++;
    }
    return cnt;
    }

    int main()
    {
    int n,m;
    int left,right,mid;
    int minx;//两个石头间的最大距离,就是青蛙最小的跳的能力
    while(scanf("%d%d%d",&L,&n,&m)!=EOF)
    {
    minx
    =0;
    for(int i=1;i<=n;i++)
    scanf(
    "%d",&d[i]);
    sort(d
    +1,d+n+1);
    d[
    0]=0;
    d[n
    +1]=L;
    d[n
    +2]=INF;
    for(int i=1;i<=n+1;i++)
    if(minx<d[i]-d[i-1])
    minx
    =d[i]-d[i-1];
    left
    =minx;right=L;
    int res;
    while(left<=right)
    {
    mid
    =(right+left)>>1;
    if(calc(mid)<=m)
    {
    res
    =mid;
    right
    =mid-1;
    }
    else left=mid+1;
    }
    printf(
    "%d\n",res);
    }
    return 0;
    }

  • 相关阅读:
    php 基本符号
    php-fpm 启动和关闭
    php redis安装
    nginx 的安装
    Windows下Nginx的安装与配置
    apache 限制IP网段访问
    解决mysql导入导出数据乱码问题
    log_bin_trust_function_creators错误解决
    Mysqlbinlog使用
    通过yum安装Nagios
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2165907.html
Copyright © 2011-2022 走看看