二分法PI

题目原文

My birthday is coming up and traditionally I’m serving pie. Not just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should be one piece of one pie, not several small pieces since that looks messy. This piece can be one whole pie though.

My friends are very annoying and if one of them gets a bigger piece than the others, they start complaining. Therefore all of them should get equally sized (but not necessarily equally shaped) pieces, even if this leads to some pie getting spoiled (which is better than spoiling the party). Of course, I want a piece of pie for myself too, and that piece should also be of the same size.

What is the largest possible piece size all of us can get? All the pies are cylindrical in shape and they all have the same height 1, but the radii of the pies can be different.

input

One line with a positive integer: the number of test cases. Then for each test case:
—One line with two integers N and F with 1 <= N, F <= 10 000: the number of pies and the number of friends.
—One line with N integers ri with 1 <= ri <= 10 000: the radii of the pies.

output

For each test case, output one line with the largest possible volume V such that me and my friends can all get a pie piece of size V. The answer should be given as a floating point number with an absolute error of at most 10^(-3).

题目大意

有N个高度为1,但半径可以不同的蛋糕,要平均分给F+1个人,每一份只能从同一蛋糕内切取,不能从多个蛋糕拼凑成一份,输入N和F,以及N个蛋糕的半径
求每个人可得到的相同的最大蛋糕体积

分析

因为蛋糕高度相同且为1,可以用底面积代替体积,二分法的思想是,对于所求对象的区间进行二分,每一次二分都能对当前的二分对象mid进行判定,能知道是比结果大了,还是比结果小了。大了就取前半段,high=mid-1,小了就取后半段low=mid+1,直到 low>=high
1.确定二分对象及其区间:这里可对面积进行二分,区间可从0最大蛋糕的面积 因为每个人只能从同一蛋糕分,分的面积最大也只能是最大蛋糕的面积。
2.确定判定条件:对于每一次的二分都要知道当前的mid和结果的大小关系,就要一个判定函数,这里可以用每一个蛋糕的面积除以当前的mid,得出的份数相加,和人数F+1相比,比F+1大说明分小了,比F+1小说明分大了,不够分的
3.设置精度:这里是对实数进行二分,所以必须有结束二分的精度,一般是1e-6

#include"stdio.h"
#include"cmath"
const double PI=acos(-1),EPS=1e-6;//pi可用acos(-1)得到精度较大的值 
double pie[10010];
int n,f;
bool check(double k)//判断函数mid 是比结果小了还是大了 
{
	int fen=0,i;
	for(i=0;i<n;i++)fen+=floor(pie[i]/k);//floor 是向下取整函数 
	return fen>=f+1;// 份数比人数大说明mid取小了 
}
int main(){
	int T,i;
	scanf("%d",&T);
	while(T--)
	{	double radi=0,mid,l=0,r=0;
		scanf("%d%d",&n,&f);
		for(i=0;i<n;i++)
		{ scanf("%lf",&radi);
		  pie[i]=radi*radi*PI;
		  if(pie[i]>r)r=pie[i];
		}
		while(fabs(r-l)>=EPS)
		{
			mid=(l+r)/2;
			if(check(mid))//取小了 
			l=mid;
			else r=mid;//取大了 
		}	
		printf("%.4f\n",l);
	}
}
二分法PI二分法PI a1260259056 发布了1 篇原创文章 · 获赞 0 · 访问量 19 私信 关注
上一篇:第1年8月1日 ios yuv pixelbuffer


下一篇:Linux中group文件