zoukankan      html  css  js  c++  java
  • POJ 1064 Cable master 二分

      题目链接: http://poj.org/problem?id=1064

      题目描述: 问给出n段长度为a[i]的绳子,  要切成K段, 问每段最长是多少

      解题思路: 二分很容易想啊......

      代码: 

    #include <cstdio>
    #include <cmath>
    using namespace std;
    const int maxn = 10005;
    const double inf = 200005.0;
    double a[maxn];
    const double eps = 1e-5;
    int n,k;
    bool ok(double x) {
        int num = 0;
        for(int i = 0; i < n; i++)
            num += (int)(a[i]/x);
        return num >= k;
    }
    void solve() {
        double low = 0;
        double high = inf;
        while( high-low > eps ) {
            double mid = (high+low)*0.5;
            if( ok(mid)) low = mid;
            else high = mid;
        }
        printf( "%.2f
    ", floor(high*100)/100 );
    }
    int main() {
        while( scanf( "%d%d", &n, &k ) == 2 ) {
            for( int i = 0; i < n; i++ ){
                scanf( "%lf", a+i );
            }
            solve();
        }
        return 0;
    }
    View Code

      思考: 我知道这是二分啊.....但是, 但是, 我又写搓了! 然后调了好久, 删了重写过了.....迷, 今天下午面试啊, fighting!

  • 相关阅读:
    webpack基本使用
    vue-路由-显示名称
    vue-父组件和路由
    vue-路由
    vue-父子组件和ref
    vue-组件
    go-面向对象编程(上)
    JavaScript的历史
    vue-列表动画
    钩子函数实现小球弹落
  • 原文地址:https://www.cnblogs.com/FriskyPuppy/p/7484032.html
Copyright © 2011-2022 走看看