hdu Pie

Pie

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


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
 

Source
NWERC2006
 

Recommend
wangye


这道题要怎么做呢?(方便描述默认ri即代表体积,y表示人数)通过思考我想通过对一些所给的较大的ri试除2,3...n这样,看是否符合要求来寻找一些规律。但发现这样想下去问题会很麻烦,于是突然想到凌霄大神的一句很经典的话,对题目不要总是纠结于细节,应当尽量想想能否把题目转化到贴近数学的思维方法去解决。于是,想到,既然这题要求最大值,而且最大值与每个ri相关,那么能否列方程解决呢?设答案为ans,这样问题就转化为求满足不等式的最大ans了。然后,设l = 0 < ans < r = maxri,然后就是在l到r的范围内求解,什么时候得到最大的ans呢?因为f(l) <= y,f(r) >= y,当l = r 时,即 f(l) = f (r) = f(ans) = y,这样就得到了解。代码如下:

#include 
#include 
#include 
#define PI	acos(-1.0) //求较精确pi的方法

using namespace std;

double s[10005];

double find(int n,int f,double r){
	double l = 0,ans;
	int count,max;
	while(1){
		if (n > f + 1)	
			max = f + 1;// 一点小优化
		else 
			max = n;
		count = 0;
		ans = (l + r) / 2;
		for(int i = 0;i < max;i++)
			count += (int)(s[i] / ans);
		if (r - l <= 1e-6)	return ans;
		if (count >= f + 1)
			l = ans;
		else 
			r = ans;
	}
}

bool cmp(double a,double b){
	return a > b;
}

int main (){
	int n,f,i,t,r;
	double rmax;
	scanf("%d",&t);
	while (t--){
		r = 0;
		scanf("%d%d",&n,&f);
		for(i = 0;i < n;i++){
			scanf("%d",&r);
			s[i] = PI * r * r;
			rmax = rmax >= s[i] ? rmax : s[i];
		}
		sort(s,s + n,cmp);
		printf("%.4lf\n",find(n,f,rmax));
	}
	return 0;
}

l与r的逼近是通过二分进行的,二分的用法其实挺灵活的,比如最常见的二分查找,解个方程,这里的求最优解不等式,甚至在前面一道xtudp题中查找离某一个点最近的点都用到了二分,这里大概归纳一下:凡是给定了一个范围的,找有一个需要符合条件的值,而且查找的范围中的数都是线性排列的,即被缩小的范围的那一半中的数一定不会是符合条件的解,都可以用到二分。而且对整数的查找二分的复杂度很低log(n),实数范围一般题目也会限定较小的初始范围,所以一般也不会超时

你可能感兴趣的:(hdu Pie)