Problem C - Pie
Time limit: 1 second
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.
Input
One line with a positive integer: the number of test cases. Thenfor each test case:
- One line with two integers N and F with 1 ≤N, F ≤ 10000: the number of pies and the number offriends.
- One line with N integers ri with 1 ≤ri ≤ 10000: 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 numberwith 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
题意:
主人开Party, 厨房有n个派, 邀请了f个小伙伴, 加上自己一共是f+1个小伙伴
然后, 主人分发派, 但是每个人要分到相同大小的派, 且只能是一整块~(一块完完整整的挖掉一小块也算一整块, 但是不许两块及两块以上拼凑)
我们要做的就是求出, 每个人最大能分到多大块的派呢?
做法:
二分法求解!!
一开始的每块大小的边界是: 最小为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;
}