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);
    }
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值