zoukankan      html  css  js  c++  java
  • poj3122——二分查找

    poj3122——二分查找

    Pie
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 11698   Accepted: 4050   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个饼给(F+1)个人去瓜分,每个人分的饼必须是整块的,且必须相同(误差小于Exp),求每个人分得的饼的面积最大值
    思路:和3273以及3258一样的做法,3273是找最大的最小,3258是找最小的最大,这里是找平均的最大。以0为下界,平均值avg为上界,进行二分查找。
    不得不吐槽的是这里居然不是卡Exp的精度,而是可Pi的精度。。。Pi=acos(-1.0)才AC。。。
    /**
     * Start at 13:21
     * End at 14:35
     * Problem: poj3122
     * Author: __560
     *
     */
     #include<iostream>
     #include<cstdio>
     #include<cstring>
     #include<cstdlib>
     #include<algorithm>
     #include<math.h>
    
     using namespace std;
    
     const int maxn=10100;
     const int INF=(1<<28);
     const double Pi=acos(-1.0);///题目居然卡Pi,晕死。。。。
     const double Exp=0.000001;
    
     int T;
     int N,F;
    double r[maxn],pie[maxn];
    double tmp_pie[maxn];
    
    bool judge(double limit)
    {
        int group=0;
        //memcpy(tmp_pie,pie,sizeof(pie));///对复件操作
        for(int i=1;i<=N;i++){
            group+=(int)(pie[i]/limit);///此处避免直接对pie进行操作而下一次二分
            /**
            while(tmp_pie[i]>=limit){ ///类似这种循环完全可以用除法取整
                group++;
                tmp_pie[i]-=limit;
            }
            */
        }
        if(group>=F+1) return true;
        return false;
    }
    
    double BinSearch(double low,double high)
    {
        while(fabs(low-high)>Exp){
            double mid=(low+high)/2;
            if(judge(mid)) low=mid;
            else high=mid;
        }
        return high;
    }
    
    int main()
    {
        cin>>T;
        while(T--){
            scanf("%d%d",&N,&F);
            double avg=0;
            for(int i=1;i<=N;i++){
                scanf("%lf",&r[i]);
                pie[i]=r[i]*r[i]*Pi;
                avg+=pie[i];
            }
            avg/=(F+1);
            double ans=BinSearch(0,avg);
            //if(ans-(int)(ans*10000)>=0.5) ans+=0.0001;///四舍五入。。。
            printf("%.4f
    ",ans);
        }
        return 0;
    }
    View Code
    没有AC不了的题,只有不努力的ACMER!
  • 相关阅读:
    logging模块
    解压序列
    python碎片
    python碎片化
    [转]pycharm快捷键
    ios开发之自定义textView
    iOS 开发之字典写入文件
    iOS 开发之内存泄漏问题
    自己写小测试 内容:添加,删除,修改,详情,导出,上传文件,easyui tree树
    使用 jxl 实现复杂的excel 表格导出 java代码
  • 原文地址:https://www.cnblogs.com/--560/p/4380869.html
Copyright © 2011-2022 走看看