POJ 3122 Pie(二分+贪心)

 

Pie
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22684   Accepted: 7121   Special Judge

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

 
题目意思:

就是公平地分披萨pie

我生日,买了n个pie,找来f个朋友,那么总人数共f+1人

每个pie都是高为1的圆柱体,输入这n个pie的每一个尺寸(半径),如果要公平地把pie分给每一个人(就是所有人得到的pie尺寸一致,但是形状可以不同),而且每个人得到的那份pie必须是从同一个pie上得到的

做法:

输入的是朋友的数量f,分pie是分给所有人,包括自己在内共f+1人

下界low=0,即每人都分不到pie

上界high=maxsize,每人都得到整个pie,而且那个pie为所有pie中最大的

                   (上界就是 n个人n个pie,每个pie还等大)

对当前上下界折中为mid,计算"如果按照mid的尺寸分pie,能分给多少人"

 

求某个pie(尺寸为size)按照mid的尺寸,能够分给的人数,就直接size / mid,舍弃小数就可以

 

code:

#include<stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include<string.h>
using namespace std;
#define max_v 10005
double PI=acos(-1.0);
double v[max_v];
int n,f;
bool test(double x)
{
    int num=0;
    for(int i=0;i<n;i++)
    {
        num+=int(v[i]/x);////每一个蛋糕的体积除以x 必须是完整蛋糕所以int();
    }
    if(num>=f+1)  //当人数大于等于f+1时说明蛋糕可以更大
        return true;
    else
        return false;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int r;
        cin>>n>>f;
        double left=0,right=0,mid=0;
        for(int i=0;i<n;i++)
        {
            cin>>r;
            v[i]=PI*r*r*1.0;//体积都为1
            right=max(right,v[i]);//最大蛋糕就是右边界
        }
        while(fabs(right-left)>1e-6)
        {
            mid=(right+left)*0.5;
            if(test(mid))
            {
                left=mid;//否则mid偏小,下界优化
            }else
            {
                right=mid;//说明mid偏大,上界优化
            }
        }
        printf("%.4f\n",mid);
    }
    return 0;
}

 

posted @ 2018-07-31 17:55  西*风  阅读(202)  评论(0编辑  收藏  举报