POJ 3122 Pie (二分+精度)

Pie

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11240   Accepted: 3919   Special Judge

Description

My birthday is coming up and traditionally I‘m serving pie. Not just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are coming to my party and each of
them gets a piece of pie. This should be one piece of one pie, not several small pieces since that looks messy. This piece can be one whole pie though.

My friends are very annoying and if one of them gets a bigger piece than the others, they start complaining. Therefore all of them should get equally sized (but not necessarily equally shaped) pieces, even if this leads to some pie getting spoiled (which is
better than spoiling the party). Of course, I want a piece of pie for myself too, and that piece should also be of the same size.

What is the largest possible piece size all of us can get? All the pies are cylindrical in shape and they all have the same height 1, but the radii of the pies can be different.

Input

One line with a positive integer: the number of test cases. Then for each test case:

  • One line with two integers N and F with 1 ≤ N, F ≤ 10 000: the number of pies and the number of friends.
  • One line with N integers ri with 1 ≤ ri ≤ 10 000: the radii of the pies.

Output

For each test case, output one line with the largest possible volume V such that me and my friends can all get a pie piece of size V. The answer should be given as a floating point number with an absolute
error of at most 10?3.

Sample Input

3
3 3
4 3 3
1 24
5
10 5
1 4 2 3 4 5 6 5 4 2

Sample Output

25.1327
3.1416
50.2655

Source

Northwestern Europe 2006

题目链接:http://poj.org/problem?id=3122

题目大意:一个人有f个朋友,n个派,每个派都是圆柱形,高为1,半径为ri,问怎样均分可以使每人得到的尽量的大,这里不可以拼接,比如一个派体积为6我要取其中的5,那剩余的1就没用了,不能补到其他的4上面变成5,还有就是不光是分给朋友,自己也要的

题目分析:高恒定为1,因此分体积就是分面积,我们可以通过二分每人分到的面积来得到答案,这里初始范围的上限为所有的面积和除(f+1)注意这里不是f,因为自己也要分到,下限是取半径最小的那块面积除(f+1),因为不是整形,二分的时候设一个精度,因为答案是小数点后4位,我们就取1e-5,再二分前最好将半径从大到小排序,这样可以节省大量时间,二分的时候从大的依次往小的取,计算按当前的mid值最多可以分给多少人,注意若此时当前半径下的派不够分给一人时break,后面的就不用看了,因为我们已经按从大到小排序了,如果算出来的人数大于等于(f+1)说明分的面积小了,可以将mid扩大,否则说明分的面积大了,只能减小mid。

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int const MAX = 1e4;
double const PI = 4.0 * atan(1.0);
double const EPS = 1e-5;
double R[MAX];

bool cmp(double a, double b)
{
    return a > b;
}

int main()
{
    int T, n;
    scanf("%d", &T);
    while(T--)
    {
        double r, l, mid, sum = 0, mi = 1e10, f;
        scanf("%d %lf", &n, &f);
        f += 1;
        for(int i = 0; i < n; i++)
        {
            scanf("%lf", &R[i]);
            sum += (PI * R[i] * R[i]);
        }
        sort(R, R + n, cmp);
        r = sum / f;
        l = R[n - 1] * R[n - 1] * PI / f;
        mid = (l + r) / 2;
        while(r - l > EPS)
        {
            int cnt = 0;
            for(int i = 0; i < n; i++)
            {
                if((R[i] * R[i] * PI) / mid  > 1.0 - 1e-10)
                    cnt += (R[i] * R[i] * PI) / mid;
                else
                    break;
            }
            if(cnt >= f)
                l = mid + EPS;
            else
                r = mid - EPS;
            mid = (l + r) / 2;
        }
        printf("%.4f\n", mid);
    }
}
时间: 2024-12-24 04:52:01

POJ 3122 Pie (二分+精度)的相关文章

POJ 3122 Pie 二分答案

Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9653   Accepted: 3478   Special Judge Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of v

poj 3122 Pie (二分)

<span style="background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 18pt;">Description</span> My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of

poj 3122 Pie 二分(最大化平均值)

Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11776   Accepted: 4076   Special Judge Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of

poj 3122 (二分查找)

链接:http://poj.org/problem?id=3122 Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10448   Accepted: 3694   Special Judge Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N

POJ 3122 Pie(简单二分)

Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12486   Accepted: 4310   Special Judge Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of

POJ 3122 Pie【二分答案】

题意:给出n个圆,分别的半径,现在需要把它们分成f+1块,问每一块的面积 和上面一题一样,二分面积,上限是这里面最大的圆的面积 另外可以先只二分r*r,到最后再乘上pi,不过这题先乘后乘都能过 不过不懂的是,输出是lf就一直wa,后来看了discuss,改成f就过了 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stac

poj 3122 Pie

Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should

POJ 3104 Drying (二分+精度)

题目链接:click here~~ [题目大意]: 题意:有一些衣服,每件衣服有一定水量,有一个烘干机,每次能够烘一件衣服,每分钟能够烘掉k单位水. 每件衣服没分钟能够自己主动蒸发掉一单位水, 用烘干机烘衣服时不蒸发.问最少须要多少时间能烘干全部的衣服. [解题思路]: 题目数据较大.常规方法肯定会TE.首先能够想到二分枚举答案.枚举时间mid值,(一般二分的题目,题目叫你求什么,就二分什么就能够了) 那么相应两种方法 1:自然风干.2:吹风机吹干 若一件衣服的水量小于mid,则自然风干就可以,

poj 3122(二分查找)

Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13564   Accepted: 4650   Special Judge Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of