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.
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
ps:设答案为ans,这样问题就转化为求满足不等式的最大ans了。然后,设l = 0 < ans < r = maxri,然后就是在l到r的范围内求解,什么时候得到最大的ans呢?因为f(l) <= y,f(r) >= y,当l = r 时,即 f(l) = f (r) = f(ans) = y,这样就得到了解。
l与r的逼近是通过二分进行的,二分的用法其实挺灵活的,比如最常见的二分查找,解个方程,这里的求最优解不等式,
凡是给定了一个范围的,找有一个需要符合条件的值,而且查找的范围中的数都是线性排列的,即被缩小的范围的那一半中的数一定不会是符合条件的解,都可以用到二分。而且对整数的查找二分的复杂度很低log(n),实数范围一般题目也会限定较小的初始范围,所以一般也不会超时。。。
代码:
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string.h>
using namespace std;
#define PI acos(-1.0)
int n,m;
double b[10020];
int vis(double a)
{
int z=0;
for(int i=1; i<=n; i++)
z+=(int)(b[i]/a);
if(z>=m)
return 1;
return 0;
}
int main()
{
int t;
int q;
scanf("%d",&t);
while(t--)
{
memset(b,0,sizeof(b));
double sum=0;
scanf("%d %d",&n,&m);
m+=1;
for(int i=1; i<=n; i++)
{
scanf("%d",&q);
b[i]=q*q*PI;
sum+=b[i];
}
double s=sum,r=0;
double w;
while(abs(s-r)>1e-7)
{
w=(s+r)/2;
if(vis(w))
r=w;
else
{
s=w;
}
}
printf("%.4f\n",w);
}
}