TOJ 1041 pie(二分)

描述

 

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.

输入

 

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.

输出

 

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 rounded to six floating point.

样例输入

 

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

样例输出

 

25.132741
3.141593
50.265482

题意

给n个蛋糕,f+1个人分,每个人分到的蛋糕必须相同,并且必须只来自一种蛋糕。求最大能分到的蛋糕体积

题解

找到最大的蛋糕进行二分,每次求最大能分给几人,如果大于f+1则说明够分l=mid,反之r=mid

代码

 1 #include"stdio.h"
 2 #include"math.h"
 3 #include"algorithm"
 4 using namespace std;
 5 const double pi=acos(-1.0);
 6 int n,f;
 7 double a[10005];
 8 int cmp(double a,double b)
 9 {
10     return a>b;
11 }
12 int check(double x)
13 {
14     int i,sum=0;
15     for(i=0;i<n;i++)
16     {
17         sum+=(int)(a[i]/x);
18         if(sum>=f)
19             return 1;
20     }
21     return 0;
22 }
23 int main()
24 {
25     int t,i;
26     double x;
27     scanf("%d",&t);
28     while(t--)
29     {
30         scanf("%d %d",&n,&f);
31         f++;
32         for(i=0;i<n;i++)
33         {
34             scanf("%lf",&x);
35             a[i]=x*x*pi;
36         }
37         sort(a,a+n,cmp);
38         double r=a[0],l=0,mid;
39         if(f<n) n=f;
40         while(l+1e-7<r)
41         {
42             mid=(l+r)/2;
43             if(check(mid))
44                 l=mid;
45             else
46                 r=mid;
47         }
48         printf("%.6f\n",l);
49     }
50 }

猜你喜欢

转载自www.cnblogs.com/xmmq/p/9594180.html
今日推荐