二分。依旧相当于求下界。由于答案是浮点数,用while(low<high)极有可能出现死循环,所以用for循环100次,可以保证精确度很高。
一开始WA了好多次,可能是π精确度不够的原因。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <cmath>
#include <algorithm>
#define ll long long
#define INF 2139062143
#define MOD 20071027
#define MAXN 1000005
#define Pi 3.1415926535897932384626433832795
using namespace std;
double compute(double r)
{
return r*r*Pi;
}
vector<double> vec;
int N,F;
bool Judge(double s)
{
int cnt=0;
for(int i=0; i<vec.size(); ++i)
cnt+=(int)(vec[i]/s);
if(cnt<F+1) return false;
return true;
}
double Bsearch(double low,double high)
{
double mid;
for(int i=0; i<100; ++i)
{
mid=(low+high)/2;
if(Judge(mid)) low=mid;
else high=mid;
}
return mid;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&F);
double maxn=0;
vec.clear();
for(int i=0; i<N; ++i)
{
double r;
scanf("%lf",&r);
double s=compute(r);
maxn=max(s,maxn);
vec.push_back(s);
}
double ans=Bsearch(0,maxn);
printf("%.4lf\n",ans);
}
return 0;
}