UVa: 12097 Pie

二分。依旧相当于求下界。由于答案是浮点数,用while(low<high)极有可能出现死循环,所以用for循环100次,可以保证精确度很高。

一开始WA了好多次,可能是π精确度不够的原因。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <cmath>
#include <algorithm>
#define ll long long
#define INF 2139062143
#define MOD 20071027
#define MAXN 1000005
#define Pi 3.1415926535897932384626433832795
using namespace std;
double compute(double r)
{
    return r*r*Pi;
}
vector<double> vec;
int N,F;
bool Judge(double s)
{
    int cnt=0;
    for(int i=0; i<vec.size(); ++i)
        cnt+=(int)(vec[i]/s);
    if(cnt<F+1) return false;
    return true;

}
double Bsearch(double low,double high)
{
    double mid;
    for(int i=0; i<100; ++i)
    {
        mid=(low+high)/2;
        if(Judge(mid)) low=mid;
        else high=mid;
    }
    return mid;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&N,&F);
        double maxn=0;
        vec.clear();
        for(int i=0; i<N; ++i)
        {
            double r;
            scanf("%lf",&r);
            double s=compute(r);
            maxn=max(s,maxn);
            vec.push_back(s);
        }
        double ans=Bsearch(0,maxn);
        printf("%.4lf\n",ans);
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值