Pie(poj 3122)
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
翻译:
我的生日快到了,按惯例我要做派。不只是一个馅饼,不,我有N个馅饼,有不同的口味和大小。我的朋友要来参加我的聚会,他们每个人都得到一块派。这应该是一块馅饼,而不是几块小馅饼,因为那样看起来很乱。这一块可以是一整块派。
我的朋友们都很烦人,如果他们中的一个人得到的比其他人大,他们就会开始抱怨。因此,所有的馅饼都应该是大小相等(但不一定是形状相等)的,即使这会导致一些馅饼被破坏(这比破坏聚会要好)。当然,我也想要一块馅饼给自己,那块馅饼也应该是同样大小的。
我们所有人能得到的最大尺寸是多少?所有的馅饼都是圆柱形的,它们都有相同的高度h为1,但是馅饼的半径可以不同。
输入
一行是正整数:测试用例的数量。然后对于每个测试用例:
一行有两个整数N和F,其中1≤N, F≤10000:馅饼的数量和朋友的数量。
一行有N个整数ri,其中1≤ri≤10000:饼的半径。
输出
对于每个测试用例,输出一行最大的体积V,这样我和我的朋友都可以得到一个大小为V的饼块。答案应该是一个绝对误差不超过10−3的浮点数。
样例输入
3.
3 3
4 3 3
1 24
5
10 5
1 4 2 3 4 5 6 5 4 2
样例输出
25.1327
3.1416
50.2655
题目分析:
类型:浮点数二分
我们需要找到它的上下边界
#include<iostream> using namespace std; const double pi = 3.14159265359; const double esp = 1e-6; int T, N, F; double a[10005], l, r, mid; int main() { scanf("%d", &T);//T个测试样例 while (T -- ) { scanf("%d%d", &N, &F); F ++ ;// m + 1个人 因为还包括 "我" r = -1.0;//先让右边界为-1 for (int i = 0; i < N; i ++ )//对于N个饼 { scanf("%lf", &a[i]);//输入半径 a[i] = a[i] * a[i] * pi;//计算圆柱体饼的体积并放入a[] r = max(r, a[i]);//更新右边界 } l = 0.0, mid = 0.0; while(r - l > esp)//左边界l是体积为0的饼 右边界r是最大的那块饼 { mid = (l + r) / 2; //mid表示的就是答案 最后要返回的就是mid // 每个人所能分到的最大的饼的体积肯定是在0到最大的那块饼干 int cnt = 0; for(int i = 0; i < N; i ++ )//对N个饼 cnt += (int)(a[i] / mid);//cnt是全部派能分size个小派的(整数)总和 //这里的size指的就是mid if (cnt >= F) l = mid; //cnt >= F表示够分 else r = mid; } printf("%.4f\n", mid); //注意是%.4f不是%.4lf } }