HDU 4004 题解

转载请标明出处:http://blog.csdn.net/Bule_Zst/article/details/76759525


题目:http://acm.hdu.edu.cn/showproblem.php?pid=4004

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.

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

题意:

在一条河上,有很多石头,求出至少需要多大的 跳跃能力(一次可以跳多远)才能在小于等于m次的跳跃后到达对岸

方法:二分

首先求出答案的上界(left)与下界(right),上界就是河的宽度,下界就是相邻石头间的最大间距(因为如果你的跳跃能力小于最大间距,那你是肯定到达不了对岸的)。

之后,求出上界下界的中点(( left + right ) / 2),进行模拟过河(不断累加相邻石头之间的距离,一旦大于你的跳跃能力,就把跳跃次数加一,并重置距离为最后一次相邻石头之间的距离),最后根据过河次数更新上下界(如果次数大于m,就把下界换成mid+1,如果小于等于m,就把上界换成mid),循环直到left>=right,输出答案。

代码:

// @Team    : nupt2017team12
// @Author  : Zst
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
#define LL long long
#define MOD 1000000007
#define CLR(a,x) memset(a,x,sizeof(a))
#define INF 0x3f3f3f3f
#define pb push_back
#define FOR(i,a,b) for( int i = ( a ); i <= ( b ); ++i )

const int N = 500000+7;

int l, n, m;
int store[N];

int test( int ability )
{
    // cout<<ability<<endl;
    int dist = 0;
    int times = 0;
    FOR( i, 1, n+1 ) {
        dist += store[i] - store[i-1];
        if( dist > ability ) {
            times++;
            // cout<<dist<<endl;
            dist = store[i] - store[i-1];
        }
    }
    return times+1;
}


int main()
{
    // freopen( "E.txt", "r", stdin );
    while( scanf( "%d%d%d", &l, &n, &m ) != EOF ) {
        store[0] = 0;
        store[n+1] = l;
        FOR( i, 1, n ) {
            scanf( "%d", store + i );
        }
        sort( store+1, store+n+1 );
        int left = 0;
        int right = 0;
        FOR( i, 1, n+1 ) {
            left = max( left, store[i] - store[i-1] );
        }
        right = l;
        while( left < right ) {
            int mid = left + ( right - left ) / 2;
            if( test( mid ) <= m ) 
                right = mid;
            else 
                left = mid + 1;
            // cout<<left<<" "<<right<<endl;
        }
        printf( "%d\n", left );
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值