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.141650.2655
题目描述:将n个pie分给F+1个人(包括自己),并且每个人分的都是完整的(不能由多个pie组成)
题目分析:每个人分的最少肯定是最小的pie除以人数,最多的肯定是最大的pie,采用二分法即可,注意PI的取值,千万不要用近似值,否则会出错
#include<iostream> #include<math.h> #include<algorithm> using namespace std; #define PI (4*atan(1.0)) //take care #define MINVAL 10001 #define MAXVAL -1 int pie[10000]; double get_max_pie(double left, double right, int length, int friends) { double mid; int cnt; while (left <= right && fabs(left - right) > 0.000001) { mid = (left + right) / 2; cnt = 0; for (int i = 0; i < length; i++) { cnt += (int)(pie[i] / mid); } if (cnt < friends) //you are too much { right = mid; } else //you are little { left = mid; } } return left; } int main() { int cases; scanf("%d", &cases); //cin >> cases; int pies; int friends; int min_pie; int max_pie; while (cases--) { //cin >> pies >> friends; scanf("%d%d", &pies, &friends); friends++; min_pie = MINVAL; max_pie = MAXVAL; for (int i = 0; i < pies; i++) { //cin >> pie[i]; scanf("%d", &pie[i]); pie[i] *= pie[i]; min_pie = min_pie < pie[i] ? min_pie : pie[i]; max_pie = max_pie > pie[i] ? max_pie : pie[i]; } double left = 1.0*min_pie / friends; //the minial area,just the biggest pei and divide it double right = 1.0*max_pie; //the maximal area,just the biggest pie double ret = get_max_pie(left, right, pies, friends)*PI; printf("%.4lf\n", ret); } return 0; }