hdu 4004 The Frog's Games 二分+贪心

来源:互联网 发布:数据库和数据源的关系 编辑:程序博客网 时间:2024/05/17 09:32

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

题意:一条长L,(1<= L <= 1000000000).(的河上有n,(0<= n <= 500000)个石子,青蛙最多能条m,(1<= m <= n+1)次。求青蛙过河单次跳跃的最小距离。


思路:二分跳跃的最小距离,然后贪心判断是否能跳过。

#include <iostream>#include<bits/stdc++.h>using namespace std;int a[550000],n,m,L;int ok(int k){    int num=1,last0=a[0],last1=a[1];    for(int i=2;i<=n+1;i++)    {        if(a[i]-last0<=k)   last1=a[i];        else if(a[i]-last1<=k)        {            last0=last1;            last1=a[i];            if(++num>m) return 0;        }        else    return 0;    }    return 1;}int main(){    while(~scanf("%d%d%d",&L,&n,&m))    {        a[0]=0,a[n+1]=L;        for(int i=1;i<=n;i++)   scanf("%d",&a[i]);        sort(a,a+n+2);        int l=0,r=L,ans=L;        while(l<=r)        {            int mid=(l+r)>>1;            if(ok(mid)) ans=mid,r=mid-1;                else    l=mid+1;        }        cout<<ans<<endl;    }}


0 0
原创粉丝点击