hdu 1969 Pie(贪心+二分查找)(简单)

Pie

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 5628    Accepted Submission(s): 2184

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

题意:

生日家里来了F个朋友,他家里有好N个Pie,主人希望把Pie分出F+1份(自己也要一个),要求体积相同,所有的Pie不需要都分完,问你每个人最大能分到多大体积的Pie。

思路:

贪心+二分查找

代码:

#include<iostream>
#include<iomanip>
#include<cstdio>
using namespace std;
const double pi=3.1415926535897932;
int t,n,f;
int r[10050];

int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>f;
        double r[10050];
        f++;
        double sum=0.0;
        for(int i=1;i<=n;i++)
        {
            cin>>r[i];
            r[i]*=r[i];
            sum+=r[i];
        }
        double low=0.0;
        double high=sum/f;
        double mid;
        while(high-low>0.000001)
        {
            mid=(low+high)/2;
            int num=0;
            for(int i=1;i<=n;i++)
                num+=(int)(r[i]/mid);
            if(num<f)
                high=mid;
            else
                low=mid;
        }
        printf("%.4lf\n",pi*mid);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-10 15:10:12

hdu 1969 Pie(贪心+二分查找)(简单)的相关文章

hdu 1969 Pie(二分查找)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1969 Pie Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4513    Accepted Submission(s): 1819 Problem Description My birthday is coming up and trad

贪心/二分查找 BestCoder Round #43 1002 pog loves szh II

题目传送门 1 /* 2 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 3 当然有可能两个数和超过p,那么an的值最优,每次还要和an比较 4 注意:不能选取两个相同的数 5 反思:比赛时想到了%p和sort,lower_bound,但是还是没有想到这个贪心方法保证得出最大值,还是题目做的少啊:( 6 */ 7 #include <cstdio> 8 #include <algorithm>

hdu 4768 Flyer(二分查找)

Flyer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1537    Accepted Submission(s): 552 Problem Description The new semester begins! Different kinds of student societies are all trying to adv

HDU 1969 Pie (二分查找)

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

hdu 5249 KPI 【二分查找】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5249 分析:这道题是2015百度之星初赛1的4题 这道题不算太难当时队友做出来了,不过费了老大劲,其实我从中能够吸取教训的, 原因是,我一看这道题就是数据结构的,然后和队友想的一样二分查找,但是从中 遇到了一系列的问题: 首先储存数据我们不能用带有下标的数组,因为题目中的数据是可删可添的这样如果用 数组的话时间复杂度是相当高的,因为如果按大小插入,就要移动其他数据,如果在 尾部添加,就要对其排序,因

HDU 3763 CD【二分查找】

解题思路:给出两个数列an,bn,求an和bn中相同元素的个数因为注意到n的取值是0到1000000,所以可以用二分查找来做,因为题目中给出的an,bn,已经是单调递增的,所以不用排序了,对于输入的每一个b[i],查找它在an数列中是否存在.存在返回值为1,不存在返回值为0 CD Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 627

hdu 4190 Distributing Ballot Boxes(贪心+二分查找)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4190 Distributing Ballot Boxes Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1065    Accepted Submission(s): 536 Problem Description Today, bes

HDU 1969 Pie 二分

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

hdu 1969 Pie

Pie Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6752    Accepted Submission(s): 2564 Problem Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I h