**【POJ - 3122】 Pie(二分寻值)

题干:

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),还能寻值

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/439313.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【转】IsCallBack属性和IsPostBack属性有什么区别?

if (Page.IsCallback) return; 此句话在page的构造函数中使用&#xff0c;不让page反复生成。比如一个TEXTbox如果不组织页面刷新&#xff0c;其数据会丢失。 以postback方式进行客户端和服务器端的交互的&#xff0c; IsPostBack就是true。 以callback方式进行客户端和服务器…

【转】使用Feature导入WebPart

原文链接&#xff1a;http://www.cnblogs.com/glife/archive/2009/10/27/1590488.html 前些天在刚刚接触WebPart的时候&#xff0c;搜到了一篇《使用Feature导入WebPart》的文章&#xff0c;那个时候对Feature的了解还为零&#xff0c;所以看了是一知半解&#xff0c;等到今天…

【HDU - 5017】Ellipsoid(爬山算法,模拟退火,三分)

题干&#xff1a; Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distance between the original point (0,0,0) and points on the ellipsoid. The distance between two points (x 1,y 1,z 1) and (x 2,y 2,z 2) is defined as Input There a…

【转】[SharePoint 开发详解] 一个Feature中使用SPGridView的几个Tips

根据上面一篇随笔所介绍的PC购买流程的项目&#xff0c;在项目中&#xff0c;需要有一个生成订单的功能&#xff0c;能够使得Admin很方便的在获得批准的申请中选取一些来生成订单&#xff0c;要求界面操作简单明了&#xff0c;大概的效果图如下&#xff1a; 点击checkbox&#…

【LeetCode - 131】分割回文串(dp,dfs)

题目链接&#xff1a;https://leetcode-cn.com/problems/palindrome-partitioning/ 题目&#xff1a; 给定一个字符串 s&#xff0c;将 s 分割成一些子串&#xff0c;使每个子串都是回文串。 返回 s 所有可能的分割方案。 示例: 输入: "aab" 输出: [ ["a…

【转】VSTS中版本控制系统Git与TFVC的区别

VSTS&#xff08;Visual Studio Team Services&#xff09; VSTS简单说就是微软TFS(Team Foundation Services)的升级云版&#xff0c;不用像TFS需要在企业内部服务器上部署&#xff0c;并且是免费提供给用户使用的。 每个有微软账号&#xff08;也是免费注册的&#xff09;的…

【LeetCode - 1254】统计封闭岛屿的数目(dfs,连通块)

题目链接&#xff1a;https://leetcode-cn.com/problems/number-of-closed-islands/ 有一个二维矩阵 grid &#xff0c;每个位置要么是陆地&#xff08;记号为 0 &#xff09;要么是水域&#xff08;记号为 1 &#xff09;。 我们从一块陆地出发&#xff0c;每次可以往上下左…

【转】0.SharePoint服务器端对象模型 之 序言

对于刚刚开始接触SharePoint的开发人员&#xff0c;即使之前有较为丰富的ASP.NET开发经验&#xff0c;在面对SharePoint时候可能也很难找到入手的方向。对于任何一种开发平台而言&#xff0c;学习开发的过程大致会包括&#xff1a;开发工具的使用、开发手段的选择和开发语言的编…

【LeetCode - 122】买卖股票的最佳时机 II(贪心 或 dp)

题目链接&#xff1a;https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/ 给定一个数组&#xff0c;它的第 i 个元素是一支给定股票第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易&#xff08;多次买卖一支股票…

【转】1.SharePoint服务器端对象模型 之 对象模型概述(Part 1)

在一个传统的ASP.NET开发过程中&#xff0c;我们往往会把开发分为界面展现层、逻辑业务层和数据访问层这三个层面。作为一个应用开发平台&#xff0c;SharePoint是微软在直观的开发能力和自由的扩展能力之间&#xff0c;取到的一个平衡点&#xff0c;其对象模型的设计理念也反映…

【LeetCode - 123】买卖股票的最佳时机 III

