高效算法——C 分饼
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 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个朋友和自己,每个朋友拿到的饼必须大小一样,而且口味只能有一种,但是是形状可以不一样,求每个朋友能拿到的最大的饼的面积,这个题目有一个高精度问题需要注意,就是pi,要将它定义成acos(-1.0),这样才不会出现误差,用二分法可以逐步实现
程序代码:
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
double pi=acos(-1.0);
double p[+];
double sum,maxn;
int i,n,f,t,k,cnt;
double l,r,m;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&f);
f++;
l=sum=;
for(i=;i<n;i++)
{
scanf("%d",&k);
p[i]=pi*k*k;
l=max(p[i],l);
sum+=p[i];
}
l=l/f;
r=sum/f;
while(l+0.00001<r)
{
m=(l+r)/;
cnt=;
for(i=;i<n;i++)
cnt+=(int)floor(p[i]/m);
if(cnt<f) r=m;
else l=m;
}
printf("%.4lf\n",l);
}
return ;
}
高效算法——C 分饼的更多相关文章
- 深入N皇后问题的两个最高效算法的详解 分类: C/C++ 2014-11-08 17:22 117人阅读 评论(0) 收藏
N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行.同一列.同一斜线上的皇后都会自动攻击). 一. 求解N皇后问题是算法中回溯法应用的一个经典案例 回溯算 ...
- JVM垃圾回收算法及分代垃圾收集器
一.垃圾收集器的分类 1.次收集器 Scavenge GC,指发生在新生代的GC,因为新生代的Java对象大多都是朝生夕死,所以Scavenge GC非常频繁,一般回收速度也比较快.当Eden空间不足 ...
- Java实现 蓝桥杯VIP 算法提高 分苹果
算法提高 分苹果 时间限制:1.0s 内存限制:256.0MB 问题描述 小朋友排成一排,老师给他们分苹果. 小朋友从左到右标号1-N.有M个老师,每次第i个老师会给第Li个到第Ri个,一共Ri-Li ...
- CVPR2020论文介绍: 3D 目标检测高效算法
CVPR2020论文介绍: 3D 目标检测高效算法 CVPR 2020: Structure Aware Single-Stage 3D Object Detection from Point Clo ...
- JVM之GC算法、垃圾收集算法——标记-清除算法、复制算法、标记-整理算法、分代收集算法
标记-清除算法 此垃圾收集算法分为“标记”和“清除”两个阶段: 首先标记出所有需要回收的对象,在标记完成后统一回收所有被标记对象,它的标记过程前面已经说过——如何判断对象是否存活/死去 死去的对象就会 ...
- 双有序队列算法——处理哈夫曼K叉树的高效算法
算法介绍: 哈夫曼树的思路及实现众所周知,大部分是用堆来维护和实现,这种思路比较清晰,在K比较小的时候处理较快(具体例子接下来再说),而且编程复杂度不是很高,利于应用.但是,其所用的数据结构是树,是在 ...
- js算法:分治法-循环赛事日程表
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- manacher算法——回文串计算的高效算法
manacher算法的由来不再赘述,自行百度QWQ... 进入正题,manacher算法是一个高效的计算回文串的算法,回文串如果不知道可以给出一个例子:" noon ",这样应该就 ...
- UVa 1210 (高效算法设计) Sum of Consecutive Prime Numbers
题意: 给出n,求把n写成若干个连续素数之和的方案数. 分析: 这道题非常类似大白书P48的例21,上面详细讲了如何从一个O(n3)的算法优化到O(n2)再到O(nlogn),最后到O(n)的神一般的 ...
随机推荐
- asp.net对word文档进行修改 对于使用word文档做模板编辑比较适用
最近做项目,需要多word文档进行编辑并导出一个新的word,在最初的word编辑中留下特定的字符串用来替换,然后在本地生成一个新的word文档,并且不修改服务器中的word文档,这样才能保证服务器中 ...
- Mongodb 启动时 lock文件访问没有权限处理
mongodb 第二次启动时候异常信息: lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance ...
- Html5 部分特性
HTML5 是 W3C 与 WHATWG 合作的结果. 编者注:W3C 指 World Wide Web Consortium,万维网联盟. 编者注:WHATWG 指 Web Hypertext Ap ...
- jquery-动态设置ul li a链接目标
概述: 先上我的布局图: ul中的每一个li里面包含着一个'a'超链接,可以猜想到,我想没点击ul->li里面每个a,链接到不同的页面,在这里,我用iframe完成. html主要代码为: &l ...
- while read line无法循环read文件
while read line 与for循环的区别 例子:要从一个ip列表中获取ip.port,然后ssh ip 到目标机器进行特定的command操作ssh -o StrictHostKeyChec ...
- redhat6.4 配置centos6 yum替换
1.卸载掉系统redhat自带的yum rpm -qa |grep yum |xargs rpm -e --nodeps 2 下载相关的centos yum插件 主要有python-inipa ...
- 06MySQL数据库入门
1.数据库的概念 数据库是保存数据的仓库,可以方便的把数据放进去,并且把数据根据各种需求取出来. 数据库管理系统(Database Management System,DBMS)是对数据库进行管理(增 ...
- 从零开始学java(猜数字游戏)
练练手不喜勿喷,看到什么学习什么第一次发博客格式就见见谅..... 2016-07-21 19:55:02 imp ...
- OpenCV2.4.9 & Visual Studio 2010 环境配置篇
1. 准备工作 1.1. 安装 Visual Studio 2010, 需要安装 VC++ 相关功能.具体可求助度娘. 1.2. 下载 OpenCV 2.4.9 For Windows:https:/ ...
- ios开发之xcode6中如何添加pch全局引用文件
xcode6中去掉了默认添加pch文件,这就需要我们自己手动添加pch文件了,添加pch文件是为了一些琐碎的头文件引用,加快编译速度! 下面就说下该如何手动添加pch文件: 1.添加一个文件,在oth ...