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

博客围绕青蛙游戏展开,给出了时间和内存限制、提交与通过情况。输入包含多组案例,每组有三个正整数及后续距离信息,要求输出青蛙至少应具备的能力值。涉及Java算法相关内容。

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

 

 

转载于:https://www.cnblogs.com/caiyishuai/p/9542460.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值