POJ 3122 Pie(二分+贪心)
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 22684 | Accepted: 7121 | Special Judge | ||
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 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
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
Source
就是公平地分披萨pie
我生日,买了n个pie,找来f个朋友,那么总人数共f+1人
每个pie都是高为1的圆柱体,输入这n个pie的每一个尺寸(半径),如果要公平地把pie分给每一个人(就是所有人得到的pie尺寸一致,但是形状可以不同),而且每个人得到的那份pie必须是从同一个pie上得到的
做法:
输入的是朋友的数量f,分pie是分给所有人,包括自己在内共f+1人
下界low=0,即每人都分不到pie
上界high=maxsize,每人都得到整个pie,而且那个pie为所有pie中最大的
(上界就是 n个人n个pie,每个pie还等大)
对当前上下界折中为mid,计算"如果按照mid的尺寸分pie,能分给多少人"
求某个pie(尺寸为size)按照mid的尺寸,能够分给的人数,就直接size / mid,舍弃小数就可以
code:
#include<stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <queue>
#include<string.h>
using namespace std;
#define max_v 10005
double PI=acos(-1.0);
double v[max_v];
int n,f;
bool test(double x)
{
int num=;
for(int i=;i<n;i++)
{
num+=int(v[i]/x);////每一个蛋糕的体积除以x 必须是完整蛋糕所以int();
}
if(num>=f+) //当人数大于等于f+1时说明蛋糕可以更大
return true;
else
return false;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int r;
cin>>n>>f;
double left=,right=,mid=;
for(int i=;i<n;i++)
{
cin>>r;
v[i]=PI*r*r*1.0;//体积都为1
right=max(right,v[i]);//最大蛋糕就是右边界
}
while(fabs(right-left)>1e-)
{
mid=(right+left)*0.5;
if(test(mid))
{
left=mid;//否则mid偏小,下界优化
}else
{
right=mid;//说明mid偏大,上界优化
}
}
printf("%.4f\n",mid);
}
return ;
}
POJ 3122 Pie(二分+贪心)的更多相关文章
- POJ 3122 Pie (贪心+二分)
My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N ...
- POJ 3122 Pie 二分枚举
题目:http://poj.org/problem?id=3122 这个题就好多了,没有恶心的精度问题,所以1A了.. #include <stdio.h> #include <ma ...
- POJ 3122 Pie 二分答案
题意:给你n个派,每个派都是高为一的圆柱体,把它等分成f份,每份的最大体积是多少. 思路: 明显的二分答案题-- 注意π的取值- 3.14159265359 这样才能AC,,, //By Sirius ...
- 【POJ 3122】 Pie (二分+贪心)
id=3122">[POJ 3122] Pie 分f个派给n+1(n个朋友和自己)个人 要求每一个人分相同面积 但不能分到超过一个派 即最多把一整个派给某个人 问能平均分的最大面积 二 ...
- poj 3122 (二分查找)
链接:http://poj.org/problem?id=3122 Pie Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1 ...
- POJ - 3122 Pie(二分)
http://poj.org/problem?id=3122 题意 主人过生日,m个人来庆生,有n块派,m+1个人(还有主人自己)分,问每个人分到的最大体积的派是多大,PS每 个人所分的派必须是在同一 ...
- 【POJ】3122 Pie [二分查找]
题目地址:http://poj.org/problem?id=3122 二分每块饼的体积.为了保证精度,可以先二分半径的平方r*r,最后再乘以PI.要注意一点,要分的人数要包括自己,及f+1. #in ...
- POJ 3122 Pie【二分答案】
<题目链接> 题目大意: 将n个半径不一但是高度为1的蛋糕分给 F+1个人,每个人分得蛋糕的体积应当相同,并且需要注意的是,每个人分得的整块蛋糕都只能从一个蛋糕上切下来,而不是从几个蛋糕上 ...
- POJ 3122 Pie
题目大意: 给出n个pie的直径,有f+1个人,如果给每人分的大小相同(形状可以不同),每个人可以分多少.要求是分出来的每一份必须出自同一个pie,也就是说当pie大小为3,2,1,只能分出两个大小为 ...
随机推荐
- Java Jsp使用
1.Jsp基础 1)Jsp的执行过程 tomcat服务器完成:jsp文件->翻译成java文件->编译成class字节码文件-> 构造类对象-> 调用方法 tomcat的wor ...
- 查看postgre都有哪些语句占用CPU
查看占用CPU最多的几个postgresql ps aux | grep postgres | sort -n -r -k 3 | head -10 | awk '{print $2, $3}' 查看 ...
- .NET开源工作流RoadFlow-流程运行-管理员干预
在流程运行过程中管理员可以干预流程实例的走向,如管理加强制退回,指派和删除流程实例操作.在 流程管理-->实例管理 中查找到相应的流程实例,点击管理按钮即可管理该流程实例: 点击指派按钮,选择要 ...
- IIS6.0配置正常,但是显示“网页无法访问”,Httperr.log中显示全是“Connections_refused”,问题总结
转自:http://blog.csdn.net/foxeatapple/article/details/21983869 最近部门的Web服务器突然无法访问! 加班解决! 问题症状: 1.“Inter ...
- Python文件夹与文件的操作 ZZ
最近在写的程序频繁地与文件操作打交道,这块比较弱,还好在百度上找到一篇不错的文章,这是原文传送门,我对原文稍做了些改动. 有关文件夹与文件的查找,删除等功能 在 os 模块中实现.使用时需先导入这个模 ...
- 【NLP_Stanford课堂】拼写校正
在多种应用比如word中都有拼写检查和校正功能,具体步骤分为: 拼写错误检测 拼写错误校正: 自动校正:hte -> the 建议一个校正 建议多个校正 拼写错误类型: Non-word Err ...
- svn merge error must be ancestrally related to,trunk merge branch报错
trunk merge branch的时候报错 xxx must be ancestrally related to xxx,这个报错的意思是两者不关联,所以需要去建立关联. [回顾背景] ...
- Eclipse编码格式
来源:http://e-ant.javaeye.com/blog/177579 如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Java文件使用UTF-8编码.然而,E ...
- 安装nginx及依赖包
#!/bin/bash #auto zhangjia #date 20171007#安装SSL##################################################### ...
- js中构造函数和普通函数的区别
this简介: this永远指向当前正在被执行的函数或方法的owner.例如: 1 2 3 4 5 function test(){ console.log(this); } test(); // ...