Pie 二分

版权声明:原创文章转载时请注明出处 https://blog.csdn.net/weixin_42856843/article/details/88877733

Pie 二分

Time limit:1000 ms Memory limit:65536 kB Source: Northwestern Europe 2006 Special judge: Yes

描述

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.

输入

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.

输出

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.

Input

3
3 3
4 3 3
1 24
5
10 5
1 4 2 3 4 5 6 5 4 2

Output

25.1327
3.1416
50.2655

代码

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctype.h>
#include <stdio.h>
#include <string.h>


#define PI 3.14159265358979383246
#define LL long long
#define INF 0x3f3f3f3f  

#define _for(i, a) for(int i = 0; i < (a); ++i)
#define _rep(i, a, b) for(int i = (a); i < (b); ++i)
#define _forit(a, b) for(a::iterator it = b.begin(); it != b.end(); it++)


using namespace std;

double pie[10000];
int N = 0, F = 0;

bool che(double x) {
	int t = 0;
	_for(i, N)
		t += (int)(pie[i] / x);
	return t >= F;
}//检验当前分法是否可行

double getpie(double left, double right) {
	double t = (right + left) / 2;
	if (right - left < 0.00001) return t;
	if (che(t))
		return getpie(t, right);
	else
		return getpie(left, t);
}

int main()
{
	//freopen("input.txt", "r", stdin);
	int T = 0;
	cin >> T;
	while (T--) {
		double ave = 0;
		cin >> N >> F; F += 1;//获取蛋糕总个数和总人数
		_for(i, N) {
			cin >> pie[i];
			pie[i] = PI * pie[i] * pie[i];//直接把半径转化成面积
			ave += pie[i];
		}
		printf("%.4f\n", getpie(0, ave));
		//cout << getpie(0, ave / N) << endl;
	}
	return 0;
}

本人也是新手,也是在学习中,勿喷

转载请注明出处

欢迎有问题的小伙伴一起交流哦~

猜你喜欢

转载自blog.csdn.net/weixin_42856843/article/details/88877733