POJ 3122(二分+高精度)

    
Pie
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12486   Accepted: 4310   Special Judge

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

Source

Northwestern Europe 2006


题意分析:你有n个蛋糕,现在要把它分给f+1个人(每个人分得的蛋糕体积大小必须相等) 

          问:每个人可以拿到的最大的体积为多少(每个人只能得一整块 不能块数累加---排除求平均数的算法)


在这个题中最大的坑点不是二分  是**********(精度)

   注意
     1 精度问题     左右两者差距需要1e-6  ///高精度加在较小的数上
     2 mid =high 或mid= low 不能 +1  //会少情况
     3 %f         //double输入用%lf  输出用%f   大坑点-----

     4 acos(-1.0)  为PI的值    PI需要高精度  


AC代码

#include<map>
#include<math.h>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
using namespace std;
typedef long long ll; 
const int MAX_N = 100000+50;
const int INF = 0x3f3f3f3f;
double a[MAX_N];
ll n,f;


int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		double maxsize = 0.0;
		scanf("%d%d",&n,&f);
		f+=1;
		for(int i = 0; i < n ; i++)
		{
			scanf("%lf",&a[i]);
			a[i] =1.0*a[i]*a[i];
			maxsize = max(maxsize,a[i]);
		}
		double lf = 0 , rt = maxsize;
		double mid,ans=0.0;
		int num = 0;
		while(lf+0.000001<rt)
		{
			mid = (rt + lf)/2;
			num = 0;
			for(int i = 0; i < n; i++)
			{
				num +=(a[i]/mid);
			}
			if(num >= f)
			{
				lf = mid;
				ans = max(ans,mid);
			}
			else
			{
				rt = mid;
			}
		}
		printf("%.4f\n",ans*pi);
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_40984919/article/details/80348977