这是一道贪心的题目,网上说了很多关于它在精度方面的问题,但我想说的是,这题同样的代码,用g++提交要TLE,用c++提交63ms
思路很简单,二分即可,但要注意的是分给每个人的派形状可以不同,不一定非要是圆柱体,所以二分的时候,直接拿中间的平方值去除半径的平方就可以得到派的数量。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const double PI= 3.14159265358979323846264338327950288;
const int maxn=10000+100;
double pies[maxn];
double r;
int n,f;
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>f;//包括主人应该有f+1个人
f++;
double maxs=0.0;
for(int i=0;i<n;i++)
{
cin>>r;
pies[i]=r*r;
maxs=max(pies[i],maxs);
}
double low=0.0,high=maxs,mid;//m是下界,n是上界,二分~~
while(high-low>1e-7)
{
mid=(high+low)/2;
int cnt=0;
for(int i=0;i<n;i++)
{
cnt+=(int)pies[i]/mid;
}
if(cnt>=f) low=mid;
else high=mid;
}
printf("%.4lf\n",mid*PI);
}
return 0;
}