pie

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

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

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

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

Output

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

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

这道题的题目大致意思是,有n块饼子,f个人去分。每个人要求饼子大小一样,饼子可以有剩余但是要求每个人的饼子是一块整的或者是从一块上面切下来的(就是说每个人拥有的饼子不能是两个切过的饼组合起来的),题目给了你半径,高度都是一样的都为1。问你怎么分才能使得每个人分得的饼子尽可能的大,这个总人数要记得+1 因为自己也要吃.

这个题目怪难理解的,小伙伴们要多读读题。

懂了之后就是个二分。我们先算出总共的饼子体积sum,算出每个人可分得的最大饼子体积。这个最大平均体积就是我们待会二分的上限

上代码

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
double pi=3.14159265359;
double v[100005];
bool cmp(double a,double b){
     return a>b;
}
int main()
{
   int t;
   scanf("%d",&t);
   double ans;
   while(t--)
   {
       int n,f;
       scanf("%d%d",&n,&f);
       f++;
       int i;
       double rr,sum=0,mx=-9999;
       for(i=0;i<n;i++){
           scanf("%lf",&rr);
           v[i]=rr*rr;
           sum+=v[i];
           mx=max(mx,v[i]);
       }
       //if(f<n) n=f;
       sort(v,v+n,cmp);
      double l,r,mid;
       l=0;
       r=sum/f;
       while(r-l>1e-6){
         mid=(l+r)/2;
          int num=0;
         for(i=0;i<n;i++) num+=(int)v[i]/mid;
         //printf("--%d---",num);
         if(num>=f) l=mid;
         else r=mid;
       }

       printf("%.4f\n",mid*pi);
   }
    return 0;
}

这个特别注意!!!!!!!!!!!!!!!!在POJ上交代码时,最后的输出只能是%.4f 不能是%.4lf。

原因在这里我就不赘述了,也可以用cout,没这么多纠结的了。

至于为什么,大家可以参考 缺省参数扩展。读入时候要%lf,输出的时候用%f。

猜你喜欢

转载自blog.csdn.net/qq_40620465/article/details/81636440
pie