您的位置:首页 > 其它

PIE(二分)

2015-06-07 15:46 375 查看

Pie

TimeLimit:5000/1000MS(Java/Others)MemoryLimit:65536/32768K(Java/Others)
TotalSubmission(s):109AcceptedSubmission(s):52
ProblemDescription
MybirthdayiscomingupandtraditionallyI'mservingpie.Notjustonepie,no,IhaveanumberNofthem,ofvarioustastesandofvarioussizes.Fofmyfriendsarecomingtomypartyandeachofthem
getsapieceofpie.Thisshouldbeonepieceofonepie,notseveralsmallpiecessincethatlooksmessy.Thispiececanbeonewholepiethough.

Myfriendsareveryannoyingandifoneofthemgetsabiggerpiecethantheothers,theystartcomplaining.Thereforeallofthemshouldgetequallysized(butnotnecessarilyequallyshaped)pieces,evenifthisleadstosome
piegettingspoiled(whichisbetterthanspoilingtheparty).Ofcourse,Iwantapieceofpieformyselftoo,andthatpieceshouldalsobeofthesamesize.

Whatisthelargestpossiblepiecesizeallofuscanget?Allthepiesarecylindricalinshapeandtheyallhavethesameheight1,buttheradiiofthepiescanbedifferent.

Input
Onelinewithapositiveinteger:thenumberoftestcases.Thenforeachtestcase:

---OnelinewithtwointegersNandFwith1<=N,F<=10000:thenumberofpiesandthenumberoffriends.

---OnelinewithNintegersriwith1<=ri<=10000:theradiiofthepies.

Output
Foreachtestcase,outputonelinewiththelargestpossiblevolumeVsuchthatmeandmyfriendscanallgetapiepieceofsizeV.Theanswershouldbegivenasafloatingpointnumberwithanabsolute
errorofatmost10^(-3).

SampleInput
3
33
433
124
5
105
1423456542



SampleOutput
25.1327
3.1416
50.2655


题目大意是要办生日Party,有n个馅饼,有f个朋友,接下来是n个馅饼的半径。然后是分馅饼了,
注意咯自己也要,大家都要一样大,形状没什么要求,但都要是一整块的那种,也就是说不能从两个饼中
各割一小块来凑一块,像面积为10的和6的两块饼(饼的厚度是1,所以面积和体积相等),
如果每人分到面积为5,则10分两块,6切成5,够分3个人,如果每人6,则只能分两个了!
题目要求我们分到的饼尽可能的大!

只要注意精度问题就可以了,一般WA都是精度问题

运用2分搜索:

首先用总饼的体积除以总人数,得到每个人最大可以得到的V,但是每个人手中不能有两片或多片拼成的一块饼,

最多只能有一片分割过得饼。用2分搜索时,把0设为left,把V设为right。mid=(left+right)/2;

搜索条件是:以mid为标志,如果每块饼都可以分割出一个mid,那么返回true,说明每个人可以得到的饼的体积可以

大于等于mid;如果不能分出这么多的mid,那么返回false,说明每个人可以得到饼的体积小于等于mid。

(1)精度为:0.000001

(2)pi用反余弦求出,精度更高。






1#include<iostream>
2#include<stdio.h>
3#include<math.h>
4usingnamespacestd;
5doublepi=acos(-1.0);
6intF,N;
7doubleV[10001];
8booltest(doublex)
9{
10intnum=0;
11for(inti=0;i<N;i++)
12{
13num+=int(V[i]/x);
14}
15if(num>=F)
16returntrue;
17elsereturnfalse;
18}
19intmain()
20{
21intt,r;
22doublev,max,left,right,mid;
23scanf("%d",&t);
24while(t--)
25{
26scanf("%d%d",&N,&F);
27F=F+1;
28for(inti=0;i<N;i++)
29{
30scanf("%d",&r);
31V[i]=pi*r*r;
32v+=V[i];
33}
34max=v/F;
35left=0.0;
36right=max;
37while((right-left)>1e-6)//注意这里的精度问题。
38{
39mid=(left+right)/2;
40if(test(mid))
41left=mid;
42elseright=mid;
43}
44printf("%.4f\n",mid);
45}
46return0;
47}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: