Pie
Time Limit : 5000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 192 Accepted Submission(s) : 43
Problem 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.<br><br>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. <br><br>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.<br>
Input
One line with a positive integer: the number of test cases. Then for each test case:<br>---One line with two integers N and F with 1 <= N, F <= 10 000: the number of pies and the number of friends.<br>---One line with N integers ri with 1 <= ri <= 10 000: the radii of the pies.<br>
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<br>3 3<br>4 3 3<br>1 24<br>5<br>10 5<br>1 4 2 3 4 5 6 5 4 2<br>
Sample Output
25.1327<br>3.1416<br>50.2655<br>
Source
NWERC2006
题目要求:给你几个pi和其尺寸,还有要分的人数,让你求出每人分的最大面积(每人只能得到一块)
思路:这是稍微变形的二分法,先求出每人平均分的最大面积,然后根据二分得到的份数和人数比较进行校正,最后得到近似解。
注意事项:输出结果的小数保留。
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
#define pi 3.14159265358979;
int main()
{
vector<double> a;
int n,f,m;
double b,e,mid,sum,r,s;
scanf("%d",&n);
while(n--)
{
sum=0;
scanf("%d%d",&m,&f);
while(m--)
{
cin>>r;
s=r*r*pi;
a.push_back(s);
sum+=s;
}
b=0;e=sum/(f+1);
vector<double>::iterator it;
while(e-b>0.00001)
{
sum=0;
mid=(b+e)/2;
for(it=a.begin();it!=a.end();it++)
sum+=int(*it/mid);
if(sum>=f+1)b=mid;
else e=mid;
}
printf("%.4f\n",b);
a.clear();
}
}