HDU 4004 The Frog's Games

/*
--------------------------------------------------------------
    stratege : 二分查找 + 贪心
    time     :2012.2.8 20:43 -- 21: 23
    URL      : http://acm.hdu.edu.cn/showproblem.php?pid=4004
*****************************************************************
    Problem : 4004 ( The Frog's Games )     Judge Status : Accepted
    RunId : 5330923    Language : G++    Author : a312745658
*****************************************************************
--------------------------------------------------------------
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std ;

#define MAXN 500005

int L, n, m ;
int a[MAXN], dist[MAXN] ;

int cmp (const void *a, const void *b)
{
    return * (int *)a - * (int *)b ;
}

int success (int jump)      //把长度作为折半查找的尺度
{
    int i, j, cnt ;
    if (jump * m < L)
        return false ;

    i = 1 ;
    j = 0 ;
    cnt = 0 ;
    while (i <= n + 1)
    {
        cnt ++ ;       //统计需要跳的次数
        if (jump < a[i] - a[j])
            return false ;
        while (jump >= a[i] - a[j] && i <= n + 1) //此时正在跳过i--j之间的石头距离
            i ++ ;      //a[i]-a[j] 表示2块石头之间的距离
        j = i - 1 ;         //状态更新,作为下次的起跳地点
    }
    if (cnt > m)    //超过规定的次数
        return false ;
    return true ;
}

int main()
{
    int i, j, k ;
    int max, min, mid ;
    while (scanf ("%d%d%d", &L, &n, &m) != EOF)
    {
        a[0] = 0 ;              //数组a[],记录石头所在位置,需进行排序
        for (i = 1; i <= n; i ++)
            scanf ("%d", &a[i]) ;
        a[n+1] = L ;

        qsort (a, n+2, sizeof(a[0]), cmp) ;
        max = L ;
        min = 0 ;

        while (min <= max)
        {
            mid = (max + min) / 2 ;
            if (success (mid))
                max = mid - 1 ;
            else
                min = mid + 1 ;
        }
        printf ("%d\n", min) ;
    }
    return 0 ;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值