杭电 1969 Pie 二分法 附翻译 解题思路

Pie

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


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
 

问题描述

我的生日快到了,并且我习惯上(traditionally)准备派。不仅仅是一个派,我有N个各种(various)味道各种大小的派。F个朋友要参加我的派对,并且每人都会得到一份派。他们每人都应该拿到一块派,并不是那种好几小片的看起来乱糟糟的。这应该是一整块派。

 如果其他人分到了较大的一块派,他们会生气并开始抱怨。所以他们都应该得到一块相同大小但不必是相同口味的派,虽然会导致一些派被糟蹋(spoil),也好过糟蹋派对。当然,我也想要一块派,并且这一块也得和其他人一样大。

最大的问题是我们每人能得到多大一块派?所有派都是圆柱形的(cylindrical),并且它们都有相同的高——1,但是这些派的半径不同。

 

输入

第一行为一个正整数:是测试的总数据。然后是每组测试数据:
一行有两个整数N和F,1 <= N, F <= 10000:代表派的数量个朋友的数量。
一行有N个ri,1 <= ri <= 10 000:表示派的半径。
 
输出
对每组测试数据,输出一行V表示我和朋友们能获得的最大体积的派,答案应是精确到小数点第四位的浮点数。
 
解题思路:
这道题的思路是需要用二分法来精确,事实上也确实如此。在这里right的值为未精确的派的体积,作为右边界。左边界必须要从0开始,如果为right的一半,则不行。并且实际计算会发现,其实两者的误差并不大,也就在1作用。sum作为蛋糕总数,如果总数较大,则说明每人的派分少了,左边界太小,令left为mid,总数较少则反之。另外,其中的1e-7作为精确度对数值精确。
 
细节:
1.精度至少为1e-5
2.自己也要吃派,所以f应加1
3.π的值要使用acos()函数
 
acos()函数介绍
头文件:#include
acos() 函数返回一个以弧度表示的反余弦值,其原型为:
【参数】x 为余弦值,范围为 -1 到 1 之间,超出此范围将会导致错误,并设置 errno 的值为 EDOM.
【返回值】返回 0 至 π 之间的计算结果,单位为弧度,在函数库中角度均以弧度来表示。
#include
#include
#define PI acos(-1)
double v[10010];


int main(){
	int n,f,t;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&f);
		int i,r;
		double right=0.0,left=0.0,mid;
		for(i=0;i1e-7){
			int sum=0;
			mid=(right+left)/2.0;
			for(i=0;i=f) left=mid;
			else right=mid;
		}
		printf("%.4lf\n",mid);
	}
	return 0;
}

你可能感兴趣的:(c语言,HDOJ,二分法)