POJ 3122 Pie(二分查找求最大值)

本文解析了一道经典的二分查找题目——POJ3122Pie,该题要求在多个不同大小的圆柱形派中均匀切分给定数量的人相同体积的派,并求出最大的可分配体积。文章详细介绍了使用二分查找解决此问题的思路与步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


 

POJ 3122 Pie(二分查找求最大值)

描述

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

 

题意

原题是派对分派吃什么的,这里简化一下题意:

有N个半径不一,高度为1的圆柱体,每个圆柱体沿高切出若干个底面扇形的柱体(可以不切),要求切出来的所有柱体体积相等,数量为f+1(别忘了“我”)。求目标体积最大值,精确到​​1e-3

思路

在满足条件(pie够切)的情况下,尽可能得到最大值,类似问题就可以用二分查找做。

先读入所有pie的半径,用自己定义的Π=acos(-1.0),计算出体积并覆盖原半径数组。

对所有pie从小到大排序,二分上下界分别为最大体积的pie即a[n-1],和0,二分中的mid是假设体积,遍历a[],对当前pie外套一个while循环,不断判断是否够切,若够切,切掉(数值-mid),计数器sum+1,直到不够切,对下一个pie进行相同操作。直至所有pie判断完成,将最终获得的sum与f+1比,若sum>=f+1,说明二分猜测的mid可能太小了太小气了,分给客人这么点pie,让left=mid(并不是mid+1,思考为什么),并用ans记录下mid,防止可能的正常答案丢失;若sum<f+1,pie不够切,mid=right-1。以上操作直至right-left<1e-6(虽然题目要求1e-3,不过在不超时的情况下精度高点总是好的),输出ans。

注意

涉及精度,不少变量需定义成double型

对pie切割时不能直接对原数组减,要另建一个数组,初始值为原pie的体积

doble型上下界,不能直接left+1或right-1

一次大循环后注意各值的初始化,不能沿用上个循环的

f=f+1,别忘了题中的“我”

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<math.h>
using namespace std;
const double PI=acos(-1.0);
double a[10005];
double a1[10005];
int main()
{
    int t;
    scanf("%d",&t);
    int n,f;
    while(t--)
    {
        scanf("%d %d",&n,&f);
        f+=1;//我也要吃
        for(int i=0;i<n;i++)
        {
            scanf("%lf",&a[i]);
            a[i]=a[i]*a[i]*PI;//换成体积
        }
        sort(a,a+n);
        double Left=0.0,Right=a[n-1]*1.0;
        double mid;
        double ans;
        int sum=0;
        while(Right-Left>1e-6)//精度
        {
            sum=0;
            mid=(Left+Right)/2.0;
            for(int i=0;i<n;i++)
            {
                a1[i]=a[i];//“替身”
                while(a1[i]>=mid)
                {
                    sum++;
                    a1[i]-=mid;
                }
            }
            if(sum>=f)
            {
                ans=mid;
                Left=mid;
            }
            else Right=mid;
        }
        printf("%lf\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值