NBUT-2014暑期集训专题练习1 -> 二分法 J - J
来源:互联网 发布:完美告白知乎 编辑:程序博客网 时间:2024/06/14 02:58
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 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
标准的二分,注意精度,
首先用mid去判断,每个蛋糕切分后,每一份为mid,最多切分(int)(area/mid)份,这是一份蛋糕的,那么所有的就是求和,然后再和F+1(注意是F+1,自己也要的啊)
比较,如果大于等于F+1,那么说明每人拿mid是可以的,答案或许会更大,
如果小于F+1,那么说明mid太大了,这就是二分的细节了
#include<stdio.h>
#include<math.h>
const double PI=acos(-1.0);
double area[10010];//蛋糕面积
int F,n;
bool judge(double each_area)
{
int seg=0;
for(int i=0;i<n;i++)
seg+=(int)(area[i]/each_area);//把每块蛋糕可以分成的份数相加,注意
return seg>=F+1;//别忘了自己
}
void huafeng(double low,double high)
{
double mid;
while(high-low>1e-6)
{
mid=(low+high)/2;
if(judge(mid))
low=mid;
else
high=mid;
}
printf("%.4f\n",mid);
}
int main()
{
int t;
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%d%d",&n,&F);
int r;
double max=-1;
for(int i=0;i<n;i++)
{
scanf("%d",&r);
area[i]=(PI*r*r);
if(area[i]>max)
max=area[i];
}
huafeng(0,max);//每个人最多拿到的蛋糕不会大于面积最大的蛋糕,所以 //可以做上限
}
}
return 0;
}
- NBUT-2014暑期集训专题练习1 -> 二分法 J - J
- NBUT-2014暑期集训专题练习1 -> 二分法 A题
- NBUT-2014暑期集训专题练习1 -> 二分法E - E
- NBUT-2014暑期集训专题练习1 -> 二分法 H - H
- NBUT-2014暑期集训专题练习1 -> 二分法B - B
- NBUT-2014暑期集训专题练习1 -> 二分法L - L
- NBUT-2014暑期集训专题练习1 -> 二分法F - F
- NBUT-2014暑期集训专题练习1 -> 二分法 N - N
- nbut线段树专题 J - Atlantis
- 2015年ALPC暑期专题练习I (计算几何) J Kadj Squares
- 【字符串入门专题1】J
- j集训8.15
- 2013秋13级预备队集训练习1 J - Hangman Judge
- 【搜索入门专题1】 hdu1242 J
- 【二分匹配入门专题1】J
- 1010-J专题三
- 专题三 Problem J
- 专题四 Problem J
- NOJ [1120] Reimu\'s Teleport
- POJ Maximum sum
- NOJ [1132] Evil Wheel
- NBUT-2014暑期集训专题练习1 -> 二分法 A题
- NBUT-2014暑期集训专题练习1 -> 二分法E - E
- NBUT-2014暑期集训专题练习1 -> 二分法 J - J
- NBUT-2014暑期集训专题练习1 -> 二分法 H - H
- NBUT-2014暑期集训专题练习1 -> 二分法B - B
- NBUT-2014暑期集训专题练习1 -> 二分法L - L
- NBUT-2014暑期集训专题练习1 -> 二分法F - F
- nbut线段树专题E - Just a Hook
- nbut线段树专题F - A Simple Problem with Integers
- nbut线段树专题I - Balanced Lineup
- <vim>VIM各种tips