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. Mstar 编译器的搭建

    机顶盒: 1.解压“mipsisa32-elf-3.4.4-20101123.i386linux.tar.gz" 应用编译器 2.mips-4.3-51-mips-linux-gnu-i68 ...

  2. ucos ii 百度官方介绍

          μC/OS II(Micro-Controller Operating System Two)是一个可以基于ROM运行的.可裁剪的.抢占式.实时多任务内核,具有高度可移植性,特别适合于微处 ...

  3. linux参数之max_map_count

    “This file contains the maximum number of memory map areas a process may have. Memory map areas are ...

  4. volatile语义

    volatile在Java内存模型(JMM)中,保证共享变量对所有线程可见,但不保证原子性.volatile语义是同步,通过共享变量的方式,完成线程间的通信. 为什么需要volatile Java内存 ...

  5. 2015.5.9 C#编写DLL及C#调用C#DLL

    过程比C#调用VC++dll简单. 一.创建DLL 新建工程,类型选择类库,生成的结果就是dll 注意:在项目属性-应用程序中,注意三个地方,程序集名称和默认命名空间可以调整,但要一致,别的程序调用此 ...

  6. [Codeforces]#179 div1-----295ABCDE

    摘自我的github:https://github.com/Anoxxx The Solution Source: Codeforces Round #179 (Div. 1) VJudge链接: h ...

  7. LAMP 3.3 mysql常用操作-1

    有一个图形化管理 mysql 的工具叫做 phpmyadmin,如何在命令行下面来管理和操作 mysql. 首先进入mysql mysql -uroot -pwangshaojun 查看有那些库 &g ...

  8. linux命令-sudo普通用户拥有root权限

    普通用户权限不够 [root@wangshaojun ~]# su - dennywang[dennywang@wangshaojun ~]$ ls /root/ls: 无法打开目录/root/: 权 ...

  9. 用JS,做一个简单的计算器

    <!DOCTYPE html> <html> <head> <meta charset="utf-8">   <title&g ...

  10. eclipse中创建maven web项目

    本文主要说明将maven web项目转成eclipse支持的web项目. 创建一个maven项目设置打包类型为war则其为web项目 结构如下 将mavenweb项目转成eclipse识别的web项目 ...