[POJ 008] Pie

description

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 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

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 rounded to 3 digits after decimal point.

Sample input

3 3
4 3 3

Sample output

25.133

answer

#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
using namespace std;

#define eps 1e-5
double pi=acos(-1.0);            // 圆周率π=arcos(-1)
double a[100005];
int n=0, f=0;

bool judge(double mid) {
  int cnt=0;                     // 合格的派计数器
  for (int i=0; i<n; i++)
    cnt+=floor(a[i]/mid);        // 派的面积合格,则计数器增加

  return cnt>=f+1;               // 自己的1份
}

double binarysearch() {
  double l=1, r=a[n-1];
  do {
    double mid = l + (r-l)/2;
    if (judge(mid)) l = mid;
    else r = mid;
  } while (fabs(r-l)>eps);        // 浮点数二分搜索
  
  return l;
}

int main() {
  cin >> n >> f;
  for (int i=0; i<n; i++) {
    cin >> a[i];
    a[i] = a[i]*a[i]*pi;    // 更新数组为派的面积
  }

  sort(a,a+n);              // 排序
  
  cout << fixed << setprecision(3) << binarysearch() << endl;
  return 0;
}
Published 21 original articles · praised 8 · visits 1495

Guess you like

Origin blog.csdn.net/K_Xin/article/details/87865071