POJ 1064 Cable master

經典二分

AC代碼:

#include <iostream>
#include <cstdio>  
#include <cstring>  
#include <cstdlib>
#include <cmath>
#include <algorithm>  
using namespace std;  

int n, k;
double a[10005]; 

bool judge(double x)
{
    int cnt = 0;
    for(int i = 0; i < n; i++){
        cnt += (int)(a[i]/x);
        if(cnt >= k)  return true;
    }
    return false;
}

int main()
{
    scanf("%d %d", &n, &k);
    double maxa = 0;
    for(int i = 0; i < n; i++){
        scanf("%lf", &a[i]);
        maxa = max(maxa, a[i]);
    }
    double lb = 0, ub = maxa;
    for(int i = 0; i < 100; i++){
        double mid = (ub + lb)/2.0;
        if(judge(mid))  lb = mid;
        else ub = mid;
    }
    printf("%.2f", floor(ub*100)/100);
    return 0;
}
發佈了46 篇原創文章 · 獲贊 3 · 訪問量 9263
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章