HDU - 4004 The Frog‘s Games

Problem Description:

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).

Input description:

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.

Output description:

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

Ideas:

The meaning of the question is that the frog crosses the river, can only fall on the stone, can only jump m times, ask us what is the shortest jump interval. First of all, we can get the maximum distance between the two stones from the input data, and then use this as the end point to divide into two, the starting point is 1, for each array, use the check function to test whether this distance can make the frog skip River, the shortest distance can be output at the end.

AC code:

#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;
}


Guess you like

Origin blog.csdn.net/m0_51727949/article/details/115186991