POJ 3122 Pie(实数二分)

题目链接:点击这里
在这里插入图片描述
在这里插入图片描述
精度设置为 1 0 8 10^{-8} 会超时。

#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>

using namespace std;
typedef long long ll;
const int MOD = 10000007;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-7;
const int maxn = 10010;

int t, n, f, r;
double maxx, area[maxn];

//当前mid小于等于时返回true,当前mid大于时返回false 
bool check(double mid)
{
	int sum = 0;
	for(int i = 0; i < n; i++)		//把每个圆蛋糕都按mid大小分开,统计总数
		sum += (int)(area[i]/mid);
	if(sum >= f)	return true;	//最后看总数够不够f个
	else	return false;
}

int main()
{
	scanf("%d", &t);
	while(t--)
	{
		scanf("%d%d", &n, &f);
		f = f + 1;
		for(int i = 0; i < n; i++)
		{
			scanf("%d", &r);
			area[i] = PI*r*r;
			if(area[i]>maxx)	maxx = area[i];		//记录最大值 
		}
		
		double left = 0, right = maxx;
		while(right-left > eps)
		{
			double mid = left+(right-left)/2;
			if(check(mid))	left = mid;
			else	right = mid;
		}
		printf("%.4f\n", left);		//打印right也对
	}
	return 0;
}
发布了690 篇原创文章 · 获赞 103 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/qq_42815188/article/details/104070246
今日推荐