题目的意思就是把n个蛋糕分给f个朋友.
每个人分到的一样大.
问每个人最大分到多少.
大小肯定是从0到最大的那块蛋糕之间,用二分搜一下.
AC代码:
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int N = 10010;
const double PI = acos(-1.0);
int n, f, t;
double a[N], 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;
}