HDU 4004 The Frog‘s Games(二分)

The Frog's Games

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



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




题意:有一只青蛙,要过一条河,河宽为L,河中间有n块石头,给出每块石头距青蛙一侧河岸的距离,青蛙只能踩在石头上,不能落入水中,青蛙最多能跳m次,问青蛙的弹跳能力最少为多远。

分析:
只需在所给的各石头的位置左边加一块位置为0的石头,在右边加一个位置为L的石头,这样就可以算出n+2块石头相邻两块之间的间距,然后就把这个问题转化成了最大值最小化的问题。同样,记在相邻两石块间隔的最大值为max,那就只需在[max,L]之间二分枚举青蛙的弹跳能力。




AC代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#define INF 0x7fffffff
#define  LL  long long
using namespace std;

int L,n,m;
int a[500001],b[500001];

bool ok(int x)
{
    int s=0,t=1;
    for(int i=0;i<=n;i++)
    {
        s+=b[i];
        if(s>x)
        {
            t++;
            s=b[i];
        }
    }
    if(t>m)  return  false;
    return  true;
}

int main()
{
    while(scanf("%d%d%d",&L,&n,&m)!=EOF)
    {
        a[0]=0;
        a[n+1]=L;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        sort(a,a+n+2);
        int max=0;
        for(int i=0;i<n+1;i++)
        {
            b[i]=a[i+1]-a[i];
            if(max<b[i])  max=b[i];
        }
        if(m>n)  { printf("%d\n",max); continue; }
        int l=max,r=L;
        while(l<r)
        {
            int mid=(l+r)/2;
            if(!ok(mid)) l=mid+1;
            else  r=mid;
        }
        printf("%d\n",r);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值