poj 3122 Pie 二分(最大化平均值)

Pie

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11776   Accepted: 4076   Special Judge

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

题意好麻烦:有n个pi,f个朋友,还有一个自己,给出每个pi的大小,每个pi可以切成几个蛋糕,而且所有pi切出的蛋糕都必须一样大,每个人至少分一个蛋糕,求最大的蛋糕的体积。高均为1.

#include <iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define maxn 25100000
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
int a[maxn];
int n,f;
bool ok(double x){
   int ans=0;
   for(int i=0;i<n;i++){
      ans+=a[i]*a[i]*pi/x;
   }
   if(ans<f+1)return true;
   else return false;
}
int main()
{
    int t;
    freopen("in.txt","r",stdin);
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&f);
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
        }
        double ua=0.0;
        double ub=1000000000;
        double mid;
       /*
        while(ub-ua>eps){        //两种方法均可,此题对精度要求比较低
            mid=(ua+ub)/2;
            if(ok(mid))ub=mid;
            else ua=mid;
        }*/
        for(int i=0;i<100;i++){
             mid=(ua+ub)/2;
            if(ok(mid))ub=mid;
            else ua=mid;
        }
        printf("%.4f\n",mid);
    }
}
时间: 2024-12-21 12:37:42

poj 3122 Pie 二分(最大化平均值)的相关文章

POJ 3122 Pie 二分答案

Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9653   Accepted: 3478   Special Judge 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 v

POJ 3122 Pie (二分+精度)

Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11240   Accepted: 3919   Special Judge 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

poj 3122 Pie (二分)

<span style="background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 18pt;">Description</span> My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of

poj 3111 K Best 最大化平均值 二分思想

poj 3111 K Best 最大化平均值 二分思想 题目链接: http://poj.org/problem?id=3111 思路: 挑战程序竞赛书上讲的很好,下面的解释也基本来源于此书 设定条件C(x):=可以选择使得单位重量的价值不小于x 如何判定C(x)是否可行 假设选了某个物品的集合是S,那么单位重量的价值是:\[ \sum\limits_{i \in S} {v_i } /\sum\limits_{i \in S} {w_i } \] 因此就变成了判断是否存在S满足下面的条件:\[

POJ 3111 K Best 二分 最大化平均值

1.题意:给一共N个物品,每个物品有重量W,价值V,要你选出K个出来,使得他们的平均单位重量的价值最高 2.分析:题意为最大化平均值问题,由于每个物品的重量不同所以无法直接按单位价值贪心,但是目标值有界且能判断与最后答案的大小关系,所以用二分来做 3.代码: 1 # include <iostream> 2 # include <cstdio> 3 # include <cmath> 4 # include <algorithm> 5 using names

poj 3122 (二分查找)

链接:http://poj.org/problem?id=3122 Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10448   Accepted: 3694   Special Judge Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N

POJ 3111 K Best(最大化平均值)

题目链接:click here~~ [题目大意]有n个物品的重量和价值分别是Wi和Vi,从中选出K个物品使得单位重量的价值最大,输出物品的编号 [解题思路]:最大化平均值的经典.参见click here~~ 代码: //#include <bits/stdc++.h> #include <stdio.h> #include <math.h> #include <string.h> #include <iostream> #include <

POJ 2976 3111(二分-最大化平均值)

POJ 2976 题意 给n组数据ai,bi,定义累计平均值为: 现给出一个整数k,要求从这n个数中去掉k个数后,最大累计平均值能有多大?(四舍五入到整数) 思路 取n?k个数,使得累计平均值最大. 定义C(x)表示能否取得n?k个数,使得累计平均值≥x.然后二分搜索最大的x. 可以这样判断可行性: 只需要从大到小选取n?k个(100?ai?x?bi)并求和sum,根据sum≥0来判断(上述的S表示n?k个元素下标的集合) #include <iostream> #include <al

POJ 3122 Pie(简单二分)

Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12486   Accepted: 4310   Special Judge 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