HDU - 4004 The Frog‘s Games

问题描述:

The annual Games in frogs’ kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The width of the river is L (1<= L <= 1000000000). There are n (0<= n <= 500000) stones lined up in a straight line from one side to the other side of the river. The frogs can only jump through the river, but they can land on the stones. If they fall into the river, they
are out. The frogs was asked to jump at most m (1<= m <= n+1) times. Now the frogs want to know if they want to jump across the river, at least what ability should they have. (That is the frog’s longest jump distance).

输入说明:

The input contains several cases. The first line of each case contains three positive integer L, n, and m.
Then n lines follow. Each stands for the distance from the starting banks to the nth stone, two stone appear in one place is impossible.

输出说明:

For each case, output a integer standing for the frog’s ability at least they should have.

SAMPLE INPUT:

6 1 2
2
25 3 3
11
2
18

SAMPLEOUTPUT:

4
11

思路:

题意就是青蛙过河,只能落在石头上,只能跳m次,问我们最短的跳跃间隔是多少。首先我们从输入数据中能够得到两块石头之间的最大距离是多少,然后以此为终点进行二分,起点为1,对每个数组,都用check函数来检验这个距离能否让青蛙跳过河,最后输出最短距离即可。

AC代码:

#include <bits/stdc++.h>
using namespace std;
long long l,n,t,ans;
long long a[500010];
int i;
int check(int x)
{
    
    
    int cnt=0,last=0;
    for(i=0; i<=n;)
    {
    
    
        if(a[i]>last+x)
        {
    
    
            if(last==a[i-1])
            {
    
    
                return 0;
            }
            last=a[i-1];
            cnt++;
        }
        else
        {
    
    
            i++;
        }
    }
    cnt++;
    if(cnt<=t)
        return 1;
    else
        return 0;
}
int main()
{
    
    
    while(scanf("%d%d%d",&l,&n,&t)!=EOF)
    {
    
    
        memset(a,0,sizeof(a));
        for(int i=0; i<n; i++)
        {
    
    
            scanf("%d",&a[i]);
        }
        a[n]=l;
        sort(a,a+n);
        int s=1,temp=l;
        while(s<=temp)
        {
    
    
            double mid=(s+temp)/2;
            if(check(mid))
            {
    
    
                ans=mid;
                temp=mid-1;
            }
            else
                s=mid+1;
        }
        cout<<ans<<endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/m0_51727949/article/details/115186991