zoukankan      html  css  js  c++  java
  • poj3122 binary search 实数区间

    Pie
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 14536   Accepted: 4979   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
    题目大意:国人很多事情都追求公平,分饼也是如此,现在这里有n个饼,每一个饼都可以看做一个圆柱体,高都是1,但是半径不同,
    每一个人都可以分到某个饼的一部分,但是只能要一部分,而不能要好几块饼,最终结果必须保证每个人分到的饼的体积(面积)相等,
    问你每个人能够获得的饼的最大面积是多少。
    思路分析:首先数据量很大,如果用暴力枚举肯定会超时,很显然我们应该采用二分逼近的方法来确定答案,但是在实际操作的时候还是
    出了一些问题,首先,二分精度不能够太高,1e-5就可以,精度太高会超时,其次,关于π,如果写做3.1415926会被精度卡掉,将pi
    定义为acos(-1.0)就可以了。
    代码:
    #include<map>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<cstdio>
    #include<vector>
    #include<string>
    #include<cstdlib>
    #include<cstring>
    #include<climits>
    #include<iostream>
    #include<algorithm>
    #include <cmath>
    #define LL long long
    using namespace std;
    const int maxn=10000+100;
    const double pi=acos(-1.0);
    double a[maxn];
    int n,f;
    double s(double r)
    {
        return pi*r*r;
    }
    bool check(double x)
    {
        int  t=0;
        for(int i=0;i<n;i++)
        {
            double p=s(a[i]);
            while(p>=x)
            {
                p-=x;
                t++;
                if(t>=f+1) return true;
            }
        }
        return false;
    }
    int main()
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            double sum=0.0;
            scanf("%d%d",&n,&f);
            for(int i=0;i<n;i++)
            {
                scanf("%lf",&a[i]);
                sum+=s(a[i]);
            }
            sort(a,a+n);
            double l=0.0,r=sum/(f+1)*1.0;
            double ans=0;
            while(l+0.000001<=r)
            {
                double mid=(l+r)/2;
                if(check(mid)) ans=mid,l=mid;
                else r=mid;
            }
            printf("%.4lf ",ans);
        }
        return 0;
    }
  • 相关阅读:
    Java 在方法和作用域内的内部类
    java 内部类和向上转型
    java 内部类使用 .this 和 .new
    java innerclasses(内部类)
    Java过滤器与SpringMVC拦截器之间的关系与区别
    Maven异常:Could not find artifact
    soapUI测试webservice(参数为xml格式的处理方式)
    maven仓库加载本地包依赖
    spring data jpa的学习总结
    Java 中常用缓存Cache机制的实现
  • 原文地址:https://www.cnblogs.com/xuejianye/p/5513587.html
Copyright © 2011-2022 走看看