HDU1969:Pie(二分)

Pie

Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 59   Accepted Submission(s) : 31

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem 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
#include <iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
int t,n,f,i,num,x;
double pi=acos(-1.0);   //不能写3.1415926
double l,r,mid;
double s[10005];
int cmp(double a,double b)
{
    return a>b;
}
int main()
{
     scanf("%d",&t);
     for(;t>0;t--)
     {
         scanf("%d%d",&n,&f);
         f++;
         double maxn=0,sum=0;
         for(i=1;i<=n;i++)
         {
             scanf("%d",&x);
             s[i]=pi*x*x;
             maxn=max(maxn,s[i]);
             sum=sum+s[i];
         }
       l=maxn/f;
       r=sum/f;
       while(l+0.00001<r)
       {
           mid=(l+r)/2;
           num=0;
           for(i=1;i<=n;i++)   num+=(int)(s[i]/mid);
           if (num>=f)  l=mid;
           if (num<f)  r=mid;
       }
       printf("%.4lf\n",l);
       //printf("%lf\n",acos(-1.0));;
     }
    return 0;
}
时间: 2024-10-05 06:39:47

HDU1969:Pie(二分)的相关文章

【hoj】2651 pie 二分查找

二分查找是一个很基本的算法,针对的是有序的数列,通过中间值的大小来判断接下来查找的是左半段还是右半段,直到中间值的大小等于要找到的数时或者中间值满足一定的条件就返回,所以当有些问题要求在一定范围内找到一个满足一些约束的值时就可以用二分查找,时间复杂度O(log n); 题目:http://acm.hit.edu.cn/hoj/problem/view?id=2651 因为题目有精度要求,对于浮点数小数点部分会有一定误差,所以可以选择将这些有小数部分的数值扩大e6倍,因为题目要求精确到e-3,之后

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 (二分)

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

HDU 1969 Pie 二分

1.题意:一项分圆饼的任务,一堆圆饼共有N个,半径不同,厚度一样,要分给F+1个人.要求每个人分的一样多,圆饼允许切但是不允许拼接,也就是每个人拿到的最多是一个完整饼,或者一个被切掉一部分的饼,要求你算出每人能分到的饼的体积最大值.输入数据依次给出,测试数据组数T,每组数据中,给出N,F,以及N个圆饼的半径.输出最大体积的数值,精确到小数点后四位. 2.分析:一看是这种输出就知道用二分写会很高效,这里对"能分出的最大体积值"进行二分.首先,这个值有界,最大值为总体积除以总人数的值,即Σ

HDU 1969 Pie (二分查找)

题目链接:click here~~ 题目大意:n块馅饼分给m+1个人,每一个人的馅饼必须是整块的.馅饼能够被切开.但不能组合,也不一定要所有分完,问你每一个人最大能分到多大体积的馅饼面积. [解题思路]:二分,对于每一个V值,我们枚举相应情况下人数P的多少,发现是单调递减的,因此二分查找区间,初始值left=0,right=inf;然后judge函数推断当前mid值是否能使得p>=m,因此累计ans=num[i]/mid,写的时候二分用的是while推断,怎么调试答案就是差了那么一点点.后来索性

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

HDU1969 Pie(二分搜索)

题目大意是要办生日Party,有n个馅饼,有f个朋友,接下来是n个馅饼的半径.然后是分馅饼了, 注意咯自己也要,大家都要一样大,形状没什么要求,但都要是一整块的那种,也就是说不能从两个饼中 各割一小块来凑一块,像面积为10的和6的两块饼(饼的厚度是1,所以面积和体积相等), 如果每人分到面积为5,则10分两块,6切成5,够分3个人,如果每人6,则只能分两个了! 题目要求我们分到的饼尽可能的大! 只要注意精度问题就可以了,一般WA 都是精度问题 运用2分搜索: 首先用总饼的体积除以总人数,得到每个

二分算法和三分算法

二分算法: hdu1969    PIE 题意:f+1个人分n分pie,pie只能切开不能从组,也就是说每个人拿到的必须是一个整块,不能是多个碎块.要求每个人分的的大小一样.n份pie的大小不同.目标是求出没人可能吃到的最大大小V. 分析抽象:首先条件是必须够n个人吃,要求大小一样的情况下求最大的V.关系是随着V的增大,所能support的人越少.这里有一个非递增的关系.所以而已考虑二分法.问题对精度的要求是二分法使用的关键.要求保留4位小数,所以我们统一乘上1000000来计算. #inclu