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.

InputOne 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. 
OutputFor 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

题解:

每个人能分到的最大的面积max=总的面积/(朋友分数目+1)

begin:

left = 0;  right = max;

mid = (left + right) / 2;

judge:

for(int i = 0; i < n; i++)

  num += 每块饼的面积 / mid;

finally:

while(right - left > 1e-6)

虽然明白题意,但是自己写的代码,还是找不到bug在哪?

 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#define PI acos(-0.1)
using namespace std; int n, f, a[];
bool solve(double m)
{
int num=;
for(int i = ; i < n; i++)
num += int(a[i]*a[i]*PI/m);
if(num >= f+)
return true;
else
return false;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
double mid, left, right, sum=0.0;
scanf("%d %d", &n, &f);
for(int i = ; i < n; i++)
scanf("%d", &a[i]);
for(int i = ; i < n; i++)
sum += a[i]*a[i]*PI;
right = sum/f;
left = 0.0;
while((right - left) > 0.000001)
{
mid = (right+left)/;
if(solve(mid))
left = mid;
else
right = mid;
//printf("%d\n", mid);
}
printf("%.4f\n", mid);
} return ;
}

AC代码:

 #include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
double pi = acos(-1.0);
int F,N;
double V[];
bool test(double x)
{
int num=;
for(int i = ; i < N;i++)
{
num += int(V[i]/x);
}
if(num>=F)
return true;
else return false;
}
int main()
{
int t,r;
double v,max,left,right,mid;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&N,&F);
F = F+;
for(int i = ; i < N; i++)
{
scanf("%d",&r);
V[i] = pi*r*r;
v += V[i];
}
max = v/F;
left = 0.0;
right = max;
while((right-left)>1e-)//注意这里的精度问题。
{
mid = (left+right)/;
if(test(mid))
left = mid;
else right = mid;
}
printf("%.4f\n",mid);
}
return ;
}

B - Pie (二分)的更多相关文章

  1. HDU 1969 Pie(二分查找)

    Problem Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no ...

  2. HDU 1969 Pie(二分,注意精度)

    Pie Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  3. 【POJ 3122】 Pie (二分+贪心)

    id=3122">[POJ 3122] Pie 分f个派给n+1(n个朋友和自己)个人 要求每一个人分相同面积 但不能分到超过一个派 即最多把一整个派给某个人 问能平均分的最大面积 二 ...

  4. Pie(二分)

    http://poj.org/problem?id=3122 题意:将n个圆柱体的不同口味的pie分给m个人,要求每个人分得的pie必须体积相同,且来自于一块pie(即:只分得一种口味的pie),求最 ...

  5. Pie(二分POJ3122)

    Pie Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12985   Accepted: 4490   Special Ju ...

  6. PIE(二分) 分类: 二分查找 2015-06-07 15:46 9人阅读 评论(0) 收藏

    Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissio ...

  7. 【hoj】2651 pie 二分查找

    二分查找是一个非常主要的算法,针对的是有序的数列,通过中间值的大小来推断接下来查找的是左半段还是右半段,直到中间值的大小等于要找到的数时或者中间值满足一定的条件就返回,所以当有些问题要求在一定范围内找 ...

  8. HDU1969:Pie(二分)

    Pie Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submissio ...

  9. UVaLive 3635 Pie (二分)

    题意:有f+1个人来分n个圆形派,每个人得到的必须是一个整块,并且是面积一样,问你面积是多少. 析:二分这个面积即可,小了就多余了,多了就不够分,很简单就能判断. 代码如下: #pragma comm ...

随机推荐

  1. 机器学习:SVM(目标函数推导:Hard Margin SVM、Soft Margin SVM)

    一.Hard Margin SVM SVM 的思想,最终用数学表达出来,就是在优化一个有条件的目标函数: 此为 Hard Margin SVM,一切的前提都是样本类型线性可分: 1)思想 SVM 算法 ...

  2. Django基础(三)

    Template 不能直接将html硬编码到视图里的原因: 对页面设计进行的任何改变都必须对python 代码进行相应的修改.站点设计的修改往往比底层python 代码的修改要频繁的多,因此如果可以在 ...

  3. 山区建小学(区间DP)

    山区建小学 时间限制: 1 Sec  内存限制: 128 MB提交: 17  解决: 5[提交][状态][讨论版][命题人:quanxing] 题目描述 政府在某山区修建了一条道路,恰好穿越总共m个村 ...

  4. python 函数对象、函数嵌套、名称空间与作用域、装饰器

    一 函数对象 一 函数是第一类对象,即函数可以当作数据传递 1 可以被引用 2 可以当作参数传递 3 返回值可以是函数 3 可以当作容器类型的元素 二 利用该特性,优雅的取代多分支的if def fo ...

  5. vs中ffmpeg release版本崩溃问题(转)

    vs2010 win7 下开发视频服务器,用到ffmpeg,debug版本运行正常,切换到release时,出现"0x00905a4d 处未处理的异常: 0xC0000005: 读取位置 0 ...

  6. 向linux内核增加一个系统调用-1

    验证编辑编译内核的流程,并增加新的系统调用 注意:需要/目录至少10GB空间,/boot目录500MB空间 下载内核并解压 kernel下载 百度云搬运 密码: qc8b 进入 /usr/src目录 ...

  7. SharedPreferences 用法

    private void getUserInfoFromPref(){ /* * 保存到文件的方法 * * Constant.user = (User)Constant.readObjectFromF ...

  8. Oracle pl/sql 基础入门语法

    PL/SQL是一种块结构的语言,这意味着PL/SQL程序被划分和编写代码的逻辑块.每块由三个子部分组成:1     声明 此部分开头使用关键字DECLARE.它是一个可选的部分,并限定在该程序中使用的 ...

  9. ROS探索总结(四)——简单的机器人仿真

    前边我们已经介绍了ROS的基本情况,以及新手入门ROS的初级教程,现在就要真正的使用ROS进入机器人世界了.接下来我们涉及到的很多例程都是<ROS by Example>这本书的内容,我是 ...

  10. python http认证

    Requests 库有一个auth 模块专门用来处理HTTP 认证: import requestsfrom requests.auth import AuthBasefrom requests.au ...