您的位置:首页 > 其它

UVA 12097 UVALive 3635 Pieni (二分)

2013-08-22 19:23 537 查看
 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 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


题意:你要开party。你有n块披萨,有f个小伙伴,你要分披萨,小伙伴如果看到别人披萨比自己的大就会很忧伤,真是林子大了什么小伙伴都有啊。所以你要保证每个小伙伴分到的披萨一样大,当然包括你自己也要分到一块。并且每个小伙伴的披萨都必须是一整块的。

思路:2分查找,判断当前x能不能满足条件,如果不能mid就等于end,如果可以mid就等于start。知道start = end。

代码:

#include <stdio.h>
#include <math.h>

const double pi = asin(1) * 2;
int t, n, f;
double r[10005];
double start, end;

int judge(double mid) {
int num = 0;
for (int i = 0; i < n; i ++) {
double sb = r[i] * r[i];
while (sb - mid > 0) {
sb -= mid;
num ++;
if (num == f)
return 1;
}
}
return 0;
}
int main() {
scanf("%d", &t);
while (t --) {
end = 0; start = 11111;
scanf("%d%d", &n, &f);
f ++;
for (int i = 0; i < n; i ++) {
scanf("%lf", &r[i]);
end += r[i] * r[i];
if (start > r[i] * r[i])
start = r[i] * r[i];
}
start /= f;
double mid;
while (fabs(start - end) >= 1e-4) {
mid = (start + end) / 2;
if (judge(mid))
start = mid;
else
end = mid;
}
printf("%.4lf\n", mid * pi);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: