zoukankan      html  css  js  c++  java
  • 分蛋糕(C

    分蛋糕

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/C

    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. Fof 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 ≤ 10000: the number of pies and the number
    of friends.
    • One line with N integers ri with 1 ≤ ri ≤ 10000: 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 oating 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

    题目大意:

    有N块蛋糕,F个朋友,( 1 ≤ N, F ≤ 10000) 。N块蛋糕都是圆柱体且蛋糕的高度都相同,均为1。要把蛋糕平均分给F+1个人,包括我自己,使每个人得到的蛋糕面积都一样(每个人得到的蛋糕是整块的),求每个人可以分到蛋糕面积最多是多少。

    注意:最后输出小数点后4位

    分析:

    1.二分查找。找出蛋糕面积最大时所在的区间

    2. 逐步的缩小范围,最后当左右端点相差小于0.00001时,即可取左端点为最大的面积。

    3. 确定是左是右区间时,可以记录每个蛋糕的可以分的人数的和cnt,比较cnt与F的大小

    4.注意精度输出和类型

    代码:

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<cmath>
     6 using namespace std;
     7 
     8 const int maxn=10001;
     9 const double pi=acos(-1.0);//pi值,注意是double类型,pi 不能直接写3.14
    10 
    11 int n,f;      //蛋糕数量和朋友人数
    12 int r[maxn];      //半径
    13 int sum=0;
    14 double p[maxn],ma;
    15 
    16 double max(int x,int y)//没有max函数会出现 WA
    17 {
    18     return x>y?x:y;
    19 }
    20 
    21 void input()
    22 {
    23     scanf("%d%d",&n,&f);
    24     f=f+1;        //加自己
    25     for(int i=0;i<n;i++)
    26         scanf("%d",&r[i]);
    27     ma=0;
    28     for(int j=0;j<n;j++)
    29     {
    30         p[j]=pi*r[j]*r[j];
    31         ma=max(p[j],ma);//找出面积最大值
    32         sum+=p[j];
    33     }
    34 }
    35 
    36 double search() //找蛋糕面积最大时所在区间
    37 {
    38     double l=0.00001;      //左边最小值
    39     double r=ma;    //右边最大值
    40     double m;    //平均值
    41     while(l+0.00001<r)   //double类型精确度向后估读一位
    42     {
    43         m=(l+r)/2;
    44         int cnt=0;
    45         for(int i=0;i<n;i++)
    46             cnt+=(int)(p[i]/m);//看能分出来多少块蛋糕
    47         if(cnt<f)  //蛋糕数与人数比较
    48             r=m;
    49         else
    50             l=m; 
    51     //    printf("cnt=%d
    ",cnt);
    52     }
    53    
    54     return l;
    55 }
    56 
    57 int main()
    58 {
    59     int t;
    60     scanf("%d",&t);
    61     while(t--)
    62     {
    63         input();
    64         double ans=search();//每人分得的最大面积
    65         printf("%.4lf
    ",ans);
    66     }
    67     return 0;
    68 }
  • 相关阅读:
    转:为什么要有Spring?
    转:jsp与servlet的区别与联系
    转 原生js canvas实现苹果电脑mac OS窗口最小化效果
    JavaFX
    salesforce零基础学习(九十八)Type浅谈
    Salesforce LWC学习(十七) 前端知识之 onclick & onblur & onmousedown
    salesforce零基础学习(九十七)Event / Task 针对WhoId的浅谈
    salesforce零基础学习(九十六)Platform Event浅谈
    Salesforce LWC学习(十六) Validity 在form中的使用浅谈
    Salesforce LWC学习(十五) Async 以及 Picklist 公用方法的实现
  • 原文地址:https://www.cnblogs.com/ttmj865/p/4705956.html
Copyright © 2011-2022 走看看