poj3122 Pie (two points)

Topic link: https: //vjudge.net/problem/POJ-3122

The meaning of problems: there are a cake n, m + 1 individual, the cake was divided into blocks m + 1, find the maximum size of each block.

Thinking: water-half the size of each block and obviously many people can eat a monotone meet the dichotomous properties. A lower limit of 0 and an upper limit sum / (m + 1).

AC Code:

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

const int maxn=1e4+5;
const double PI=acos(-1);
int T,n,m;
double a[maxn],sum;

int check(double x){
    int num=0;
    for(int i=n;i>=1;--i){
        num+=a[i]/x;
        if(num>m) break;
    }
    return num;
}

int main(){
    scanf("%d",&T);
    while(T--){
        sum=0;
        scanf("%d%d",&n,&m);
        m+=1;
        for(int i=1;i<=n;++i){
            scanf("%lf",&a[i]);
            a[i]=PI*a[i]*a[i];
            sum+=a[i];
        }
        sort(a+1,a+n+1);
        double l=0.0,r=sum/m,mid;
        while(l<=r){
            mid=(l+r)/2;
            if(check(mid)>=m) l=mid+(1e-5);
            else r=mid-(1e-5);
        }
        printf("%.4f\n",r);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/FrankChen831X/p/11411885.html