zoukankan      html  css  js  c++  java
  • POJ_3122 经典二分题

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

    我去。。。这个题目已经做过一遍了,今天挂在比赛上,还把它当难题看。。还满脑子想的是背包和动归
    一个经典的二分搜索题,注意人数要+1个,包括主人在内

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    const double pi = acos(-1.0);//这个式子可以直接求出pi
    double area[10005];
    int n,f;
    bool judge(double x)//这个专门用来判断当前mid值是大了还是小了,也是该二分的精髓
    {
        int sum=0;
        for (int i=0; i<n; i++)
        {
            sum+=(int)(area[i]/x);//这个式子用来统计出当前pie可以供给多少块mid大小的蛋糕,非常厉害啊。
        }
        if (sum>=f) return true;//如果可以供给超过总人数,说明当前mid值偏小了,还可以切大一点。
        else
            return false;
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while (t--)
        {
    
            scanf("%d %d",&n,&f);
            double sum=0;
            int r;
            for (int i=0; i<n; i++)
            {
                scanf("%d",&r);
                area[i]=r*r*pi;
                sum+=area[i];
            }
            f++;//根据题目意思,主人也要分一块,注意人数要在原来基础上+1;
            double l=0,right=sum/(f*1.0),mid;//将二分搜索的上界这样定义是理想状态所有pie都能被分到。
            while (right-l>1e-6)//以精度为二分终止的界限
            {
                mid=(l+right)/2;
                if (judge(mid))
                {
                    l=mid;
                }
                else
                    right=mid;
            }
            printf("%.4lf
    ",l);
        }
        return 0;
    }
  • 相关阅读:
    倒计时显示
    Global.asax实现屏蔽ip和图片防盗链
    同一账号不能同时登陆
    javascript中defer的作用
    javascript焦点图
    asp.net自定义分页
    GridVew linkbutton点击时获取点击行label的绑定值
    内存对齐
    深入.NET托管堆(Managed Heap)
    内存对齐1
  • 原文地址:https://www.cnblogs.com/kkrisen/p/3234907.html
Copyright © 2011-2022 走看看