POJ 3122 Pie

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

题意:T组数据,每组数据第一行是两个整数,N,F:N代表有N种口味的蛋糕,F代表有F个人来给他庆祝,下一行给你N个蛋糕的半径,每个蛋糕的高度都是固定的为1,现在要你把这个N个蛋糕分给F+1个人(因为F个人再加上一个过生日的主角,所以F+1个人),每个人分的蛋糕要求,体积完全一样(形状不需要考虑),并且每个人手中的蛋糕必须是一种蛋糕(不能是多种口味的蛋糕混起来“凑”体积)。

这题的坑点很多,我也是wa到输入验证码得来的结果。

(1)首先在其他代码无误的情况下,PI至少取11位,否则错误。

(2)在PI取15位的情况下,eps只能取到1e-7,1e-8以后就会超时。

(3)别忘了POJ的%f

以前以为PI能记住十位已经可以了,从今以后再加十位:3.14159265358979323846。

具体请看代码:

#include 
#include 
#include 
#include 
using namespace std;
#define eps 1e-6
#define inf 0x3f3f3f3f
#define PI 3.14159265358979323846
#define f(R) PI*R*R
int N, F;
double radius[10005];
int judge(double x) {
    int sum = 0;
    for(int i = 0; i < N; i++) {
        double V = radius[i];
        sum += (int)(V/x);
    }
    if(sum >= F+1) return 1;
        return 0;
}

int main() {
	int T;
	scanf("%d", &T);
	while(T--) {
		scanf("%d %d", &N, &F);
		double left = 0, right = 0, mid;
		for(int i = 0; i < N; i++) {
			scanf("%lf", &radius[i]);
			radius[i] = f(radius[i]);
			if(right < radius[i]) right = radius[i];
		}
		while(right - left > eps) {
            mid = (left + right)/2;
            if(judge(mid)) left = mid+eps;
            else right = mid-eps;
		}
		printf("%.4f\n", right);
	}
	return 0;
}

你可能感兴趣的:(水题,高精度)