题意:为每个小伙伴切糕,要求每个小盆友(包括你自己)分得的pie一样大,但是每个人只能分得一份pie,不能拿两份凑一起的。
做法:二分查找切糕的大小,然后看看分出来的个数有没有大于小盆友们的个数,它又没说每个pie都要分完,分不完的留给工作人员吃嘛。
代码:
/*
* Author: illuz <iilluzen[at]gmail.com>
* Blog: http://blog.csdn.net/hcbbt
* File: live3652.cpp
* Create Date: 2013-09-10 00:40:36
* Descripton: binary, greedy
*/ #include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std; const int MAXN = 10010;
const double PI = acos(-1.0); int n, f, t;
double a[MAXN], Max; bool judge(double x) {
int sum = 0;
for (int i = 0; i < n; i++)
sum += a[i] / x;
if (sum >= f + 1) return true;
return false;
} int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d", &n, &f);
for (int i = 0; i < n; i++) {
scanf("%lf", &a[i]);
a[i] = PI * a[i] * a[i];
Max = max(Max, a[i]);
}
double low = 0, mid;
while (Max - low > 1e-5) {
mid = low + (Max - low) / 2;
if (judge(mid)) low = mid;
else Max = mid;
}
printf("%.4lf\n", low);
}
return 0;
}