POJ 3122 Pie(二分答案)

来源:互联网 发布:执业兽医资格考试软件 编辑:程序博客网 时间:2024/05/20 09:44

this is the question
to solve this , we should use some method called binary search the solution , when we examine one possible answer , we see if the ans ( pie size) and be depart to all the friends.

#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#define ll long long#define eps 1e-9#define trace printf("here\n");using namespace std;const int maxn = 100005;const double PI = acos(-1.0);int n,m;double pie[maxn];bool check(double x){    int cnt = 0;    for(int i=0;i<n;i++)    {        cnt+=(int)floor(pie[i]/x);    }    //cout<<cnt<<endl;    if(cnt<m) return false;    else return true;}int main(){    int ca = 0;    scanf("%d",&ca);    while(ca--)    {        scanf("%d%d",&n,&m);        m++;        double mn = 0;        double sum = 0;        for(int i=0;i<n;i++)        {            int r;            scanf("%d",&r);            pie[i] = r*r*PI;            sum+=pie[i];            mn = max(pie[i],mn);        }        //sort(pie,pie+n);//        for(int i=0;i<n;i++)//        {//            cout<<pie[i]<<endl;//        }        double l = mn/m ,r = sum/m ;        while( l+0.00001 < r )        {            double mid = (l+r)/2;            if(check(mid)) l =mid;            else r = mid;        }        printf("%.4f\n",l);    }    return 0;}
原创粉丝点击