HDU 4004 The Frog's Games [2011 大连网络赛]

题意:

青蛙过河,河的宽度是L,给出n个石头的坐标,不存在同一坐标的两个石头( two stone appear in one place is impossible),青蛙最多跳m次,求青蛙应该跳的最大距离中的最小距离


分析:

二分


//AC CODE:

#include <iostream>
#include<algorithm>
#include <cstdio>

using namespace std;

int L,n,m;
int dis[500005];
int is_ok(int mid)//过河需要跳的最小次数
{
    int cnt=0,from=0;
    for(int i=0; i<=n; i++)
    {
        if(dis[i]-from<=mid && dis[i+1]-from>mid)
        {
            from=dis[i];
            cnt++;
        }
    }
    //cout<<mid<<" "<<cnt<<endl;
    return cnt;
}
int main()
{
    int i,mid,ans,maxn;
    while(scanf("%d %d %d",&L,&n,&m)!=EOF)
    {
        for(i = 0 ; i < n ; i ++)
            scanf("%d",&dis[i]);
        dis[n]=L;
        maxn=-1;
        sort(dis,dis+n+1);
        for(i=1; i<=n; i++)
            if(maxn<dis[i]-dis[i-1])
                maxn=dis[i]-dis[i-1];
        dis[n+1]=2*L;//保证过不了这一点
        int beg=maxn,end=L;
        while(beg<=end)
        {
            mid=(beg+end)>>1;
            if(is_ok(mid)<=m)// <=
            {
                end=mid-1;
                ans=mid;
            }
            else// >
            {
                beg=mid+1;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值