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\n",ll,rr);
       for(int i=1;i<=100;i++){
            double mid=(ll+rr)/2;
            if(get(mid)) ll=mid;
            else rr=mid;
        }
       // printf("%.4f\n",mid);
        printf("%.4f\n",ll);
       // printf("%.4f\n",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
*/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值