【UVA12097】Pie

来源:互联网 发布:萧山区网络问政 编辑:程序博客网 时间:2024/06/07 21:55

题面

  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 com-plaining. 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.

题意

  有n个饼,半径为riri104),分给f+1个人,要求每个人分到的面积是一样的,而且每个人分到的饼是连续的一块(即不能是几个小块凑起来的),求能够分到的最大面积,精确到三位小数

解法

二分:
  虽然不是求什么最大的最小值(或者反过来)什么的……但还是可以用二分的,因为之前就做过一道小数型二分题(下面等会讲)
  考虑二分面积,下界L=0,上界R=ni=1πri2。对于一个中值x和一张半径为r的饼来说,这张饼能够分出的最多分数显然是:πr2x,所以我们只需要求出ni=1πri2x,判断其与f+1的关系即可
  特别要注意的一点:π的大小!!!,因为r104,所以r2108,又因为题目要求精确到三位小数,所以π就要精确到10-11,即π=3.14159265358

拓展

maze

分析

  maze代码:https://gitee.com/qjlyh/codes/dg3rbnm45xfz7ytjlckhu64
  maze数据:https://pan.baidu.com/s/1eS3Z9aq

复杂度

O(nlogRL

代码

#include<iostream>#include<cstdlib>#include<cstdio>#include<queue>#define Lint long long intusing namespace std;const double pi=3.141592653589;//79323846const double eps=1e-6;const int MAXN=10010;double r[MAXN],s[MAXN];double n,L,R;int T,f;bool check(double lim){    int ret=0;    for(int i=1;i<=n;i++)        ret+=(int)(s[i]/lim);    return ret>=f;}int main(){    scanf("%d",&T);    while( T-- )    {        L=R=0;        scanf("%lf%d",&n,&f),f++;        for(int i=1;i<=n;i++)        {            scanf("%lf",&r[i]);            s[i]=pi*r[i]*r[i];            R+=s[i];        }        while( R-L>eps )        {            double mid=(L+R)/2;            if( check( mid ) )   L=mid;            else   R=mid;        }        printf("%.4lf\n",L);    }    return 0;}