分披萨

题意:

   My birthday is coming up and traditionally I’m serving pie. Not just one pie, no, I have a numberN of them,

of various tastes and of various sizes. Fof my friends are coming to my party and each ofthem gets a piece

of pie. This should be one pieceof one pie, not several small pieces since that looksmessy. This piece can be one whole pie though.My friends are very annoying and if one of themgets a bigger piece than the others, they start complaining.Therefore all of them should get equallysized (but not necessarily equally shaped) pieces,even if this leads to some pie getting spoiled (whichis better than spoiling the party). Of course, I wanta 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 andthey 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

思路:二分。根据每个人所得的披萨在最小份—所有的的披萨平均后之间,那么就可以利用二分法来从中间找

源代码:

 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 #include<cmath>
 5 using namespace std;
 6 #define maxn 10000+5
 7 double pi = acos(-1.0);    //π
 8 int  N, F,rid[maxn];
 9 double v[maxn];
10 double vi,mk,left, right, mid;
11 bool judge(double M)
12 {
13     int sum = 0;
14     for (int i = 0; i < N; i++)
15         sum += int(v[i]/M);      //能分到的人数
16     if (sum>=F)                 //与实际人数做比较
17         return true;
18     else
19         return false;
20 
21 }
22 int main()
23 {
24     int T;
25     cin >> T;
26     while (T--)
27     {
28         memset(v, 0, sizeof(v));
29         memset(rid, 0, sizeof(rid));
30         double left, right, mid;
31         cin >> N >> F;
32         F = F + 1;                 //加上自己
33         for (int i = 0; i < N; i++)
34         {
35             cin >> rid[i];
36             v[i] = pi*rid[i] * rid[i];
37             vi += v[i];
38         }
39         mk = vi / F;               //
40         left = 0.0;
41         right = mk;
42         while ((right - left)>1e-6)  //控制精度
43         {
44             mid = (left + right) / 2;
45             if (judge(mid))            //判断是大了还是小了
46                 left = mid;            //大了
47             else
48                 right = mid;           //小了
49         }
50         printf("%.4f
", mid);
51 
52     }
53 
54     return 0;
55 
56 }
原文地址:https://www.cnblogs.com/Lynn0814/p/4716038.html