POJ 3122 Pie

[Description]
作者要开一个生日 party,他现在拥有 n 块高度都为 1 的圆柱形奶酪,已知每块奶酪的底面
半径为 r 不等,作者邀请了 f 个朋友参加了他的 party,他要把这些奶酪平均分给所有的朋
友和他自己(f+1 人),每个人分得奶酪的体积必须相等,形状就没有要求。现在要你求
出所有人都能够得到的最大块奶酪的体积是多少?
[Input]
第一行一个数 T,表示有 T 组测试数据
每组测试数据第一行两个数,n,f,表示奶酪数和朋友数,第二行 n 个数 ri,表示每个奶
酪的底面半径。
[Output]
对于每组测试数据输出答案,误差在 0.001 以内
[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
[Hint]

1 <= n ,f, ri<= 10,000


思路:

就是二分。
二分每个人可以分得的派的体积。最多为每个人得一个最大的派,最少为所有人分最小一个。
但是要注意精度。
Pi=4.0*atan(1.0)=acos(-1.0); 头文件为#include<cmath> 


代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;


const int maxn=10000+10;
int n,m;
int r[maxn];
double tot,ans;
double Pi=acos(-1.0);


int check(double x)
{
int cnt=0;
for(int i=1;i<=n;i++)
{
double temp=Pi*r[i]*r[i];
while(temp>0)
{
if(temp-x>0.0000000001)
{
temp=temp-x;
cnt++;
}
else
{
break;
}
}
}
if(cnt>=m+1)return 1;
else return 0;
}


void find()
{
double l,r;
l=0;
r=(tot+1)*1.0/(m+1);
while(r-l>0.0000001)
{
double mid=(l+r)*1.0/2;
if(check(mid)==1)
{
l=mid;
ans=mid;
}
else
{
r=mid-0.0000001;
}
}
}


int main()
{
int t;
scanf("%d",&t);
for(int u=1;u<=t;u++)
{
tot=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&r[i]);
tot=tot+Pi*r[i]*r[i];
}
find();
printf("%.4lf\n",ans);
}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值