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.
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.
题意:有n个pie,要分给f+1(包括我自己)个人,要求每个人被分得的pie大小要一样且被分得的pie来自同一个pie,然后输入n个pie的半径,求每个人分得的pie最大的体积是多少(高全部为1)。
思路:因为所求体积的高都为1则直接求最大面积即可,即直接二分枚举符合要求的最大半径即可。
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
#include <cstdio>
#include <algorithm>
#include <cmath>
#define INF 0x3f3f3f3f
#define esp 1e-12
using namespace std;
int s[10005];
const double PI=acos(-1.0); //精确的PI=acos(-1.0)
//int dmp(double x)
//{
// return fabs(x)<0?0:(x>0?1:-1);
//}
int main()
{
//freopen("lalala.text","r",stdin);
int t,n,f,cnt,mm;
double l,r,mid;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&f);
f++; //包括自己的一份所以f要加1
mm=0;
for(int i=1; i<=n; i++)
{
scanf("%d",&s[i]);
if(s[i]>mm)
mm=s[i]; //找所枚举的半径上限
}
l=0;
r=mm;
while((r-l)>esp) //对于浮点数也可以这样比大小,但不造为啥,用dmp(r-l)>0就蹦不出循环了(23333
{
mid=(l+r)/2;
cnt=0; //累加能以当前半径分得的pie的份数
for(int i=1; i<=n; i++)
cnt+=(int)((s[i]*s[i])/(mid*mid));
if(cnt<f) //如果份数小于要被分得的人数则说明当前半径大了
r=mid;
else //如果份数大于等于要被分得的人数则说明当前半径小了
l=mid;
}
printf("%.4f\n",l*l*PI);
}
return 0;
}