B - Pie (二分)
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 (二分)的更多相关文章
- HDU 1969 Pie(二分查找)
Problem Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no ...
- HDU 1969 Pie(二分,注意精度)
Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- 【POJ 3122】 Pie (二分+贪心)
id=3122">[POJ 3122] Pie 分f个派给n+1(n个朋友和自己)个人 要求每一个人分相同面积 但不能分到超过一个派 即最多把一整个派给某个人 问能平均分的最大面积 二 ...
- Pie(二分)
http://poj.org/problem?id=3122 题意:将n个圆柱体的不同口味的pie分给m个人,要求每个人分得的pie必须体积相同,且来自于一块pie(即:只分得一种口味的pie),求最 ...
- Pie(二分POJ3122)
Pie Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12985 Accepted: 4490 Special Ju ...
- 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 ...
- 【hoj】2651 pie 二分查找
二分查找是一个非常主要的算法,针对的是有序的数列,通过中间值的大小来推断接下来查找的是左半段还是右半段,直到中间值的大小等于要找到的数时或者中间值满足一定的条件就返回,所以当有些问题要求在一定范围内找 ...
- HDU1969:Pie(二分)
Pie Time Limit : 5000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submissio ...
- UVaLive 3635 Pie (二分)
题意:有f+1个人来分n个圆形派,每个人得到的必须是一个整块,并且是面积一样,问你面积是多少. 析:二分这个面积即可,小了就多余了,多了就不够分,很简单就能判断. 代码如下: #pragma comm ...
随机推荐
- PD中设置外键约束名称生成规则
选择Database—>Edit Current DBMS选择Scripts->Objects->Reference->ConstName可以发现右侧的Value为: FK_% ...
- jhipster初接触
在Windows7部署之前把几个依赖下了 jdk:1.80 Maven :3.3.9 git:2.14.1 npm:唯一要注意的就是配置一个阿里的镜像,不然慢的你崩溃 Yeoman: npm inst ...
- Eclipse与github整合完整版
最近朋友都推荐使用github管理自己的项目,而且免费用户可以有5个仓库,恰好我也想了解下git,借此机会学习一下. github官方指南使用独立第三方git工具来进行版本控制,并不借助于eclips ...
- 新增线下、APP、公众号多处入口,小程序会再火起来么?
现在,大多数互联网创业者最缺的是流量,第二缺的是钱.之前开发者们追捧小程序的重要原因就是在于认为这可能是下一个微信公众号体量的流量入口,因为大家都想从微信的8亿多用户中收获自己的一部分用户. 近期部分 ...
- C#改变LInqToSQL的引用地址,读取config的数据库字符串
C#改变LInqToSQL的引用地址,读取config的数据库字符串修改Properties 下 Settings.Settings 下 Settings.Designer.cs 下 return ( ...
- 什么是个CDN???CDN是干什么的??
1.什么是CDN??? CDN的全称是Content Delivery Network,即内容分发网络.其目的是通过在现有的Internet中增加一层新的网络架构,将网站的内容发布到最接近用户的网络& ...
- JAVA基础知识总结12(多线程)
进程:正在进行中的程序.其实进程就是一个应用程序运行时的内存分配空间. 线程:其实就是进程中一个程序执行控制单元,一条执行路径.进程负责的是应用程序的空间的标示.线程负责的是应用程序的执行顺序. 一个 ...
- 【总结整理】IFeatureBuffer
IFeatureBuffer pRowBuffer = objTabWYDCQ_Tar.CreateFeatureBuffer(); pRowBuffer.Shape = SourceRow.Shap ...
- 2018网络预选赛 青岛 H
题目链接:https://pintia.cn/problem-sets/1036903825309761536/problems/1041156323504345088 题意:小明从某一点出发,向右方 ...
- ES6中变量的解析赋值的用途
变量的解构赋值用途很多. (1)交换变量的值 let x = 1; let y = 2; [x, y] = [y, x]; 上面代码交换变量x和y的值,这样的写法不仅简洁,而且易读,语义非常清晰. ( ...