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\n", 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!

posted on 2017-09-06 12:05  FriskyPuppy  阅读(130)  评论(0编辑  收藏  举报

导航