HDU 1969 Pie

D - Pie
Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u
Submit Status

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
 
题目大概意思是说:有F+1个人,N块Pie,每块Pie的半径可以相同也可以不同,每个人能得到的均分一小块Pie最大面积是多大?不同的Pie之间不能拼接。
这题一开始我没想好从哪里下手,困扰了好久。后来明白,既然Pie之间不能拼接,那最后均分的一定是一个在0-Pie(max)之间的值。
得到的思路是:将所有的Pie面积排序,得到Pie(max),在0-Pie(max)之间求二分,每次将得到的值进行验证,判断这个值能不能通过将现在的所有Pie分成F+1份得到,如果可以,尝试让二分的值更大,如果不行,说明这个值过大了,让二分区间的值减小。

#include<iostream>
#include<stdio.h>
#define _USE_MATH_DEFINES
#include<math.h>
#include<algorithm>
using namespace std;
double ri[10005];
int N, F;
int isok(double x)
{
    int sum = 0;
    for (int i = 0; i < N; i++)
    {
        int flag = ri[i] / x;
        sum += flag;
    }
    if (sum >= F+1)
        return 1;
    else
    return 0;
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        cin >> N >> F;
        for (int i = 0; i < N; i++)
        {
            cin >> ri[i];
            ri[i] = M_PI*ri[i] * ri[i];
        }
        sort(ri, ri + N);
        double lit = pow(10.0,-5);
        double max = ri[N - 1];
        double min = 0;
        double mid = (max + min) / 2;
        while (fabs(max-min) > lit)
        {        
            if (isok(mid))
                min = mid ;
            else
                max = mid;
            mid = (max + min) / 2;
        }
        printf("%.4lf\n", min);
    }
    return 0;
}




你可能感兴趣的:(HDU 1969 Pie)