Pie(HDU1969)

Pie

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22099    Accepted Submission(s): 7551


 

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:
---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

题意真的不太好懂  看了半天最后是从博客那里了解题意的


题意:N个派,F个人,注意实际上要带上自己,所以是F+1个人。然后这堆人要求每个人拿的饼的面积是一样的,并且饼有不同口味,如果一个人拿到的是切了的饼,那么这个饼只能是一个口味的,不能和其他口味的饼拼起来来凑面积


思路:自己做的时候确实想到二分,先sort,然后在(0,v[max])中二分。但是自己的卡点是怎么确定这个check函数,即怎么判断二分的面积是否满足。

如何去判断满足,这是这道题要告诉我们的。


总结:当我们要判断一个物品能被分成好多份,并且物品可以剩余,我们可以通过(r[i]/mid)的累加和来转化,转化成判断是否满足人的数量。


代码总结:精度的问题:r-l的精度一般多开两位(经验),少了容易wa,大了容易T。             圆周率的acos(-1.0)更准确


#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
double const pie=acos(-1.0);
LL t,np,nr;//np代表pie的数量,nr代表人的数量 
int const maxn=10010;
double  k[maxn];
//[Error] invalid types 'double[int]' for array subscript
///变量数组名和其他变量重复...百度百到的都什么玩意 
double bsearch(double l,double r)
{
	while(r-l>1e-6)///开高可能T,开低不太行,一般多开两位 
	{
		int  ans=0; 
		double mid=(l+r)/2;
		
		for(int i=1;i<=np;i++)
		 {
		 	ans=ans+(k[i]/mid);
		 } 
		
		if(ans>=nr) l=mid;
		else r=mid;
	}
return r;	
}


int main(void)
{
	cin>>t;
	while(t--)
	{
		cin>>np>>nr;
		nr=nr+1;//加上自己 
		for(int i=1;i<=np;i++)
		{
			cin>>k[i];k[i]=k[i]*k[i]*pie;
		}
		sort(k+1,k+np+1);
		
		double p=bsearch(0,k[np]);
		printf("%.4f\n",p);
	}
return 0;	
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/104880643
pie