分蛋糕

来源:互联网 发布:京东面试题java 编辑:程序博客网 时间:2024/04/27 14:15

/*
题意:作者要开一个生日party,他现在拥有n块高度都为1的圆柱形奶酪,已知每块奶酪的底面半径为r不等,
作者邀请了f个朋友参加了他的party,他要把这些奶酪平均分给所有的朋友和他自己(f+1人),
每个人分得奶酪的体积必须相等(这个值是确定的),形状就没有要求。
现在要你求出所有人都能够得到的最大块奶酪的体积是多少?

要求是分出来的每一份必须出自同一个pie,也就是说当pie大小为3,2,1,只能分出两个大小为2的,
剩下两个要扔掉。

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.

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
*/

思路:
分出来的每一份只能出自一个蛋糕,那么分的蛋糕大小一定不能超过最大那个蛋糕的体积。所以二分在(0,max);

#include<stdio.h>#include<string.h>#define PI 3.1415926535897932double s[1500],max;int n,f;double bsearch(){   double l,r,mid;   int ans,i;   l=0;   r=max;   while(r-l>0.000001)   {      ans=0;      mid=(l+r)/2;      for(i=0;i<n;i++)      {         ans=ans+(int)(s[i]/mid);      }      if(ans>=f+1)      {         l=mid;       }       else       {         r=mid;       }    }       return r;  }int main(){    int t,r,i;    scanf("%d",&t);    while(t--)    {      max=0;      memset(s,0,sizeof(s));      scanf("%d%d",&n,&f);      for(i=0;i<n;i++)      {        scanf("%d",&r);        s[i]=PI*r*r;        if(s[i]>max)        {          max=s[i];        }       }      printf("%.4lf\n",bsearch());    }      return 0;  }
0 0
原创粉丝点击