给N块蛋糕,分给f+1个人要求,尽量大且大小相同,可以浪费也就是可以有蛋糕剩余,那么采用二分,上下界为单个蛋糕体积最大值,0;便可以出解,注意精度问题。
#include<cstdio>
#include<cstdlib>
#define pi 3.1415926535898
#define max(a,b)(a>b?a:b)
const double eps=10e-8;
int dblcmp(double x){
return x<-eps?-1:x>eps;
}
int main(){
int t,n,f;
double r[10005],v[10005];
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&f);
double m=0;
for(int i=0; i<n; i++){
scanf("%lf",&r[i]);
v[i]=r[i]*r[i]*pi;
m=max(v[i],m);
}
double low=0,high=m;
double mid;
while(dblcmp(low-high)<0){
mid=(low+high)/2;
int sum=0;
for(int i=0;i<n; i++)
sum+=(int)(v[i]/mid);
if(sum>=f+1)low=mid;
else high=mid;
}
printf("%.4lf\n",mid);
}
return 0;
}