zoukankan      html  css  js  c++  java
  • Pie 杭电1969 二分

    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. 

    InputOne 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. 
    OutputFor 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

    思路 : 刚看到题目时以为是贪心 然后把我给写自闭了,,这个题目要用二分法来做,,找两个极值,,一个是极小值或者最小的蛋糕/m 极大值就是最大的蛋糕啦!

    接下来就是判断 就是让每一块蛋糕的面积/mid 利用保留正数字的原则,就是判断当个人分mid面积是,,每一块蛋糕可以提供几个人的量.
    AC代码:
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    #define N 10010
    #define M_PI 3.1415926535898
    using namespace std;
    double arr[N];
    int n,m;
    
    int judge(double x){
        int cnt=0; 
        for(int i=0;i<n;i++){
            cnt+=arr[i]/x;
        }
        if(cnt>=m) return 1;
        return 0;
    }
    
    int main()
    {
        int t;
        cin>>t;
        while(t--){
            cin>>n>>m;
            m++;
            int r;
            double sum=0;
            for(int i=0;i<n;i++){
                cin>>r;
                arr[i]=M_PI*r*r;//保存每个蛋糕的面积 
            }    
            
            double low,high;
            sort(arr,arr+n);
            
            low=arr[0]/m;//最小取值 
            high=arr[n-1];//最大取值 
            
            double mid=(high+low)/2;
                    
            while(fabs(high-low)>1e-6)
            {
                mid=(high+low)/2;
                
                if(judge(mid))
                {
                    low=mid;
                }
                
                else {
                    high=mid;
                }
            }
            printf("%.4lf
    ",mid);
        }
        return 0;
    }


  • 相关阅读:
    js对象方法
    重要的小知识点
    vs2017引用vue组件中文乱码
    单行文本溢出打点、多行文本溢出打点
    span和input布局时对不齐
    在思科三层交换机上配置DHCP,不同网段/VLAN间互通
    搭建ssm整合
    使用Redis
    MyBatis常用实现方式
    Java 面向对象
  • 原文地址:https://www.cnblogs.com/Accepting/p/11246232.html
Copyright © 2011-2022 走看看