uvalive 3635 - Pie(二分搜索)

来源:互联网 发布:企业报表软件推荐 编辑:程序博客网 时间:2024/04/27 20:56

题目大意: 有m个派, 要分给n + 1个人, 要求每个人拿到的大小相同, 并且每个人的派必须是一整块, 不能说分别从两个派切出一块凑成。 现在给出m个派的半径(派均为圆柱,高为1),输出每人可以拿到的最大派的体积。


解题思路:二分查找。

#include <cstdio>#include <cmath>#include <algorithm>using namespace std;const int maxn = 10000 + 10;const double PI = acos(-1.0);int N, F;double A[maxn];bool judge(double area) {int cnt  = 0;for (int i = 0; i < N; i++)cnt += floor(A[i] / area);return cnt >= F + 1;}int main() {int T;scanf("%d", &T);while (scanf("%d%d", &N, &F) != EOF) {double maxa = -1;for (int i = 0; i < N; i++) {int r;scanf("%d", &r);A[i] = PI * r * r;maxa = max(maxa, A[i]);}double L = 0, R = maxa;while (R - L > 1e-5) {double M = (L + R) / 2;judge(M) ? L = M : R = M;}printf("%.4lf\n", L);}return 0;}


0 0
原创粉丝点击