Pie
Description
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个不同口味、不同大小的派。 有F个朋友会来参加我的派对,每个人会拿到一块派(不能由几个派的小 块拼成;可以是一整个派)。 所有人拿到的派必须是同样大小的(但不需要是同样形状的)。 当然,我也要给自己留一块,而这一块也要和其他人的同样大小。 每个派都是一个高为1,半径不等的圆柱体。 请问我们每个人拿到的派最大是多少?
思路:
为保证精度不丢失,用一维数组存储每块蛋糕的半径平方。采用二分,0为二分下限,最大蛋糕半径的平方为二分上限。二分查找可以分得足够蛋糕分数的切割上限。
题目挖坑:
圆周率必须足够精确,否则WA,这里取3.14159265359。
AC代码:
#include <iostream>
#include <algorithm>
using namespace std;
#define pi 3.14159265359//圆周率
double esp=1e-6;
double pie[100000];//半径平方的数组
void solve(int n,int f){
double l=0.0,r=-1.0,mid;
for(int i=0;i<n;i++){
cin>>pie[i];//输入半径
pie[i]*=pie[i];
r=max(r,pie[i]);//最大半径平方
}
while(r-l>esp){//二分循环
mid=(r+l)/2;//二分值
int all=0;
for(int i=0;i<n;i++){
all+=int(pie[i]/mid);//切割份数
}
if(all>=f) l=mid;//切割份数大于需求值,可增大下限
else r=mid;//减小上限
}
printf("%.4lf\n",r*pi);//输出
}
int main(){
int t,n,f;
cin>>t;
while(t--){
cin>>n>>f;
f++;
solve(n,f);
}
return 0;
}
/*
3
3 3
4 3 3
1 24
5
10 5
1 4 2 3 4 5 6 5 4 2
*/