hdu 1969二分答案

算水题吧,不过这题精度卡得还是挺厉害的,刚开始的时候我是把面积都放大,放大100000000倍,都用long long进行处理,还是过不了,只能用double控制精度了。

/*
 * hdu1969/win.cpp
 * Created on: 2012-11-2
 * Author    : ben
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
const double eps = 0.000001;
const double pi = acos(-1);
const int MAXN = 10009;
int N, F;
double data[MAXN];

inline bool judge(double ans) {
    int t = 0;
    for(int i = 0; i < N; i++) {
        t += (int)(data[i] / ans);
    }
    return t >= F;
}

double getans() {
    double low = 0, mid;
    double high = *max_element(data, data + N);
    while(high - low > eps) {
        mid = (low + high) / 2;
        if(judge(mid)) {
            low = mid;
        }else {
            high = mid;
        }
    }
    return mid;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
#endif
    int T, a;
    scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &N, &F);
        F++;
        for(int i = 0; i < N; i++) {
            scanf("%d", &a);
            data[i] = pi * a * a;
        }
        printf("%.4f\n", getans());
    }
    return 0;
}
posted @ 2012-11-02 14:45  moonbay  阅读(134)  评论(0编辑  收藏  举报