Pie POJ - 3122 二分

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#define pi acos(-1.0)
using namespace std;
const int maxn = 10005;
int r[maxn];
double v[maxn];
int n,f;
bool check(double x)
{
	int cnt = 0;
	int t = lower_bound(v,v + n,x) - v;
	for(int i = t; i < n; i ++)
		cnt += (int)(v[i] / x);
	if(cnt >= f+1)
		return true;
	else
		return false;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>f;
		for(int i = 0; i < n; i ++)
		{
			cin>>r[i];
			v[i]= pi * r[i] * r[i] * 1;
		}
		if(n == 1)
			printf("%.4lf\n",v[0] / (f + 1));
		else
		{
			sort(v,v + n);
			double l = 0.0,r = v[n-1];
			while(l + 1e-4 < r)
			{
				double mid = (l + r) / 2;
				if(check(mid))
					l = mid;
				else
					r = mid;
			}
			printf("%.4lf\n",l);
		}
	}
	return 0;
}
posted @ 2020-05-07 01:20  晴屿  阅读(79)  评论(0编辑  收藏  举报