zoukankan      html  css  js  c++  java
  • hdu1969Pie(根据体积二分,分馅饼)

    Pie

    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 17056    Accepted Submission(s): 5995


    Problem 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个人分n个圆柱形的馅饼,要求每人分到的馅饼体积相同,可以切割馅饼,但分给每个人的不能是零碎的,分给一个人的只能是从同一块上切下来的。最后输出每个人分到的体积。

    可以根据体积二分来找到答案的体积。体积最小是0,(都没吃到),最大是馅饼总体积/总人数 (理想情况)。每次二分看该体积分的话可以分给多少人,如果可以分的人数大于等于f+1(因为自己也要吃),就把中间值赋给左边界。否则就赋给右边界。一直这样二分下去。直到r-l足够小

    写得有点啰嗦。推荐https://blog.csdn.net/qq_36731677/article/details/54971485

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 double a[10005];
     4 void init()
     5 {
     6     for(int i=0;i<1005;i++)
     7     {
     8         a[i]=0.0;
     9     }
    10 }
    11 int main()
    12 {
    13     int t;
    14     while(~scanf("%d",&t))
    15     {
    16         while(t--)
    17         {
    18             init();
    19             int n,f;double sum=0;
    20             scanf("%d %d",&n,&f);
    21             for(int i=0;i<n;i++)
    22             {
    23                 scanf("%lf",&a[i]);
    24                 a[i]=acos(-1.0)*a[i]*a[i];
    25                 sum=sum+a[i];
    26                 
    27             }
    28             //根据每个人分到的体积二分,最小是0,最大是馅饼总体积/总人数 
    29             double l=0.0,r=acos(-1.0)*sum,mid=(l+r)/2.0;
    30             while(r-l>1e-6)//精度不要设太高,容易t,也不要太低,适中 
    31             {
    32                 int temp=0;
    33                 for(int i=0;i<n;i++)
    34                 {
    35                     temp=temp+(int)(a[i]/mid);
    36                 }
    37                 if(temp>=f+1)
    38                 {
    39                     l=mid;
    40                     mid=(l+r)/2.0;
    41                 }
    42                 if(temp<f+1)
    43                 {
    44                     r=mid;
    45                     mid=(l+r)/2.0;
    46                 }
    47                 
    48             }
    49             printf("%.4lf
    ",mid);
    50         }
    51         
    52     }
    53     return 0;
    54 }
  • 相关阅读:
    Mysql一分钟定位 Next-Key Lock,你需要几分钟
    golang 服务大量 CLOSE_WAIT 故障排查
    Mysql大并发热点行更新的两个骚操作
    golang 服务诡异499、504网络故障排查
    golang 服务平滑重启小结
    vim 列编辑模式
    特邀全球互联网技术大会(麒麟会GITC)分享 —大型团购系统架构设计
    ElasticSearch 评分排序
    zookeeper 实现分布式锁安全用法
    诡异的druid链接池链接断开故障经验总结
  • 原文地址:https://www.cnblogs.com/fqfzs/p/9943907.html
Copyright © 2011-2022 走看看