• POJ 3122 pie (二分法)


    Description

    My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should be one piece of one pie, not several small pieces since that looks messy. This piece can be one whole pie though. 

    My friends are very annoying and if one of them gets a bigger piece than the others, they start complaining. Therefore all of them should get equally sized (but not necessarily equally shaped) pieces, even if this leads to some pie getting spoiled (which is better than spoiling the party). Of course, I want a piece of pie for myself too, and that piece should also be of the same size. 

    What is the largest possible piece size all of us can get? All the pies are cylindrical in shape and they all have the same height 1, but the radii of the pies can be different. 
     

    Input

    One line with a positive integer: the number of test cases. Then for each test case: 
    ---One line with two integers N and F with 1 <= N, F <= 10 000: the number of pies and the number of friends. 
    ---One line with N integers ri with 1 <= ri <= 10 000: the radii of the pies. 
     

    Output

    For each test case, output one line with the largest possible volume V such that me and my friends can all get a pie piece of size V. The answer should be given as a floating point number with an absolute error of at most 10^(-3).
     

    Sample Input

    3
    3 3
    4 3 3
    1 24
    5
    10 5
    1 4 2 3 4 5 6 5 4 2
     

    Sample Output

    25.1327
    3.1416
    50.2655

    题目意思:我过生日请了f 个朋友来参加我的生日party,m个蛋糕,我要把它平均分给每个人(包括我),并且每个人只能从一块蛋糕得到自己的那一份,并且分得的蛋糕大小要一样,形状可以不一样,每块蛋糕都是圆柱,高度一样。

     1 #include<cstdio>
     2 #include<math.h>
     3 double pi = acos(-1.0);
     4 double sum,mid,le,ri,b[10000+11],max0;
     5 int n,f,r;
     6 double fl(double x)
     7 {
     8     int su=0;
     9     for(int i = 0 ; i < n ; i++)
    10     {
    11         su+=(int)(b[i]/x);
    12     }
    13     return su;
    14 }
    15 int main()
    16 {
    17     int t;
    18     scanf("%d",&t);
    19     while(t--)
    20     {
    21         sum=0;
    22         scanf("%d %d",&n,&f);
    23         f++;
    24         for(int i = 0 ; i < n ; i++)
    25         {
    26             scanf("%d",&r);
    27             b[i]=r*r*pi;
    28             sum+=b[i];
    29         }
    30         sum=sum/f;
    31         le=0.0;
    32         ri=sum;
    33         while(fabs(ri-le)>1e-6)
    34         {
    35             mid=(le+ri)/2;
    36             if(fl(mid) >= f)
    37             {
    38                 le=mid;
    39             }
    40             else
    41             {
    42                 ri=mid;
    43             }
    44         }
    45         printf("%.4f
    ",le);
    46     }
    47 }
     
     
     
     
     
    ——将来的你会感谢现在努力的自己。
  • 相关阅读:
    基础总结篇之一:Activity生命周期
    putExtra方法
    fork函数
    linux中fork()函数详解(原创!!实例讲解) (转载)
    系统调用wait、waitpid和exec函数
    版本控制工具VSS使用介绍
    学习进度条六(第七周)
    阶段冲刺4
    阶段冲刺3
    阶段冲刺2
  • 原文地址:https://www.cnblogs.com/yexiaozi/p/5710982.html
Copyright © 2020-2023  润新知