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!

  • 相关阅读:
    jqmodal遮罩层的实现
    让Editplus和SVN集成
    asp.net很有用的字符串操作类
    TCP socket编程
    Adroid: ProgressBar 的使用
    在想的事情......
    I'm new to CNBlogs!
    压力
    开心工作
    feature
  • 原文地址:https://www.cnblogs.com/FriskyPuppy/p/7484032.html
Copyright © 2011-2022 走看看