分蛋糕

该博客探讨了一个关于如何将不同半径的圆柱形奶酪平均分给朋友和自己的问题,要求每个人分得的奶酪体积相等。博主提出,每份必须来自同一个奶酪,且给出了一种二分搜索的解决思路。

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

/*
题意:作者要开一个生日party,他现在拥有n块高度都为1的圆柱形奶酪,已知每块奶酪的底面半径为r不等,
作者邀请了f个朋友参加了他的party,他要把这些奶酪平均分给所有的朋友和他自己(f+1人),
每个人分得奶酪的体积必须相等(这个值是确定的),形状就没有要求。
现在要你求出所有人都能够得到的最大块奶酪的体积是多少?

要求是分出来的每一份必须出自同一个pie,也就是说当pie大小为3,2,1,只能分出两个大小为2的,
剩下两个要扔掉。

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.

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
*/

思路:
分出来的每一份只能出自一个蛋糕,那么分的蛋糕大小一定不能超过最大那个蛋糕的体积。所以二分在(0,max);

#include<stdio.h>
#include<string.h>
#define PI 3.1415926535897932
double s[1500],max;
int n,f;
double bsearch()
{
   double l,r,mid;
   int ans,i;
   l=0;
   r=max;
   while(r-l>0.000001)
   {
      ans=0;
      mid=(l+r)/2;
      for(i=0;i<n;i++)
      {
         ans=ans+(int)(s[i]/mid);
      }
      if(ans>=f+1)
      {
         l=mid;
       }
       else
       {
         r=mid;
       }
    }
       return r;
  }
int main()
{
    int t,r,i;
    scanf("%d",&t);
    while(t--)
    {
      max=0;
      memset(s,0,sizeof(s));
      scanf("%d%d",&n,&f);
      for(i=0;i<n;i++)
      {
        scanf("%d",&r);
        s[i]=PI*r*r;
        if(s[i]>max)
        {
          max=s[i];
        }
       }
      printf("%.4lf\n",bsearch());
    }
      return 0;
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值