您的位置:首页 > 其它

UVA - 12097 (pie 二分求解逼近值)

2014-12-30 10:29 423 查看
一开始开到y-x >1e-9直接超时;看来不可过度的去二分啊;

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
typedef long long LL;
const double P = acos(-1.0);
const int maxn = 10010;
double a[maxn];
int n,F;
int Judge(double x){
  int cnt=0;
  for(int i=1;i<=n;i++){
    cnt+=floor(a[i]/x);
  }
  return cnt >= F ? 1 : 0;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&F);
        F++;
        double x=0,y=maxn;
        for(int i=1;i<=n;i++){
            scanf("%lf",&a[i]);
            a[i]=P*a[i]*a[i];
            y=max(y,a[i]);
        }
        while(y-x>1e-5){
            double m=(y+x)/2;
            if(Judge(m)) x=m;
            else y=m;
        }
        printf("%.5lf\n",x);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: