题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=1636&mosmsg=Submission+received+with+ID+1205580
这个题要求求出将n个圆形蛋糕分为f+1个人的最大面积,要求蛋糕必须是整体,不能几块拼在一起。
由题意可以想到这个题可以用二分的思想,只是在做的过程中需要注意精度问题,而且对于一块蛋糕能分为多少个x面积的蛋糕时,应该向上取整。
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;
int n,f;
double p[10001],maxi;
const double PI=acos(-1.0);
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%d%d",&n,&f);
maxi=0;
for(int i=0;i<n;i++)
{
double r;
scanf("%lf",&r);
p[i]=r*r*PI;
maxi=max(maxi,p[i]);
}
double l=0,r=maxi;
while(r-l>1e-6)
{
double mid=(l+r)/2;
int sum=0;
for(int i=0;i<n;i++)
sum+=floor(p[i]/mid);
if(sum>=f+1)
l=mid;
else
r=mid;
}
printf("%.4lf\n",l);
}
return 0;
}