Problem:
給出n個圓的半徑,體積均爲1,分給f+1個人,每個人的體積相同,問最大的半徑是多少?
Solution:
二分枚舉半徑,然後用每個圓去試能有幾個這樣的圓,如果滿足,就嘗試更高的。
note:
double輸出要用%f,輸入用%lf。
#include <cstdio>
#include <algorithm>
using namespace std;
const double PI = 3.141592653589793;
const double esp = 1e-6;
double rad[10010];
int main() {
int t;
scanf("%d", &t);
while(t--) {
double low = 0, high = 1e8, mid;
int n, f, cnt;
scanf("%d%d", &n, &f);
for(int i=1; i<=n; i++) {
scanf("%lf", &rad[i]);
rad[i] *= rad[i];
}
while((high-low) > esp) {
cnt = 0;
mid = (high+low)/2;
for(int i = 1; i <= n; i++)
cnt += rad[i]/mid;
if(cnt >= f+1)
low = mid;
else
high = mid;
}
printf("%.4f\n", mid * PI);
}
return 0;
}