HDU4004The Frog's Games(二分求恰当的步长)

The Frog's Games
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 4167    Accepted Submission(s): 2029
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
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


 
Source
The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  4001 4007 4006 4003 4008 
题意:一只青蛙凭借横向散放在 l 宽度的河中的n块小石块渡河,要求在m步以内到达河对岸,问青蛙一步至少要跳多远

思路:二分求恰当的步长

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[500005];

int main()
{
    int l,n,m;


    int maxs;
    while(~scanf("%d%d%d",&l,&n,&m))
    {
        maxs=0;
        for(int i=1; i<=n; i++)
            scanf("%d",&a[i]);
        a[0]=0;
        a[n+1]=l;
        sort(a+1,a+n+2);   //这样更好的理解
        for(int i=n+1; i>=1; i--)
        {
            if(a[i]-a[i-1]>maxs)
                maxs=a[i]-a[i-1];
        }
        int left=maxs;
        int right=l;
        int mid;
        while(left<=right)
        {
            int t=0;   //跳的步数
            int nodes=0;  //跳到的点,从0点开始
            mid=(left+right)/2;  //也可以表示为mid=(left+right)>>1,  >>n 表示算术右移n位,数值除以2^n
            for(int i=1; i<=n; i++)
            {
                if(a[i]-a[nodes]<=mid&&a[i+1]-a[nodes]>mid)
                {
                    t++;
                    nodes=i;
                }
            }
            t++;
            if(t<=m)
                right=mid-1;
            else
                left=mid+1;
        }
        printf("%d ",left);     //强行要写left,,可能是要求的是至少的步长,喜欢写mid,不过大多都是写left,不太懂呀
    }
    return 0;
}

原文地址:https://www.cnblogs.com/dshn/p/4750781.html