Pie (二分)
发布日期:2021-10-16 05:05:08 浏览次数:16 分类:技术文章

本文共 3403 字,大约阅读时间需要 11 分钟。

Pie

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 107   Accepted Submission(s) : 19
Special Judge
Problem 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 various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should be one piece of one pie, not several small pieces since that looks messy. This piece can be one whole pie though.
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:<ul><li>One line with two integers N and F with 1 ≤ N, F ≤ 10 000: the number of pies and the number of friends.<li>One line with N integers ri with 1 ≤ ri ≤ 10 000: the radii of the pies.</ul>
 
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<sup>3</sup>.
 
Sample Input
33 34 3 31 24510 51 4 2 3 4 5 6 5 4 2
 
Sample Output
25.13273.141650.2655
 
Source
PKU
 

题意:

有f+1个人分n快披萨,每个人要求分得的体积意义,且披萨只能被切开不能重新组合,求每个人能分到的最大的体积。

思路:

用二分法,注意题目输入的是半径,我们不直接寻找半径,而是转换成面积寻找面积。一般遇到实数问题且范围不是很大时,由于精度差问题,我们一般考虑将数扩大相应的倍数,来去掉小数点,以便我们后来的计算。

代码:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define maxn 15#define mod 1000000007#define INF 0x3f3f3f3f#define exp 1e-15#define pi acos(-1.0)using namespace std;long long a[20005];int n,f;bool judge(long long s) //判断当为某一个面积时是否能组成f+1块。{ long long cnt=0; for(int i=0;i
=f;}int main(){ int t; cin>>t; long long high,mid,low,r,res; //注意,因为扩大了100万倍(10万倍也可以,1万倍也A了,越大结果越准确),所以此处用long long,wa了几次。。。 while(t--) { cin>>n>>f; f=f+1; low=0,high=0; for(int i=0;i
>r;a[i]=r*r*pi*1000000;high+=a[i];} //将输入的半径随即转换成体积 while(high>=low) //条件,此处也可以用high-low>exp。 { mid=(high+low)/2; if(judge(mid)){low=mid+1;res=mid;} //不这样写会错 else high=mid-1; } cout<
<
<<(double) res/1000000<

另一种没扩大倍数的代码:(关键还是将半径随即转换成面积,然后来找面积)

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define mod 1000000007#define INF 0x3f3f3f3f#define exp 1e-6#define pi acos(-1.0)using namespace std;double a[20005],sum;int n,f,c;bool judge(double s){ int cnt=0; for(int i=0;i
=f;}int main(){ int t; cin>>t; while(t--) { double maxn=0; cin>>n>>f; sum=0; f+=1; for(int i=0;i
>c;a[i]=pi*c*c*1;if(a[i]>maxn)maxn=a[i];sum+=a[i];} double low=maxn/f,high=sum/f,mid; while(low+0.00001
exp { mid=(high+low)/2; if(judge(mid)) low=mid; else high=mid; } cout<
<
<
<

心得:

熟悉熟悉二分的基本套路,学会分析问题。

转载地址:https://blog.csdn.net/sinat_37668729/article/details/77043549 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Bovine Birthday(思维题)
下一篇:Borg Maze(bfs+prim)

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月03日 04时51分02秒