题干:

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 ≤ 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

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 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

解题报告:

馅饼的面积排序然后从0到最大的面积二分寻找满足的值

ac代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-6;
double a[10000 + 5];
int n,f,t;
bool cmp(const double & a,const double & b) {return a>b;
}
double mid;
bool ok() {int sum=0;for(int i = 0; i<n; i++) {sum+=(int)(a[i] / mid);}return sum >= f ;
}int main()
{
//  freopen("in.txt","r",stdin);scanf("%d",&t);while(t--) {scanf("%d %d",&n,&f);f++;for(int i = 0; i<n; i++) {scanf("%lf",&a[i]);a[i]=a[i]*a[i]*PI;}sort(a,a+n,cmp);if(n>f) n=f;//如果人数没有蛋糕数多,那就取最大的那几个蛋糕。
//      sort(a,a+n);
//      double l=a[0];
//      double r=a[n-1];double l = 0;double r = a[0];while(r-l>=eps) {mid = (l+r )/2;if(ok() ) l=mid;else r=mid;}printf("%.4f\n",l);}return 0 ;
}

总结:

1. ok函数中for循环内的那一层判断,完全可以写成(吗?)(好像不太行 但是为什么?)

    bool ok() {int sum=0;for(int i = 0; i<n; i++) {int tmp = a[i];while(tmp>=mid) {sum++;tmp-=mid;} }return sum >= f ;}

即:依次递减总是可以优化成除法!因为是线性结构,所以可以跳跃

2.还是ok函数中,加括号!! 不然出来的全是整数! 优先级问题!

3.最后的输出  mid 也能ac,但是不太符合原题的意思,所以最好还是输出l。

4.这件事情告诉我们 二分不仅能查找量(可用stl),还能寻值

**【POJ - 3122】 Pie(二分寻值)相关推荐

  1. POJ 3122 Pie 二分枚举

    题目:http://poj.org/problem?id=3122 这个题就好多了,没有恶心的精度问题,所以1A了.. 1 #include <stdio.h> 2 #include &l ...

  2. POJ 3122 Pie【二分答案】

    题意:求每个人能分到的所有蛋糕中最大的面积,像是第1个蛋糕分到2,第2个分到4,那结果是4 关于精度问题 我无力吐槽 本来是二分半径的,结果还是转分面积了 #include <iostream& ...

  3. poj 3122 Pie

    其实真的没读懂这题是什么意思,读了好几遍题怎么都弄不懂它到底想让求什么的,最后还是查了解题报告,但是他们讲的题意仍是不明白,直到看了代码才似乎明白点他的意思,唉,这个英语啊! 我理解的题意:将每一个P ...

  4. A - Expanding Rods POJ - 1905(二分)

    A - Expanding Rods POJ - 1905(二分) 题目 Problem Description When a thin rod of length L is heated n deg ...

  5. 二分平均值聚类 java_二分K-均值聚类算法

    #K-means聚类 from numpy import * importmatplotlib.pyplot as plt plt.ion()#开启交互模式,实时绘制 plt.subplots() p ...

  6. poj 2049(二分+spfa判负环)

    poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...

  7. F. Gourmet and Banquet(贪心加二分求值)

    题目链接:http://codeforces.com/problemset/problem/589/F A gourmet came into the banquet hall, where the ...

  8. POJ NOI0105-36 计算多项式的值

    问题链接:POJ NOI0105-36 计算多项式的值. 总时间限制: 1000ms 内存限制: 65536kB 描述 假定多项式的形式为xn+xn-1+-+x2+x+1,请计算给定单精度浮点数x和正 ...

  9. 为什么二分K-均值比K-均值的聚类效果更好?

    上一篇文章<K-均值算法的原理与实战>,讲到K均值算法只能收敛到局部最小值的问题,本文给大家带来了克服这个问题的二分K均值算法.   K-均值算法因为受到初始簇质心的影响,可能只收敛到局部 ...

最新文章

  1. 【文末送5本书】与「韦神」齐名,35岁刁晗生任教清华!18岁一战成名,数学界颜值巅峰...
  2. 《Swift开发实战》——第2章,第2.4节函数和闭包
  3. VUE.js项目中控制台报错: Uncaught (in promise) NavigationDuplicated解决方法
  4. jQuery核心函数的使用总结
  5. 客户端序列码生成_[基础]-序列化
  6. TypeScript输入参数的默认值一例,以及对应生成的JavaScript代码分析
  7. 解决myeclipse报错500,MySQLyog报错误码问题
  8. Docker安装OpenResty教程
  9. 部分代码_C primer plus 第三章 (代码部分)
  10. java数字的实验怎么做_java实验题目
  11. SQL语句 常用语句
  12. pythonarray去除inf_Python中的inf与nan
  13. uvalive4987
  14. ENFP型人格的特征分析(mbti性格测试)
  15. 5分钟教你掌握异常检测方法
  16. 好看的桑基图是如何炼成的!
  17. Qt成功运行Zynq UltraScale+MPSoC平台上
  18. long自动转为float类型
  19. 20200929 008.海龟绘图_画出奥运五环图
  20. linux c查看子进程,Linux 如何查看进程和控制进程

热门文章

  1. 关于浏览器模式和文本模式的困惑
  2. js+ asp.Net ajax开发163邮箱效果(列表底色、多选拖动等)--checkBox多选
  3. 310. Minimum Height Trees
  4. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol52]47.什么是Fiat-Shamir变换?
  5. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第25篇]使用特殊的素数定义GF(p)和GF(2^n)的方法
  6. [Leetcode][第117题][JAVA][填充每个节点的下一个右侧节点指针][BFS]
  7. POJ-2773 欧几里得 + 二分 + 容斥
  8. 同名字的数值求和插入行_EXCEL条件求和的6种技术,你会的超过3种吗?
  9. 仿苹果涂鸦软件_这些iPhone自带软件,学会一个少装十几个APP,16G手机也够用
  10. python django部署docker_centos利用docker部署django项目