人生列车

follow on!success!

导航

二分法经典习题——HDU1969

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

double pie[10005];
int T, N, F;
double PI = acos(-1.0);

bool test(double x)
{
int cnt = 0;
for(int i=1; i<=N; ++i)
cnt += int(pie[i]/x);
if(cnt >= F+1)
return 1;
else
return 0;
}

int main()
{
cin >> T;
double rad;
double sum;
while(T--)
{
cin >> N >> F;
sum = 0.0;
for(int i=1; i<=N; ++i)
{
cin >> rad;
pie[i] = rad*rad*PI;
sum += pie[i];
}
double l = 0.0;
double r = sum/(F+1);
double mid;
while(r-l > 1e-6)
{
mid = (l+r)/2.0;
if(test(mid))
l = mid;
else
r = mid;
}
cout << fixed << setprecision(4) << mid << endl;
}
}

posted on 2014-07-17 14:48  tianxia2s  阅读(542)  评论(0编辑  收藏  举报