zoukankan      html  css  js  c++  java
  • POJ-3122-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 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


    1. 二分枚举时间。由于最后和面积有关系。所以一开始数组就保存的是平方值。
    2. 判断过程中,计算饼子数量。如果数量大于人数。返回true。表示mid比较小。此时保存的是l值。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 using namespace std;
     5 #define M 10002
     6 const double PI=3.14159265359;
     7 const double eps = 1e-7;
     8 double a[M];
     9 int n,f;
    10 bool cmp(int a,int b)
    11 {
    12     return a>b;
    13 }
    14 bool check(double mid)
    15 {
    16     int sum=0;
    17     for(int i =0;i<n;i++)
    18     {
    19         if(a[i]>=mid)
    20             sum+=(int)a[i]/mid;
    21     }
    22     return sum>=f;
    23 }
    24 int main()
    25 {
    26     int T;
    27     scanf("%d",&T);
    28     while(T--)
    29     {
    30         scanf("%d%d",&n,&f);
    31 
    32         double maxx = 0;
    33         for(int i=0;i<n;i++)
    34         {
    35                 scanf("%lf",&a[i]);
    36                 a[i] = a[i]*a[i];
    37                 maxx= max(maxx,a[i]);
    38         }
    39 
    40         f++;
    41         double l=0,r = maxx,mid;
    42         while(r-l>=eps)
    43         {
    44             mid = (l+r)/2;
    45             if(check(mid))
    46                 l = mid;
    47             else
    48                 r = mid;
    49         }
    50         printf("%.4f
    ", l *PI);
    51     }
    52     return 0;
    53 }


    注:转载请注明出处
  • 相关阅读:
    Python基础
    SQL脚本
    PDF技术之-jasperreports的使用
    redis缓存和mysql数据库如何保证数据一致性
    理解MySQL的乐观锁,悲观锁与MVCC
    intellj idea创建maven项目一直处于加载的解决问题
    Linux目录详解,软件应该安装到哪个目录
    总结
    总结
    总结
  • 原文地址:https://www.cnblogs.com/1625--H/p/9368652.html
Copyright © 2011-2022 走看看