hdu 1969 Pie 【二分+精度】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1969

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个蛋糕的半径,你有m个朋友,所以总共有m+1个人,现在要分蛋糕,要求每个人分到的大小都是一样的,且每个人的蛋糕都是从一块上切割下来的(不能是2个不同的蛋糕拼起来的),现在问每个人最多能分到多少蛋糕(体积),保留到小数点后4位输出。

分析:套用二分搜索的模型来 试着解决这个问题。令

                 C(x): = 可以将蛋糕分为(m+1)块面积为x的蛋糕

于是这个问题就变成了求满足C(x)条件的最大的x。现在的问题显然重点应该放在我的代码的judge函数上,面积为S_{i}的蛋糕最多可以切出 (int)(S_{i}/x) 段面积为x的蛋糕,所以

                C(x) = (int)(S_{i}/x) 的总和是否大于等于(m+1),如果符合的话说明答案的解在 (mid,r] 之间,否则答案的解在[l,mid] 之间,不断二分,知道精度很小,这是的mid就是最后的答案。

\bigstar特别注意对左右端点的初始化,左端点l初始化为0即可,右端点r要初始化大点,不然过不了(我对右端点初始化为所有的面积和)

代码如下:

#include
#include
#include
#define pi acos(-1)
using namespace std;
const int maxn = 1e4+10;
double p[maxn],a,sum; //p数组存储的是每个蛋糕的高为1的体积
int n,m;
bool judge(double d)
{
    int sum1=0;
    for(int i=0;i= m+1)
        return true; //符合,答案的解在(mid,r]之间
    return false; //不符合,答案的解在[l,mid]之间
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        sum = 0;
        scanf("%d%d",&n,&m);
        for(int i=0;i

 

你可能感兴趣的:(二分)