zoukankan      html  css  js  c++  java
  • HDU 1969 Pie【二分】

    【分析】

    “虽然不是求什么最大的最小值(或者反过来)什么的……但还是可以用二分的,因为之前就做过一道小数型二分题(下面等会讲) 考虑二分面积,下界L=0,上界R=ni=1nπ∗ri2。对于一个中值x和一张半径r的饼来说,这张饼能够分出的最多分数显然是:πr2x,所以我们只需要求出ni=1πri2x,判断其与f+1的关系即可 

    特别要注意的一点:π的大小!,因为r104r≤104,所以r2108r2≤108,又因为题目要求精确到三位小数,所以ππ就要精确到10-11,即π=3.14159265358

    Pie

    Problem 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
     
    Recommend
    wangye   |   We have carefully selected several similar problems for you:  1551 2899 2446 2298 2333 
     
    【题意】:有n个饼,半径为riri104ri≤104),分给m+1个人,要求每个人分到的面积是一样的,而且每个人分到的饼是连续的一块(即不能是几个小块凑起来的),求能够分到的最大面积,精确到三位小数
     
    【代码】:
    #include<bits/stdc++.h>
    using namespace std;
    #define Lint long long int
    #define  pi acos(-1)
    const double eps=1e-6;
    const int MAXN=10010;
    double r[MAXN],s[MAXN];
    double n,L,R;
    int T,m;
    bool check(double mid)
    {
        int ret=0;
        for(int i=1;i<=n;i++)
            ret+=(int)(s[i]/mid);
        return ret>=m; //一旦出现Pie的数目大于等于到场人数的话就true 
    }
    int main()
    {
        scanf("%d",&T);
        while( T-- )
        {
            L=R=0;
            scanf("%lf%d",&n,&m),m++;
            //m的原来数值只是朋友的个数,而主人公也是一员,所以加1,加上主人公   
            for(int i=1;i<=n;i++)
            {
                scanf("%lf",&r[i]);
                s[i]=pi*r[i]*r[i];   //这里是直接把所输入的半径变量变成面积,方便下面计算   
                R+=s[i];            // 上界 也可以写成R=max(R,r[i]);这里是吧右边界给设成pie里面的最大的那一个   
            }
            while( R-L>eps )
            {
                double mid=(L+R)/2;
                if( check( mid ) ) 
                //如果返回值为真,那么代表我们可以用此时mid的大小来替换左边界,看是否存在比mid还大但仍然满足所有人都能吃到一样大小的pie,因为题目上说的是尽可能大     
                    L=mid;
                else   
                //一旦发现不符合的话,此时mid的大小就得替换掉右边界,继续在比mid小的大小里面找符合题目条件的大小   
                    R=mid;
            }
            //最后输出l,因为只有满足条件的时候L的值才变,所以对于L来说,它是永远符合题目条件的那个值   
            printf("%.4lf
    ",L);
        }
        return 0;
    }
    二分
     

      

  • 相关阅读:
    “贴身外教”是英语口语的学习方法综合解决方案
    浅谈提高英语口语的最有效方法
    【PERL】Perl默认的内部变量
    Perl——哈希的创建和引用
    linux和windows下,C/C++开发的延时函数,sleep函数
    linux标准库#include <unistd.h>与windows的#include <windows.h>(C语言开发)
    linux 如何显示一个文件的某几行(中间几行)
    使程序在Linux下后台运行
    Perl中关于数组的输出——需要注意的地方
    linux下C/C++,多线程pthread
  • 原文地址:https://www.cnblogs.com/Roni-i/p/8672164.html
Copyright © 2011-2022 走看看