UVALive - 3635 Pie

来源:互联网 发布:网络层的协议内容 编辑:程序博客网 时间:2024/04/30 04:23

Problem C - Pie

Time limit: 1 second

My birthday is coming up and traditionally I'm serving pie. Not justone pie, no, I have a number N of them, of various tastes andof various sizes. F of my friends are coming to my party andeach 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 onewhole pie though.

My friends are very annoying and if one of them gets a bigger piecethan the others, they start complaining. Therefore all of them shouldget equally sized (but not necessarily equally shaped) pieces, even ifthis leads to some pie getting spoiled (which is better than spoilingthe party). Of course, I want a piece of pie for myself too, and thatpiece should also be of the same size.

What is the largest possible piece size all of us can get? All thepies 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. Thenfor each test case:

  • One line with two integers N and F with 1 ≤N, F ≤ 10000: the number of pies and the number offriends.
  • One line with N integers ri with 1 ≤ri ≤ 10000: the radii of the pies.

Output

For each test case, output one line with the largest possible volumeV such that me and my friends can all get a pie piece of sizeV. The answer should be given as a floating point numberwith an absolute error of at most 10-3.

Sample Input

33 34 3 31 24510 51 4 2 3 4 5 6 5 4 2

Sample Output

25.13273.141650.2655
The 2006 ACM Northwestern European Programming Contest




分析:
依旧是刘汝佳大白书的题,感觉就是double型的二分,比较简单。
ac代码如下:
#include <iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

const double pi=acos(-1.0);
const int maxn=10000+5;
int n,f;
double a[maxn];

bool ok(double area)
{
    int sum=0;
    for(int i=0;i<n;i++)
    sum+=floor(a[i]/area);
    return sum>=f+1;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&f);
        double maxa=-1;
        for(int i=0;i<n;i++)
        {
            int r;
            scanf("%d",&r);
            a[i]=pi*r*r;
            maxa=max(maxa,a[i]);
        }
        double L=0,R=maxa;
        while(R-L>1e-5)
        {
            double M=(L+R)/2;
            if(ok(M)) L=M;
            else R=M;
        }
        printf("%.5lf\n",L);
    }
    return 0;
}

0 0
原创粉丝点击