首页 > 代码库 > hdu4004(二分)

hdu4004(二分)

 

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

 

大致题意 二分最大跳跃能力,判断是否可以在m次内到达对岸!

 

分析:由于求青蛙最小弹跳能力,所以二分不断枚举能力x,再对x判断是否在m次内到达对岸。。。

 

 

#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <queue>#include <cstdlib>#include <vector>#include <set>#include <map>#define LL long long#define inf 1<<30using namespace std;int n,m,l,a[500010];int judge(int x){    int res=0,temp=0;    for(int i=1;i<=n;i++)    {        if(a[i]-temp>x)return 0;        while(a[i]-temp<=x&&i<=n)i++;        temp=a[i-1];i--;        res++;    }    return res<=m;}int main(){    while(scanf("%d%d%d",&l,&n,&m)>0)    {        for(int i=1;i<=n;i++)scanf("%d",&a[i]);        a[++n]=l;        sort(a+1,a+n+1);        int left=0,right=l,mid,ans;        while(left<=right)        {            mid=(left+right)/2;            if(judge(mid))                right=mid-1,ans=mid;            else left=mid+1;        }        printf("%d\n",ans);    }}
View Code

 

hdu4004(二分)