My birthday is coming up and traditionally I'm serving pie. Not justone pie, no, I have a number N of them, of various tastes andof various sizes. F of my friends are coming to my party andeach 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 onewhole pie though.
My friends are very annoying and if one of them gets a bigger piecethan the others, they start complaining. Therefore all of them shouldget equally sized (but not necessarily equally shaped) pieces, even ifthis leads to some pie getting spoiled (which is better than spoilingthe party). Of course, I want a piece of pie for myself too, and thatpiece should also be of the same size.
What is the largest possible piece size all of us can get? All thepies are cylindrical in shape and they all have the same height 1,but the radii of the pies can be different.
One line with a positive integer: the number of test cases. Thenfor each test case:
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 numberwith an absolute error of at most 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
我们要做的就是求出, 每个人最大能分到多大块的派呢?
做法:
二分法求解!!
一开始的每块大小的边界是: 最小为0, 最大的为 把总面积加起来除以人数(最理想的情况)
然后题目要求误差不超过 10的负三次方~ (这算是二分递归的边界了)
AC代码:
#include<stdio.h> #include<math.h> const double PI = acos(-1); int n, f; int trueF; double R[12345]; double search(double l, double r) { if(r-l < 1e-6) return l; double mid = (l + r) / 2.0; int tSumF = 0; //临时参数, 表示这种情况下饼能分给几个人 for(int i = 0; i < n; i++) tSumF += R[i] / mid; if(tSumF >= trueF) //能分更多的人, 表明饼切小了 return search(mid, r); else //这是切大了 return search(l, mid); } int main() { int T; scanf("%d", &T); while(T--) { scanf("%d %d", &n, &f); trueF = f + 1; //真正的人数加上自己~ double sumR; //表示总大小 for(int i = 0; i < n; i++) { scanf("%lf", &R[i]); R[i] = R[i] * R[i] * PI; sumR += R[i]; } printf("%.4lf\n", search(0, sumR/trueF)); } return 0; }