HDU 4004 The Frog's Games(二分+小思维+用到了lower_bound)

The Frog's Games

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 9678    Accepted Submission(s): 4428

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

InputThe 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.OutputFor 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
题意:蛤蟆直线过河,直线上有石头可以踩,告诉你河的宽度还有石头个数还有最多可以跳几次,每种过河方案都有一个单次跳的最大距离,让你求出所有方案里面单次跳的距离最小的那个方案,并输出这个距离
从单次跳的最大距离下手进行二分,而过河最少跳一次,所以单次跳的最大距离应该就是河的跨度
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
int a[500005];
int main()
{
    int l,n,m;
    while(~scanf("%d %d %d",&l,&n,&m))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        a[++n]=l;//把河的长度也放进去
        sort(a+1,a+1+n);//排序
        int ri=l,mid;
        int le=l/m;
        if(le*m<l)//为了保证以le的长度跳m次(不管石头的情况下)一定能到对岸
            le++;
        int mm=l;
        while(le<=ri)
        {
            mid=(le+ri)/2;
            int sum=0;
            for(int i=1;i<=m;i++)//以mid的距离模拟跳m次看看能不能到
            {
                int y=sum+mid;//直接跳能到达的距离
                int p=lower_bound(a+1,a+1+n,y)-a;//p是这次最远能跳的石头编号+1
                if(a[p]!=y&&(p==1||a[p]==sum))//要是p是当前的石头,代表这次不能跳到任何的石头上,失败
                    break;
                if(a[p]!=y)//因为是lower_bound(大于等于),所以如果不是y处有石头的话,要减一
                    p--;
                sum=a[p];
            }
            if(sum!=l)
                le=mid+1;
            else if(sum==l)
            {
                if(mm>mid)//挑出最小的
                    mm=mid;
                ri=mid-1;
            }
        }
        printf("%d\n",mm);
    }
    return 0;
}
 

猜你喜欢

转载自www.cnblogs.com/caiyishuai/p/9542460.html