原文链接
http://poj.org/problem?id=3122
二分,很好的二分应用,刚开始以为是用贪心,但是,后来怎么都不行,和平时的思维一样,如果要是分够的话,直接找大于mid的那一部分,要是不够的话,找小于mid 的那一部分,经典!
#include<iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const int Max = 10010;
const double PI = 4*atan(1.0);
const double epx =1e-6;
int main(){
int t, n, f, i;
double pie[Max];
scanf("%d", &t);
while(t --){
scanf("%d%d", &n, &f);
f ++;
double sum = 0;
for(i = 1; i <= n; i ++){
int r;
scanf("%d", &r);
pie[i] = r * r;
sum += pie[i];
}
double mid, low = 0, high = sum / f;
while(low + epx < high){
mid = (low + high) / 2;
int count = 0;
for(i = 1; i <= n; i ++)
count += (int)(pie[i] / mid);
if(count >= f) low = mid;
else high = mid;
}
printf("%.4f\n", mid * PI);
}
return 0;
}