POJ 3122 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



題目大意:

        我有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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章