NBUT-2014暑期集训专题练习1 -> 二分法 J - J

来源:互联网 发布:完美告白知乎 编辑:程序博客网 时间:2024/06/14 02:58

Description

NBUT-2014暑期集训专题练习1 - 二分法  J - J - 深海灬孤独 - AlexMy 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.


标准的二分,注意精度,

首先用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;
}


0 0