分馅饼 Pie

Pie

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

题目:

 

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

 

题意:

有n个馅饼,有f个朋友,接下来是n个馅饼的半径。然后是分馅饼,
要求大家都要一样大,形状没什么要求,但都要是一整块的那种,也就说不能从两个饼中
各割一小块来凑一块。
题目要求我们分到的饼尽可能的大。

分析:
用二分法查找,
把0设为l,把y设为r。mid=(l+r)/2;
以mid为标志,如果所有的饼可以分割出f+1《还有自己》个mid,那么返回1,说明每个人可以得到的饼的体积可以
大于等于mid;如果不能分出这么多的mid,那么返回0,说明每个人可以得到饼的体积小于mid。
 注意精度
 代码:

复制代码
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6  double pi=acos(-1.0);        //用反余弦求pi
 7 const int maxn=10010;
 8 double sum,y;
 9 int n,a[maxn],f;
10 double b[maxn];
11 int xz(double m)
12 {
13     int x=0,i;
14    for(i=0;i<n;i++)
15   {
16      x+=(int)(b[i]/m);
17   }
18      if(x>=f+1)
19      return 1;
20         else return 0;
21  }
22   void slove()
23  {
24    double l=0.0,r=y,mid=0;
25        while((r-l)>1e-6)                    //精度问题
26       {  
27          mid=(l+r)/2;
28         if(xz(mid)) l=mid;
29          else r=mid;
30      }
31 printf("%.4f\n",l);
32 }
33 int main()
34  {
35     int i,t;
36     cin>>t;
37       while(t--)
38      {
39        sum=0.0;
40         cin>>n>>f;
41            for(i=0;i<n;i++)
42           {
43             cin>>a[i];
44            b[i]=a[i]*a[i]*pi;
45            sum+=b[i];
46           }
47         y=sum/(f+1);
48         slove();
49     }
50 return 0;
51 }
复制代码

 


 
posted @   枫虹  阅读(385)  评论(0编辑  收藏  举报
编辑推荐:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
点击右上角即可分享
微信分享提示