poj-3122-pie

Pie
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 21697   Accepted: 6923   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

题目大意:生日聚会,我做了n个蛋糕,它们口味和大小都不同,但是高度相同,来了f个朋友,现在分蛋糕,每个人分到的蛋糕必须是完整的,即不能是拼凑的,现在问,如何分才能让包括我在内的所有人分到的蛋糕体积最大

思路:二分,假设每个人分到蛋糕体积为x,那么x一定不会大于最大的那块蛋糕,最大的那块蛋糕就是二分的右边,然后再与0取中间值,直到找到答案,注以误差,而且此代码只能过c++,不能过g++

代码:

#include<map>
#include<stack>
#include<cmath>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 11000
#define ll long long
#define pi 3.1415926535897932
int a[maxn];
double s[maxn];
int n,f;
int cmp(double a,double b)
{
    return a>b;
}
int ff(double mid)
{
    int i,sum=0;
    for(i=0;i<n;i++)
    {
        sum+=(s[i]/mid);
        if(sum>=f)
            return 1;
    }
    return 0;
}
int main()
{
    int test;
    scanf("%d",&test);
    while(test--)
    {
        int i;
        double mid=0.00,r,l;
        scanf("%d%d",&n,&f);
        f+=1;
        memset(s,0,sizeof(s));
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            s[i]=(double)a[i]*(double)a[i]*pi;
        }
        sort(s,s+n,cmp);
        l=0;
        r=s[0];
        if(f<n)
        {
            n=f;
        }
        while(r-l>1e-5)
        {
           mid=(l+r)/2;
           if(ff(mid))
            l=mid;
           else
            r=mid;
        }
        printf("%.4lf\n",l);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lee371042/article/details/81034923
今日推荐