http://poj.org/problem?id=1064

 

题意:共有n段绳子,要求总共被分为k段。问在符合题意的前提下,每段长最大是多少?

 

 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <math.h>

using namespace std;

#define INF 0x3f3f3f3f
const int maxn = 11000;
typedef long long LL;
double a[maxn];
int n;

int Judge(double x)
{
    int ans = 0;
    for(int i=1; i<=n; i++)
    {
       ans += (int)(a[i]/x);
    }

    return ans;
}
int main()
{
    int  k;

    while(scanf("%d %d", &n, &k)!=EOF)
    {
        double ans = 0;
        for(int i=1; i<=n; i++)
        {
            scanf("%lf", &a[i]);
            ans = max(ans, a[i]);
        }

        double l = 0;
        double r = ans;

        while(r-l>1e-6)
        {
            double mid=(l+r)/2.0;

            if(Judge(mid)>=k) l=mid;
            else r = mid;
        }

        printf("%.2f\n",floor(r*100)/100);
        
        ///%.2f是四舍五入的,floor可以保证只舍不入
    }
    return 0;
}
View Code

 

posted on 2016-08-24 11:03  不忧尘世不忧心  阅读(133)  评论(0编辑  收藏  举报