poj 3122 Pie
#include <iostream> //二分查找
#include <algorithm>
#include <math.h>
using namespace std;
# define pi 3.1415926535898
#define precisions 1e-5
bool cmp(const double& a,const double& b)
{
return a>b;
}
int piece(double total,double a)
{
int m=0;
while(total>=a)
{
m++;
total-=a;
}
return m;
}
int main()
{
int c,n,f,i,r;double pie[10010];
scanf("%d",&c);
while(c--)
{
scanf("%d%d",&n,&f);
int max=0;
for(i=0;i<n;++i)
{
scanf("%d",&r);
if(max<r)
max=r;
pie[i]=pi*r*r;
}
sort(pie,pie+n,cmp);
double begin=0,end=pi*max*max,mid;
while(end-begin>precisions)
{
mid=(begin+end)/2;
int count=0;
for(i=0;i<n;++i)
{
if(pie[i]<mid)
break;
count+=piece(pie[i],mid);
if(count>=f+1)
break;
}
if(count>=f+1)
begin=mid;
else
end=mid;
}
printf("%.4f\n",mid);
}
return 0;
}