ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POJ 3122 Pie 二分答案

2021-10-24 17:01:25  阅读:204  来源: 互联网

标签:pie return 3122 ll Pie mid num POJ piece


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 InputSample 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 ;
}



标签:pie,return,3122,ll,Pie,mid,num,POJ,piece
来源: https://blog.csdn.net/qq_42106422/article/details/120936041

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有