POJ 3122(二分)

来源:互联网 发布:网络科学导论 百度云 编辑:程序博客网 时间:2024/06/06 16:58

题目链接:http://poj.org/problem?id=3122

题意:有n块馅饼,每块馅饼的形状为圆柱体,且高均为1,现在将n块馅饼分给F+1个人(包括自己),要求每个人获得的馅饼体积相等,问最大体积是多少?

很经典的二分。需要注意的是精度问题,不知道wa了多少发。

数据类型用double,因为double类型最多精确到小数点后6位,所以可以以high-low>0.000001为结束条件,另外就是注意二分的临界值,因为low和high不可能相等,

所以每次向下取high=mid或者low=mid。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>using namespace std;const double pi=acos(-1.0);const int INF=0x3f3f3f3f;const int maxn=10010;int T,N,F;double r[maxn];int main(){#ifndef ONLINE_JUDGE    freopen("test.in","r",stdin);    freopen("test.out","w",stdout);#endifscanf("%d",&T);while(T--){scanf("%d%d",&N,&F);double low,high,mid;double max,sum=0;for(int i=0;i<N;i++){scanf("%lf",&r[i]);r[i]*=r[i];sum+=r[i];}low=0,high=sum/(F+1);while(high-low>0.000001){mid=(low+high)/2;int cnt=0;for(int i=0;i<N;i++){cnt+=(int)(r[i]/mid);}if(cnt<F+1){high=mid;}else{max=mid;low=mid;}}double ans=max*pi;printf("%.4lf\n",ans);}return 0;}


0 0
原创粉丝点击