POJ 3122 Pie 二分答案

Pie [POJ 3122]

题目链接:http://poj.org/problem?id=3122

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 Sample Output
3
3 3
4 3 3
1 24
5
10 5
1 4 2 3 4 5 6 5 4 2
25.1327
3.1416
50.2655

题意:举行生日派对,请来了f个朋友,我们准备了n个派,每个派都是圆柱形的,有自己的半径r,高度都为1,现在想要切开这些派,分成完整的(f+1)块(为什么时f+1,因为他自己也想吃),每块体积相同,问能够分出来的块最大的体积是多少。

思路:首先每个派都是圆柱型,且高度是1,最大的体积就等于最大的块的面积。考虑二分答案,check时,如果二分的mid(表示面积),大于最小的派的面积,则答案不合法,若mid能分出来的块小于(f-1),也不合法。

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std ;
#define ll long long
const ll N = 1e5+9 ;
double a[ N ] , pi = acos(-1);
ll n , f ;
bool cmp( double a , double b ){
    return a > b  ;
}
bool check( double x ){
    ll num = 0 ;
    for( int i = 1 ; i <= n ; i ++ ){
        if( x > a[ i ] ) return 0 ; // 如果mid大于最小的派的面积,那么不合法
        ll tmp = (ll)(a[i]/x); // 计算mid对于a[i]这个派能分多少块
        num += tmp ;//num是总共能分出来的块
        if( num >= f ) return 1 ;//如果num能满足f,答案合法
    }
return 0 ;
}
int main(){
    ll t ; cin >> t ;
    while( t-- ){
        cin >> n >> f ; f ++ ; // 算上了过生日的人自己
        for( int i = 1 ; i <= n ; i ++ ){
            ll r ; cin >> r ;
            a[ i ] = pi*r*r ;
        }
        sort( a+1 , a+1+n , cmp ) ;
        double l , r , mid , ans = 0 ;
        l = 0 , r = 1e10 ;
        for( int i = 1 ; i <= 120 ; i ++ ){
            mid = (l+r)/2.0;
            if( check(mid) ){ l = mid ; ans = mid ; }
            else r = mid ;
        }
        printf("%.6f\n",ans);
    }
return 0 ;
}



上一篇:阿里p8所写的Mysql基础篇:必知必会


下一篇:22.详解过拟合代码