POJ 3122-Pie(二分+精度),poj3122-pie
題目地址:POJ 3122
題意:給出n個pie的直徑,有F+1個人,如果給每人分的大小相同(形狀可以不同),每個人可以分多少。要求是分出來的每一份必須出自同一個pi(既當pie大小為3,2,1,只能分出兩個大小為2的份,剩下兩個要扔掉。)
思路:對每一個人分的大小進行二分。注意講pi放在最後成,可以提高精度。
Ps:wa了5發,不知道錯在哪,然後把輸出的%lf改成%f就A了,並不知道為什麼。。。sad
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const double pi= acos(-1.0);const double esp=1e-6;const int maxn=10010;double a[maxn];int main(){ int T,n,F; scanf("%d",&T); while(T--){ double low=0,high=0; scanf("%d %d",&n,&F); F+=1; for(int i=0;i<n;i++){ scanf("%lf",&a[i]); a[i]=a[i]*a[i]; high=max(high,a[i]); } double mid; int cnt; while(high-low>esp){ cnt=0; mid=(low+high)/2; for(int i=0;i<n;i++) cnt+=(int)(a[i]/mid); if(cnt>=F){ low=mid; } else high=mid; } printf("%.4f\n",mid*pi); } return 0;}