Description

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 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
标准的二分,注意精度,
首先用mid去判断,每个蛋糕切分后,每一份为mid,最多切分(int)(area/mid)份,这是一份蛋糕的,那么所有的就是求和,然后再和F+1(注意是F+1,自己也要的啊)
比较,如果大于等于F+1,那么说明每人拿mid是可以的,答案或许会更大,
如果小于F+1,那么说明mid太大了,这就是二分的细节了
#include<stdio.h>
#include<math.h>
const double PI=acos(-1.0);
double area[10010];//蛋糕面积
int F,n;
bool judge(double each_area)
{
int seg=0;
for(int i=0;i<n;i++)
seg+=(int)(area[i]/each_area);//把每块蛋糕可以分成的份数相加,注意
return seg>=F+1;//别忘了自己
}
void huafeng(double low,double high)
{
double mid;
while(high-low>1e-6)
{
mid=(low+high)/2;
if(judge(mid))
low=mid;
else
high=mid;
}
printf("%.4f\n",mid);
}
int main()
{
int t;
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%d%d",&n,&F);
int r;
double max=-1;
for(int i=0;i<n;i++)
{
scanf("%d",&r);
area[i]=(PI*r*r);
if(area[i]>max)
max=area[i];
}
huafeng(0,max);//每个人最多拿到的蛋糕不会大于面积最大的蛋糕,所以 //可以做上限
}
}
return 0;
}