2018 ccnu summer 二分 [Cloned] The Frog's Games

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

Input

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

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

Sample Output

4
11

题意:青蛙跳越过河,河的长度为L,河里线性排列着n块石头分别排列,青蛙跳m下踩着石头过河,问 青蛙每次最少跳多远能过河?

解题思路:1.青蛙的跳远距离可以限制在 1 ~ L ,其跳远的距离(这里假设为 x )可以用二分给出来,问题就变成了跳远距离 x 是否能在青蛙跳 小于或等与 m 下的情况下成功跳到对岸。在跳的过程中遵循这种方式:每一次跳的过程中在跳远距离范围内尽可能跳到更远的石块上。若最后能成功在小于等于 m 次下跳到对面则返回 true,超过m次返回 false,若跳不过直接返回 false。 (这种方法有跳远距离 x 小与两块石头的情况出现下面给出另种限制条件)。

2.从所有的相邻两块石块中找到分隔距离最大的两块 距离为Max,则把青蛙的跳远距离限制在Max ~ L 之间,这就不存在跳不过去的情况了,最后判断按所遵循的方法跳的次数是否会 <= m 如果是返回 true ,否则 false ;


代码1:(限制条件在L范围内的)

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int L,n,m;
int s[500005];
bool tofind(int mid)
{
    int ans=0;
    int los=0;
    for(int i=1;i<=n+1;i++)
    {
        if(mid>=s[i]-s[los]&&(s[i+1]-s[los])>mid)
        {
            ans++;
            los=i;
        }
        if(mid<s[i]-s[los])
        {
            return false;
        }
    }
    ans++;
    if(ans<=m)
    {
        return true;
    }
    else
    {
        return false;
    }
}
int main()
{
    while(scanf("%d%d%d",&L,&n,&m)!=EOF)
    {
        s[0]=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&s[i]);
    }
    s[n+1]=L;
    sort(s+1,s+n+1);         //-- 借用了第二种限制条件的代码,这里没有删去。
    int Max=0;
    for(int i=1;i<=n+1;i++)
    {
        if((s[i]-s[i-1])>Max)  
        {
            Max=s[i]-s[i-1];
        }
    }
                            //-----------------------------------------
    int l=1,r=L,mid;
    while(r>=l)   
    {               
        mid=(r+l)/2; 

        bool  an=(tofind(mid));


        if(tofind(mid))
        {
            r=mid-1;
        }
        else
        {
            l=mid+1;
        }
    }
    printf("%d\n",l);
    }
    return 0;
}

代码2:(限制条件在 Max ~ L 的)

#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int L,n,m;
int s[500005];
bool tofind(int mid)
{
    int ans,los;
    ans=0;
    los=0;
    for(int i=1;i<=n;i++)
    {
        if(s[i]-s[los]<=mid&&s[i+1]-s[los]>mid)  //2 11 18 25
        {                                        // 2 6
            ans++;
            los=i;
        }
    }
    ans++;
    if(ans<=m)
    {
        return true;
    }
    else
        return false;
}
int main()
{
    while(scanf("%d%d%d",&L,&n,&m)!=EOF)
    {
        s[0]=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&s[i]);
        }
        s[n+1]=L;
        sort(s+1,s+n+1);

        int Max=0;
        for(int j=1;j<=n+1;j++)
        {
            if(s[j]-s[j-1]>Max)
            {
                Max=s[j]-s[j-1];
            }
        }

        int l=Max,r=L,mid,c;
        while(r>=l)
        {
            mid=(r+l)/2;
            bool an=tofind(mid);

            if(tofind(mid))
            {
                c=mid;
                r=mid-1;
            }
            else
            {
                l=mid+1;
            }
        }
        printf("%d\n",c);
    }
    return 0;
}

借鉴了csdn博主的文章,吸收了前辈的思想,写出了第一种限制范围的代码。虽然用了一天时间,但是我还是好菜啊。。

。。。。。

万木春来

猜你喜欢

转载自blog.csdn.net/LaoXiangQ/article/details/81489580