poj3122--Pie(二分的精度问题)

Pie

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11178   Accepted: 3899   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

Northwestern Europe 2006

题目大意:给出n个馅饼,m+1个人平均分这些馅饼,每人一块,问每个人最多分多少。

二分可以得到的馅饼大小。double二分

while( (high-low) > eqs )

{

mid = (low+high)/2.0 ;

if( solve(mid) )

{

low = mid  ;

last = mid ;

}

else

high = mid ;

}

注意点:1 double 减法   (high - low)> eqs ,eqs用来卡精度,eqs太大精度降低,eqs太小时间变高。

2 二分的时候不加0.0001, 即low = mid  high = mid;这样分可能会慢一点,但精度会高

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std ;
#define PI 3.1415926535898
#define eqs 1e-5
double s[11000] ;
int n , m ;
double f(double x)
{
    int k = (x+eqs) * 10000 ;
    x = k * 1.0 / 10000 ;
    return x ;
}
int solve(double x)
{
    int i , j , num = 0 ;
    for(i = n-1 ; i >= 0 && (s[i]-x) > eqs ; i--)
    {
        j = s[i] / x ;
        num += j ;
        if( num >= m+1 )return 1 ;
    }
    if( num >= m+1 ) return 1 ;
    return 0 ;
}
int main()
{
    int t , i , k ;
    double low , mid , high , last ;
    while( scanf("%d", &t) != EOF )
    {
        while(t--)
        {
            scanf("%d %d", &n, &m) ;
            for(i = 0 ; i < n ; i++)
                scanf("%lf", &s[i]) ;
            sort(s,s+n) ;
            for(i = 0 ; i < n ; i++)
                s[i] = s[i]*s[i]*PI ;
            low = 0 ;
            high = s[n-1] ;
            while( (high-low) > eqs )
            {
                mid = (low+high)/2.0 ;
                if( solve(mid) )
                {
                    low = mid  ;
                    last = mid ;
                }
                else
                    high = mid ;
            }
            printf("%.4lf\n", last) ;
        }
    }
    return 0;
}

时间: 2024-08-29 06:58:06

poj3122--Pie(二分的精度问题)的相关文章

POJ 3122 Pie (二分+精度)

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

【hoj】2651 pie 二分查找

二分查找是一个很基本的算法,针对的是有序的数列,通过中间值的大小来判断接下来查找的是左半段还是右半段,直到中间值的大小等于要找到的数时或者中间值满足一定的条件就返回,所以当有些问题要求在一定范围内找到一个满足一些约束的值时就可以用二分查找,时间复杂度O(log n); 题目:http://acm.hit.edu.cn/hoj/problem/view?id=2651 因为题目有精度要求,对于浮点数小数点部分会有一定误差,所以可以选择将这些有小数部分的数值扩大e6倍,因为题目要求精确到e-3,之后

POJ 3122 Pie 二分答案

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

POJ3122 Pie(二分)

题目链接:http://poj.org/problem?id=3122 题意:一堆人分蛋糕,每人蛋糕大小一样,求最大能分多少,蛋糕必须是整块整块的,不能两块拼一起.然后注意输入F个人最后要分F+1份. 思路:很简单很水,但是精度处理很恶心,wa了很多发,直接二分蛋糕的半径就行了 AC代码: 1 #include<iostream> 2 #include<vector> 3 #include<cstdio> 4 #include<algorithm> 5 #i

POJ3122 Pie

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15473   Accepted: 5302   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 vari

POJ 1905 Expanding Rods (二分+计算几何+精度处理)

题目地址:POJ 1905 用二分枚举h,然后判断弧长是否符合条件.重点还是在精度问题上,具体看代码吧.. #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #includ

poj 3122 Pie (二分)

<span style="background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 18pt;">Description</span> My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of

poj1905--Expanding Rods(二分,精度伤不起啊)

Expanding Rods Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2014-03-19) Description When a thin rod of length L is heated n degrees, it expands to a new length L'=(1

poj 3122 Pie 二分(最大化平均值)

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