题目链接&#xff1a; 给定一个数组&#xff0c;它的第 i 个元素是一支给定的股票在第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。 注意&#xff1a;你不能同时参与多笔交易&#xff08;你必须在再次购买前出售掉之前的股票&#xf…

【转】1.2SharePoint服务器端对象模型 之 对象模型概述(Part 2)

&#xff08;三&#xff09;Url 作为一个B/S体系&#xff0c;在SharePoint的属性、方法参数和返回值中&#xff0c;大量的涉及到了Url&#xff0c;总的来说&#xff0c;涉及到的Url可以分为如下四类&#xff1a; 绝对路径&#xff1a;完整的Url&#xff0c;包含了协议头&…

【LeetCode - 224】基本计算器(栈)

实现一个基本的计算器来计算一个简单的字符串表达式 s 的值。 题目链接&#xff1a;https://leetcode-cn.com/problems/basic-calculator/ 示例 1&#xff1a; 输入&#xff1a;s "1 1" 输出&#xff1a;2 示例 2&#xff1a; 输入&#xff1a;s " 2-1 …

【转】2.1 SharePoint服务器端对象模型 之 访问网站和列表数据(Part 1)

本节将会介绍SharePoint中最为常用的一些对象模型&#xff0c;以及如何使用这些对象模型来访问和操作网站中的数据。几乎所有的SharePoint服务器端开发都会涉及到这些内容&#xff0c;因此应着重掌握本节中所介绍的基本对象模型的使用方法。由于篇幅所限&#xff0c;在介绍每种…

【LeetCode - 1047】删除字符串中的所有相邻重复项(栈)

https://leetcode-cn.com/problems/remove-all-adjacent-duplicates-in-string/ 给出由小写字母组成的字符串 S&#xff0c;重复项删除操作会选择两个相邻且相同的字母&#xff0c;并删除它们。 在 S 上反复执行重复项删除操作&#xff0c;直到无法继续删除。 在完成所有重复…

【转】2.2 SharePoint服务器端对象模型 之 访问网站和列表数据(Part 2)

&#xff08;二&#xff09;列表&#xff08;SPList&#xff09; 列表是SharePoint中最为重要的数据容器&#xff0c;我们一般保存在SharePoint中的所有数据&#xff0c;都是保存在列表中&#xff08;文档库也是一种列表&#xff09;&#xff0c;因此列表对象在SharePoint的开…

【LeetCode - 227】基本计算器 II(栈)

https://leetcode-cn.com/problems/basic-calculator-ii/ 给你一个字符串表达式 s &#xff0c;请你实现一个基本计算器来计算并返回它的值。 整数除法仅保留整数部分。 示例 1&#xff1a; 输入&#xff1a;s "32*2" 输出&#xff1a;7 示例 2&#xff1a; 输入…

Team Foundation Server的回滚操作

VSTF Rollback 操作 最近遇到要把有些项目需要做回滚操作&#xff0c;发现TFS的UI上没有回滚的操作。 经过百度&#xff0c;查到一个CSDN上的博主发了一种方法&#xff0c;经过验证&#xff0c;那种方法是错误的&#xff1a; 他通过先获取指定变更集-》签出-》签回去&#xff…

【LeetCode - 141142】环形链表(i和ii)(快慢指针,链表)

https://leetcode-cn.com/problems/linked-list-cycle/ 给定一个链表&#xff0c;判断链表中是否有环。 如果链表中有某个节点&#xff0c;可以通过连续跟踪 next 指针再次到达&#xff0c;则链表中存在环。 为了表示给定链表中的环&#xff0c;我们使用整数 pos 来表示链表尾…

sharepoint的文件是怎样存放的及存放的表是哪个

主要有两个表&#xff0c;一个是AllDocs&#xff0c;另一个是AllDocStreams&#xff0c;在AllDocstreams有一个content字段&#xff0c;这个字段是用来保存文件内容的&#xff0c;这个字段最大只能放2G&#xff0c;这也就是SharePoint上传文件最大不能超过2G的原因。 表 AllDoc…