Pie(POJ--3122【二分查找】

该博客介绍了如何使用二分查找算法解决POJ中的一道题目——Pie,主要内容包括理解题意,设定边界条件,编写二分查找代码,并给出样例输入和输出。
摘要由CSDN通过智能技术生成

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.
题意:有n个pie,要分给f+1(包括我自己)个人,要求每个人被分得的pie大小要一样且被分得的pie来自同一个pie,然后输入n个pie的半径,求每个人分得的pie最大的体积是多少(高全部为1)。
思路:因为所求体积的高都为1则直接求最大面积即可,即直接二分枚举符合要求的最大半径即可。

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

#include <cstdio>
#include <algorithm>
#include <cmath>
#define INF 0x3f3f3f3f
#define esp 1e-12
using namespace std;
int s[10005];
const double PI=acos(-1.0);            //精确的PI=acos(-1.0)
//int dmp(double x)
//{
//    return fabs(x)<0?0:(x>0?1:-1);
//}
int main()
{
    //freopen("lalala.text","r",stdin);
    int t,n,f,cnt,mm;
    double l,r,mid;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&f);
        f++;                                      //包括自己的一份所以f要加1
        mm=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&s[i]);
            if(s[i]>mm)
                mm=s[i];                      //找所枚举的半径上限
        }
        l=0;
        r=mm;
        while((r-l)>esp)            //对于浮点数也可以这样比大小,但不造为啥,用dmp(r-l)>0就蹦不出循环了(23333
        {
            mid=(l+r)/2;
            cnt=0;                        //累加能以当前半径分得的pie的份数
            for(int i=1; i<=n; i++)
                cnt+=(int)((s[i]*s[i])/(mid*mid));
            if(cnt<f)                     //如果份数小于要被分得的人数则说明当前半径大了
                r=mid;
            else                           //如果份数大于等于要被分得的人数则说明当前半径小了
                l=mid;
        }
        printf("%.4f\n",l*l*PI);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值