您的位置:首页 > 其它

[HDU 4004] The Frog's Games 二分+贪心

2015-08-15 16:44 531 查看
http://acm.hdu.edu.cn/showproblem.php?pid=4004

题意:青蛙通过河中央的n块石头过河, 所有石头均在与河岸垂直的一条线上,给定每块石头到河岸的距离,给出河的宽L,给定青蛙跳的次数上限m,求出青蛙能够过河的最小步长。

思路:二分步长,贪心判断步长是否能过河

[code]#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int len, n, m;
int mapn[500005];

bool check(int dis)
{
    if(m * dis < len)
        return false;
    int step = 0;
    int i = 1, k = 0;
    while(i <= n+1){
        step++;
        if(dis < mapn[i] - mapn[k])
            return false;
        while(dis > mapn[i] - mapn[k] && i <= n+1){  //贪心
            i++;
        }
        k = i - 1;
        if(step > m)
            return false;
    }
    return true;
}

int main()
{
    while(~scanf("%d%d%d", &len, &n, &m)){
        mapn[0] = 0, mapn[n+1] = len;
        for(int i = 1; i <= n; i++){
            scanf("%d", &mapn[i]);
        }
        sort(mapn, mapn+n+2);
        int mid;
        int l = 0, r = len;
        while(l <= r){
            mid = (l + r) >> 1;
            if(check(mid))
                r = mid - 1;
            else
                l = mid + 1;
        }
        printf("%d\n", l-1);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: