POJ 3122 Pie(二分+贪心)
Pie
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 22684 | Accepted: 7121 | Special Judge |
Description

My friends are very annoying and if one of them gets a bigger piece than the others, they start complaining. Therefore all of them should get equally sized (but not necessarily equally shaped) pieces, even if this leads to some pie getting spoiled (which is better than spoiling the party). Of course, I want a piece of pie for myself too, and that piece should also be of the same size.
What is the largest possible piece size all of us can get? All the pies are cylindrical in shape and they all have the same height 1, but the radii of the pies can be different.
Input
One line with a positive integer: the number of test cases. Then for each test case:
- One line with two integers N and F with 1 ≤ N, F ≤ 10 000: the number of pies and the number of friends.
- One line with N integers ri with 1 ≤ ri ≤ 10 000: the radii of the pies.
Output
For each test case, output one line with the largest possible volume V such that me and my friends can all get a pie piece of size V. The answer should be given as a floating point number with an absolute error of at most 10−3.
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
Source
题目意思:
就是公平地分披萨pie
我生日,买了n个pie,找来f个朋友,那么总人数共f+1人
每个pie都是高为1的圆柱体,输入这n个pie的每一个尺寸(半径),如果要公平地把pie分给每一个人(就是所有人得到的pie尺寸一致,但是形状可以不同),而且每个人得到的那份pie必须是从同一个pie上得到的
做法:
输入的是朋友的数量f,分pie是分给所有人,包括自己在内共f+1人
下界low=0,即每人都分不到pie
上界high=maxsize,每人都得到整个pie,而且那个pie为所有pie中最大的
(上界就是 n个人n个pie,每个pie还等大)
对当前上下界折中为mid,计算"如果按照mid的尺寸分pie,能分给多少人"
求某个pie(尺寸为size)按照mid的尺寸,能够分给的人数,就直接size / mid,舍弃小数就可以
code:
#include<stdio.h> #include <iostream> #include <algorithm> #include <math.h> #include <queue> #include<string.h> using namespace std; #define max_v 10005 double PI=acos(-1.0); double v[max_v]; int n,f; bool test(double x) { int num=0; for(int i=0;i<n;i++) { num+=int(v[i]/x);////每一个蛋糕的体积除以x 必须是完整蛋糕所以int(); } if(num>=f+1) //当人数大于等于f+1时说明蛋糕可以更大 return true; else return false; } int main() { int t; cin>>t; while(t--) { int r; cin>>n>>f; double left=0,right=0,mid=0; for(int i=0;i<n;i++) { cin>>r; v[i]=PI*r*r*1.0;//体积都为1 right=max(right,v[i]);//最大蛋糕就是右边界 } while(fabs(right-left)>1e-6) { mid=(right+left)*0.5; if(test(mid)) { left=mid;//否则mid偏小,下界优化 }else { right=mid;//说明mid偏大,上界优化 } } printf("%.4f\n",mid); } return 0; }
心之所向,素履以往
分类:
ACM
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南