Description
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 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
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
題目大意:
我有f個朋友,n個派,每個派有不同的半徑,開趴的時候,給每個朋友包括我自己都分一塊派,這塊派必須來自一個派的一部分(也就是說不接受兩塊或兩塊以上來自不同派的塊),並且所有人分得的派的面積要相等,形狀不限,問最多每人能分得多大面積的派
解題思路:
二分枚舉答案,注意一下精度就好
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <map>
#include <set>
#include <bitset>
#include <ctime>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#define STD_REOPEN() freopen("../in.in","r",stdin)
#define STREAM_REOPEN fstream cin("../in.in")
#define INF 0x3f3f3f3f
#define _INF 63
#define eps 1e-5
#define MAX_V 100010
#define MAX_P 110
#define MAX_E 10010
#define MAX 32000
#define MOD_P 3221225473
#define MOD 9901
using namespace std;
int t,n,f;
double s[10010];
const double PI=acos(-1.0);
int dcmp(double x)
{
return fabs(x)<eps?0:(x<0?-1:1);
}
int main()
{
//STD_REOPEN();
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&f);
for(int i=0;i<n;i++)
{
scanf("%lf",&s[i]);
s[i]=s[i]*s[i]; //爲了避免精度問題的出現,這裏暫時不乘PI
}
f++;
double l=0,r=200000010.0;
double ans=-1.0;
while(dcmp(r-l)>0)
{
double mid=(l+r)/2;
int cnt=0;
for(int i=0;i<n;i++)
cnt+=s[i]/mid; //記錄當前面積可以分給多少人
if(cnt<f) //小於總人數,面積過大,向左枚舉
r=mid;
else //否則向右枚舉
{
ans=max(ans,mid);
l=mid;
}
}
printf("%.4f\n",ans*PI);
}
return 0;
}