分蛋糕

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/C

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. Fof 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 ≤ 10000: the number of pies and the number
of friends.
• One line with N integers ri with 1 ≤ ri ≤ 10000: 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 oating 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块蛋糕,F个朋友,( 1 ≤ N, F ≤ 10000) 。N块蛋糕都是圆柱体且蛋糕的高度都相同,均为1。要把蛋糕平均分给F+1个人,包括我自己,使每个人得到的蛋糕面积都一样(每个人得到的蛋糕是整块的),求每个人可以分到蛋糕面积最多是多少。

注意:最后输出小数点后4位

分析:

1.二分查找。找出蛋糕面积最大时所在的区间

2. 逐步的缩小范围,最后当左右端点相差小于0.00001时,即可取左端点为最大的面积。

3. 确定是左是右区间时,可以记录每个蛋糕的可以分的人数的和cnt,比较cnt与F的大小

4.注意精度输出和类型

代码:

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std; const int maxn=;
const double pi=acos(-1.0);//pi值,注意是double类型,pi 不能直接写3.14 int n,f; //蛋糕数量和朋友人数
int r[maxn]; //半径
int sum=;
double p[maxn],ma; double max(int x,int y)//没有max函数会出现 WA
{
return x>y?x:y;
} void input()
{
scanf("%d%d",&n,&f);
f=f+; //加自己
for(int i=;i<n;i++)
scanf("%d",&r[i]);
ma=;
for(int j=;j<n;j++)
{
p[j]=pi*r[j]*r[j];
ma=max(p[j],ma);//找出面积最大值
sum+=p[j];
}
} double search() //找蛋糕面积最大时所在区间
{
double l=0.00001; //左边最小值
double r=ma; //右边最大值
double m; //平均值
while(l+0.00001<r) //double类型精确度向后估读一位
{
m=(l+r)/;
int cnt=;
for(int i=;i<n;i++)
cnt+=(int)(p[i]/m);//看能分出来多少块蛋糕
if(cnt<f) //蛋糕数与人数比较
r=m;
else
l=m;
// printf("cnt=%d\n",cnt);
} return l;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
input();
double ans=search();//每人分得的最大面积
printf("%.4lf\n",ans);
}
return ;
}

分蛋糕(C - 二分查找)的更多相关文章

  1. 1044 Shopping in Mars (25分)(二分查找)

    Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...

  2. 从一个NOI题目再学习二分查找。

    二分法的基本思路是对一个有序序列(递增递减都可以)查找时,测试一个中间下标处的值,若值比期待值小,则在更大的一侧进行查找(反之亦然),查找时再次二分.这比顺序访问要少很多访问量,效率很高. 设:low ...

  3. 二分查找-python

    约12年年底的时候,接触了python不到半年的样子,入门是直接实现GUI测试case的.今天面试地平线机器人,发现忘得差不多了- -. 当时的问题是这样的 写一个二分查找是实现,我好像不记得二分查找 ...

  4. 二分查找算法(JAVA)

    1.二分查找又称折半查找,它是一种效率较高的查找方法. 2.二分查找要求:(1)必须采用顺序存储结构 (2).必须按关键字大小有序排列 3.原理:将数组分为三部分,依次是中值(所谓的中值就是数组中间位 ...

  5. 二分查找C++

    #include <iostream> using namespace std; //二分查找:每次都从中间位置寻找,如果找到了就返回,如果没找到, //则分两种情况: //(1)中间元素 ...

  6. 南理第八届校赛同步赛-F sequence//贪心算法&二分查找优化

    题目大意:求一个序列中不严格单调递增的子序列的最小数目(子序列之间没有交叉). 这题证明贪心法可行的时候,可以发现和求最长递减子序列的长度是同一个方法,只是思考的角度不同,具体证明并不是很清楚,这里就 ...

  7. POJ 3273 Monthly Expense(二分查找+边界条件)

    POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...

  8. Yougth的最大化(好题,二分查找 0 1分数规划)

    Yougth的最大化 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价 ...

  9. 【DSA MOOC】有序向量二分查找的三个 版本

    内容来自 TsinghuaX: 30240184X 数据结构(2015秋) 课程的Vector一章,对有序向量的二分查找有三个版本 三个版本的函数原型是一致的,都是 Rank search(T con ...

随机推荐

  1. 转: Promises与Javascript异步编程

    在如今都追求用户体验的时代,Ajax应用真的是无所不在.加上这些年浏览器技术.HTML5以及CSS3等的发展,越来越多的富Web应用出现:在给与我们良好体验的同时,Web开发人员在背后需要处理越来越多 ...

  2. 使用 Java 实现 Comet 风格的 Web 应用

    参考这个: http://www.ibm.com/developerworks/cn/web/wa-cometjava/

  3. POJ 2758 Checking the Text(Hash+二分答案)

    [题目链接] http://poj.org/problem?id=2758 [题目大意] 给出一个字符串,支持两个操作,在任意位置插入一个字符串,或者查询两个位置往后的最长公共前缀,注意查询的时候是原 ...

  4. wndows make images

    配置文件/etc/xen/mywindows.内容如下 import os, re arch_libdir = 'lib' arch = os.uname()[4] if os.uname()[0] ...

  5. 第一次当Uber司机,就拉到漂亮妹纸

    黑马哥的Uber司机端装上很久了,一次活儿也没拉,心里一直有一种当“张师傅”的冲动.黑马哥当Uber司机,肯定不是为了图挣钱,也不是因为Uber有“新约炮神器”的称号,能通过“拉活”来泡妹纸.黑马哥体 ...

  6. YY前端笔试总结

    1.一个元素float以后.为什么要清除浮动?清除浮动的方法有哪些? 浮动确实是经经常使用,也知道清除浮动的必要性.但要我道个所以然,还是得绞尽脑汁.我个人的理解是,当一个元素float以后,就脱离正 ...

  7. BootStrap 智能表单系列 十 自动完成组件的支持

    web开发中,肯定遇到像百度.google这种搜索的功能吧,那智能表单中的自动完成可以做什么呢,下面来揭晓: 1.包含像google.百度等类似的简单搜索 2.复杂结构的支持,比如说 输入产品编号,需 ...

  8. java线程的使用(Runnable)

    在实际项目开发过程中,线程是经常要用到的,特别是为了不影响项目的运行效果. 以下就以实际项目中的简单例子来介绍: public class SystemRedisInfoController exte ...

  9. C++内置类型对象之间的转换

    C++定义了一组内置类型对象之间的标准转换,在必要时它们被编译器隐式地应用到对象上. 隐式类型转换发生在下列这些典型情况下. 1. 在混合类型的算数表达式中 规则:在这种情况下最宽的数据类型成为目标转 ...

  10. Andy's First Dictionary

    Description Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy ...