POJ3122 Pie(二分)

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

题意:一堆人分蛋糕,每人蛋糕大小一样,求最大能分多少,蛋糕必须是整块整块的,不能两块拼一起。然后注意输入F个人最后要分F+1份。

思路:很简单很水,但是精度处理很恶心,wa了很多发,直接二分蛋糕的半径就行了

AC代码:

 1 #include<iostream>
 2 #include<vector>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<cstring>
 7 #include<queue>
 8 #include<map>
 9 #define p 3.14159265359
10 using namespace std;
11 const int maxn = 1e4+5;
12 const double eps = 1e-8;
13 double pie[maxn];
14 int N,F;
15 bool check(double x){
16     int cnt = 0;
17     for(int i = 0;i<N;i++){
18         cnt+=(int)(pie[i]*pie[i]/(x*x));//每次check一下是否满足大于F个
19     }
20     return cnt>=F;
21 }
22 int main(){
23     int t;
24     scanf("%d",&t);
25     while(t--){
26         scanf("%d%d",&N,&F);
27         F+=1;
28         double MAX = 0.0;
29         for(int i = 0;i<N;i++){
30             scanf("%lf",&pie[i]);
31             MAX = max(MAX,pie[i]);//求出最大半径
32         }
33         double l = 0, r = MAX*2;//左右区间
34         double mid;
35         while(l+eps<r){
36             mid = (l+r)/2;
37             if(check(mid)){
38                 l = mid;
39             }
40             else{
41                 r = mid;
42             }
43         }
44         printf("%.4lf\n",l*l*p);
45     }
46     return 0;
47 }

原文地址:https://www.cnblogs.com/AaronChang/p/12171854.html

时间: 2024-08-30 04:46:45

POJ3122 Pie(二分)的相关文章

【hoj】2651 pie 二分查找

二分查找是一个很基本的算法,针对的是有序的数列,通过中间值的大小来判断接下来查找的是左半段还是右半段,直到中间值的大小等于要找到的数时或者中间值满足一定的条件就返回,所以当有些问题要求在一定范围内找到一个满足一些约束的值时就可以用二分查找,时间复杂度O(log n); 题目:http://acm.hit.edu.cn/hoj/problem/view?id=2651 因为题目有精度要求,对于浮点数小数点部分会有一定误差,所以可以选择将这些有小数部分的数值扩大e6倍,因为题目要求精确到e-3,之后

POJ 3122 Pie 二分答案

Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9653   Accepted: 3478   Special Judge Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of v

poj 3122 Pie (二分)

<span style="background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 18pt;">Description</span> My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of

POJ 3122 Pie (二分+精度)

Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11240   Accepted: 3919   Special Judge Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of

POJ3122 Pie

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15473   Accepted: 5302   Special Judge Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of vari

HDU 1969 Pie 二分

1.题意:一项分圆饼的任务,一堆圆饼共有N个,半径不同,厚度一样,要分给F+1个人.要求每个人分的一样多,圆饼允许切但是不允许拼接,也就是每个人拿到的最多是一个完整饼,或者一个被切掉一部分的饼,要求你算出每人能分到的饼的体积最大值.输入数据依次给出,测试数据组数T,每组数据中,给出N,F,以及N个圆饼的半径.输出最大体积的数值,精确到小数点后四位. 2.分析:一看是这种输出就知道用二分写会很高效,这里对"能分出的最大体积值"进行二分.首先,这个值有界,最大值为总体积除以总人数的值,即Σ

HDU 1969 Pie (二分查找)

题目链接:click here~~ 题目大意:n块馅饼分给m+1个人,每一个人的馅饼必须是整块的.馅饼能够被切开.但不能组合,也不一定要所有分完,问你每一个人最大能分到多大体积的馅饼面积. [解题思路]:二分,对于每一个V值,我们枚举相应情况下人数P的多少,发现是单调递减的,因此二分查找区间,初始值left=0,right=inf;然后judge函数推断当前mid值是否能使得p>=m,因此累计ans=num[i]/mid,写的时候二分用的是while推断,怎么调试答案就是差了那么一点点.后来索性

poj 3122 Pie 二分(最大化平均值)

Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11776   Accepted: 4076   Special Judge Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of

HDU1969:Pie(二分)

Pie Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 59   Accepted Submission(s) : 31 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description My birthday is coming up and trad