hdu 4004 The Frog's Games

来源:互联网 发布:萝莉酱llj222备用域名 编辑:程序博客网 时间:2024/05/19 04:02

http://acm.hdu.edu.cn/showproblem.php?pid=4004

分析:二分查找+贪心(哎)

#include<iostream>#include<cstdio>#include<algorithm>using namespace std;const int NM=500005;int a[NM],n,m;bool Stone(int mid){    int k,t,i,j;    i=1;j=k=0;    while(i<=n+1)    {        k++;  //需要的跳数        while(a[i]-a[j]<=mid&&i<=n+1)  //寻找能跳的最远的石头            i++;        j=i-1;    }if(k<=m) return 1;  else return 0;}int main(){    int L,temp,t,i,mid,high,low;    while(scanf("%d%d%d",&L,&n,&m)!=EOF)    {        for(i=1;i<=n;i++)            scanf("%d",&a[i]);        sort(a+1,a+n+1);        a[n+1]=L;a[0]=0;  //        t=a[1];        for(i=2;i<=n+1;i++)  //寻找最大间距        {            temp=a[i]-a[i-1];            t=temp>t?temp:t;        }        if(m>=n+1)  //跳数m和石头数+1相同            printf("%d\n",t);        else        {            low=t,high=L;            while(low<=high)            {                mid=(low+high)/2;                if(Stone(mid))                    high=mid-1;                else                    low=mid+1;            }            printf("%d\n",low);        }    }    return 0;}