zoukankan      html  css  js  c++  java
  • HDU 1969 Pie (二分查找)

    题目链接:click here~~

    题目大意:n块馅饼分给m+1个人,每一个人的馅饼必须是整块的。馅饼能够被切开。但不能组合,也不一定要所有分完,问你每一个人最大能分到多大体积的馅饼面积。

    【解题思路】:二分,对于每一个V值,我们枚举相应情况下人数P的多少,发现是单调递减的,因此二分查找区间,初始值left=0,right=inf;然后judge函数推断当前mid值是否能使得p>=m,因此累计ans=num[i]/mid,写的时候二分用的是while推断,怎么调试答案就是差了那么一点点。后来索性改成for循环。结果立马就出来了,看来有时候实在不知道那错了,换一种写法可能就对了

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    const int N=1e4+10;
    const int inf=1e10;
    const int eps=1e-6;
    const double pi=acos(-1.0);
    double num[N];
    int n,m;
    bool get(double mid)
    {
        int  ans=0;
        for(int i=1; i<=n; i++){
            ans+=(int)floor(num[i]/mid);
        }
        return ans>=m;//确保m+1个人每人都能得到最大面积
    }
    int main()
    {
        //freopen("1.txt","r",stdin);
        int t,tot=1;
        scanf("%d",&t);
        while(t--)
        {
            double c,sum=0,maxx=0;
            scanf("%d%d",&n,&m);m+=1;
            for(int i=1; i<=n; i++){
                scanf("%lf",&c);
                num[i]=c*c*pi;
            }
            double ll=0,rr=inf;
            //printf("ll==%.4lf rr==%.4lf
    ",ll,rr);
           for(int i=1;i<=100;i++){
                double mid=(ll+rr)/2;
                if(get(mid)) ll=mid;
                else rr=mid;
            }
           // printf("%.4f
    ",mid);
            printf("%.4f
    ",ll);
           // printf("%.4f
    ",rr);
        }
        return 0;
    }
    /*
    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
    */



  • 相关阅读:
    Linux下文件的基本操作
    conpot_usage简要说明
    const声明常量以及特点
    let变量声明以及声明特性
    盒子模型
    文本样式
    行间距
    字体的其他样式
    字体分类
    字体样式
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/6876506.html
Copyright © 2011-2022 走看看