思路
给定N块半径分别为ri(i=1,2,..,N)的派,问若分给包括自己在内的F+1个人,每个人能分到的派最大是多少?
错误的思路是以整数的形式考察r,这将得不到所有的结果,以浮点数的形式考察的思路也是不明晰的,每次的增减标准量应该设为多少?
那么直接考察派的大小,可以尝试使用二分法去撞出合理的值。这里,简要的描述二分法。
当左边的值相离右边的值的时候 取左右的中间值尝试 如果派不够分 将右值靠近 否则 将左值靠近
这里有一个二分法要注意的地方,中间值的计算比如h = (l + r) / 2
,在本题是不可能溢出的,如果值过大呢?l + r
将会溢出。这里应该h = l + (r - l) / 2
。
另外对于本题使用了浮点数更要考虑二分中比较左右时精度的问题,这里取EPSILON = 1e-5
。具体的取多大合适可以研究。
还有一件事..PI不建议直接描述其值..3.1415926535
获得了WA
,3.14159265358
获得了AC
。可以考虑使用数学库里的三角函数。
代码
#include <cstdio>
#define PI 3.14159265358
int N, F;
double A[10005];
bool judge(double v)
{
int s = 0;
for (int i = 0; i < N; i++) s += A[i] / v;
if (s > F) return true;
return false;
}
void solve()
{
double l = 0, r = PI * 1e8, h = 0;
while (r - l > 1e-5) {
h = l + ((r - l) / 2);
if (judge(h)) l = h;
else r = h;
}
printf("%.4lf\n", r);
}
int main()
{
int T, V;
scanf("%d", &T);
while (T--) {
scanf("%d%d", &N, &F);
for (int i = 0; i < N; i++) {
scanf("%d", &V);
A[i] = PI * V * V;
}
solve();
}
return 0;
}
题目
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 ≤ 10000: the number of pies and the number
of friends. - One line with N integers ri with 1 ≤ ri ≤ 10000: 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 oating 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