码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-23 13:29:39      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:

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

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

标签:

原文地址:http://blog.csdn.net/winddreams/article/details/43053069

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!