zoukankan      html  css  js  c++  java
  • hdu 4004 二分查找

    直接二分查找答案即可,我的判断函数没有像大牛们那样优化,但是过是没问题的~

    /*
    * hdu4004/linux.cpp
    * Created on: 2011-9-4
    * Author : ben
    */
    #include
    <cstdio>
    #include
    <cstdlib>
    #include
    <cstring>
    #include
    <cmath>
    #include
    <algorithm>
    using namespace std;

    const int MAXN = 500010;
    int stone[MAXN], L, N, M;

    int step(int dis) {
    int ret, cur, next;
    next
    = ret = cur = 0;
    while (cur <= N) {
    next
    = cur + 1;
    while (next <= N + 1 && stone[next] - stone[cur] <= dis) {
    next
    ++;
    }
    if (cur == next - 1) {
    return 0x7fffffff;
    }
    cur
    = next - 1;
    ret
    ++;
    }
    return ret;
    }

    void work();
    int main() {
    #ifndef ONLINE_JUDGE
    freopen(
    "data.in", "r", stdin);
    #endif
    work();
    return 0;
    }

    void work() {
    int mid, high, low, ans;
    while (scanf("%d%d%d", &L, &N, &M) == 3) {
    stone[
    0] = 0;
    for (int i = 1; i <= N; i++) {
    scanf(
    "%d", &stone[i]);
    }
    stone[N
    + 1] = L;
    sort(stone, stone
    + N + 2);
    low
    = 0;
    high
    = L;
    while (low <= high) {
    mid
    = (low + high) / 2;
    if (step(mid) > M) {
    low
    = mid + 1;
    }
    else {
    high
    = mid - 1;
    ans
    = mid;
    }
    }
    printf(
    "%d\n", ans);
    }
    }
  • 相关阅读:
    oracle 存储过程
    交错数组
    延迟加载
    js 闭包
    引用类型和值类型
    事务
    web api 之身份验证
    SQLServer中的服务器角色与数据库角色
    按照某一字段的相同值合并所对应的行的值
    VC工程中的字符集工程属性和字符编码(转)
  • 原文地址:https://www.cnblogs.com/moonbay/p/2166648.html
Copyright © 2011-2022 走看看