AOJ.850 电缆公司的烦恼 (二分+枚举)

AOJ.850 电缆公司的烦恼 (二分+枚举)

题意分析

从[1,average]二分枚举长度即可,由于保留2位小数,可以将数据扩大10^2倍后后枚举,输出时除100即可。

代码总览

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#define INF 0x3f3f3f3f
#define nmax 100000
#define MEM(x) memset(x,0,sizeof(x))
int a[nmax];
using namespace std;
int main()
{
    //freopen("in.txt","r",stdin);
    int n,k;
    scanf("%d %d",&n,&k);
    {
        double hh;
        int r = -INF;
        for(int i  = 0; i<n ;++i){
            scanf("%lf",&hh);
            a[i] = hh* 100;
            r = max(r,a[i]);
        }
        r++;
        int l = 0;
        int temp  = 0,mid = 0;
        while(l+1<r){
            temp = 0;
            mid = (l+r)/2;
            //printf("%d %d %d/n",l,r,mid);
            for(int i =0; i<n;++i){
                temp+= a[i]/mid;
            }
            if(temp<k) r = mid;
            else l = mid;
        }
        printf("%.2f\n",l*1.0/100);
    }
    return 0;
}
posted @ 2017-04-28 21:26  pengwill  阅读(161)  评论(0编辑  收藏  举报