Pie

31 篇文章 1 订阅
12 篇文章 0 订阅

Pie

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).

Sample Input

3
3 3
4 3 3
1 24
5
10 5
1 4 2 3 4 5 6 5 4 2

Sample Output

25.1327
3.1416
50.2655

题目意思

题目就是过生日朋友来家里吃馅饼,我也要吃,看你怎么分
馅饼不能合到一起,要保证每个人得到的馅饼的体积是一样的
高为1所以就考虑面积就行
第一行输入有多少组数据
每组第一行输入馅饼个数和朋友个数
每组第二行输入每个馅饼的半径

题目分析

如果给一个馅饼就很好分,直接除以人数
要是题目给的第一组数据
3 3
4 3 3
的话就是4个人要去分,先判断最大的4
例如PI为3.1415926吧
体积是第一个 PI * 4 * 4 * 1=50.2654816
第二三个馅饼 PI * 3 * 3 * 1=28.2743334
如果只分第一个另外两个就可惜了
如果是三个人一人28.2743334就可以了但是我也要吃
4个人去分,那就只能最大的分两份,然后其他俩馅饼按那两份的量去切,就均分了,题目大概思路就是这样.题目实现的话就是
二分去判断,然后输出就行

代码实现

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<map>
#include<set> 
#include<string>
#include<stack>
#include<math.h>
using namespace std;
typedef long long ll;
#define PI acos(-1)//这里是math.h库中的函数acos acos(-1)就是π,而且小位数很多的那种
int main()
{
	int t;
	double r[10000],r1;
	cin>>t;
	while(t--)
	{
		int n,f;
		double Max=0;
		cin>>n>>f;
		f+=1;//算上我自己要加一
		for(int i=0;i<n;i++)
		{
			cin>>r1;
			r[i]=PI*r1*r1;//每个都计算体积,高度是1不用乘了
			Max=max(Max,r[i]);//找出最大的馅饼
		}
		double z=0,y=Max,mid;//二分的数,其中mid表示为判断的体积
		int d=0;//人数
		while(y-z>=1e-7)//题目要求小数点4位这里定义比他小的差值就行(1e-6也可以建议比题目多两位)
		{
			mid=(z+y)/2;
			d=0;//每次都初始化人数
			for(int i=0;i<n;i++)
			{
				d+=(int)(r[i]/mid); //人数为整数,直接转换为int型 r[i]/mid就是这个馅饼能分几个人
			}
			if(d>=f)//如果分的人数多了,就说明分小了,左区间变为mid
			{
				z=mid;
			}
			else//分大了不够,右区间变mid
			{
				y=mid;
			}
		}
		printf("%.4lf\n",mid);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值