POJ 3122 Pie【二分答案】

题意:给出n个圆,分别的半径,现在需要把它们分成f+1块,问每一块的面积

和上面一题一样,二分面积,上限是这里面最大的圆的面积

另外可以先只二分r*r,到最后再乘上pi,不过这题先乘后乘都能过

不过不懂的是,输出是lf就一直wa,后来看了discuss,改成f就过了

 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<cstring> 
 4 #include <cmath> 
 5 #include<stack>
 6 #include<vector>
 7 #include<map> 
 8 #include<set>
 9 #include<queue> 
10 #include<algorithm>  
11 using namespace std;
12 
13 typedef long long LL;
14 const int INF = (1<<30)-1;
15 const int mod=1000000007;
16 const int maxn=1000005;
17 const double pi=3.14159265359;
18 
19 double a[maxn],r[maxn];
20 int n,f;
21 
22 int ok(double x){
23     int ans=0;
24     for(int i=1;i<=n;i++){
25         ans+=(int)(a[i]/x);
26     }
27     if(ans<(f+1)) return 0;
28     return 1;
29 }
30 
31 int main(){
32     int T;
33     scanf("%d",&T);
34     while(T--){
35         scanf("%d %d",&n,&f);
36         double smax=-1;
37         for(int i=1;i<=n;i++){
38             scanf("%lf",&r[i]);
39             a[i]=r[i]*r[i];
40             
41         //    printf("a[%d]=%lf
",i,a[i]);
42             smax=max(smax,a[i]);
43         }
44         
45         double l=0.000,r=smax,mid;
46         while(r-l > 1e-6){
47             mid=(l+r)/2;
48             if(ok(mid)) l=mid;
49             else r=mid;
50     //        printf("l=%lf
",l);
51     //        printf("r=%lf
",r);
52     //        printf("mid=%lf
",mid);
53         }
54         printf("%.4f
",l*pi);
55         
56     }
57     return 0;
58 }
View Code
原文地址:https://www.cnblogs.com/wuyuewoniu/p/4541568.html