poj3122 binary search 实数区间

Pie

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14536   Accepted: 4979   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 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题目大意:国人很多事情都追求公平,分饼也是如此,现在这里有n个饼,每一个饼都可以看做一个圆柱体,高都是1,但是半径不同,每一个人都可以分到某个饼的一部分,但是只能要一部分,而不能要好几块饼,最终结果必须保证每个人分到的饼的体积(面积)相等,问你每个人能够获得的饼的最大面积是多少。思路分析:首先数据量很大,如果用暴力枚举肯定会超时,很显然我们应该采用二分逼近的方法来确定答案,但是在实际操作的时候还是出了一些问题,首先,二分精度不能够太高,1e-5就可以,精度太高会超时,其次,关于π,如果写做3.1415926会被精度卡掉,将pi定义为acos(-1.0)就可以了。代码:#include<map>#include<queue>#include<stack>#include<cmath>#include<cstdio>#include<vector>#include<string>#include<cstdlib>#include<cstring>#include<climits>#include<iostream>#include<algorithm>#include <cmath>#define LL long longusing namespace std;const int maxn=10000+100;const double pi=acos(-1.0);double a[maxn];int n,f;double s(double r){    return pi*r*r;}bool check(double x){    int  t=0;    for(int i=0;i<n;i++)    {        double p=s(a[i]);        while(p>=x)        {            p-=x;            t++;            if(t>=f+1) return true;        }    }    return false;}int main(){    int T;    scanf("%d", &T);    while(T--)    {        double sum=0.0;        scanf("%d%d",&n,&f);        for(int i=0;i<n;i++)        {            scanf("%lf",&a[i]);            sum+=s(a[i]);        }        sort(a,a+n);        double l=0.0,r=sum/(f+1)*1.0;        double ans=0;        while(l+0.000001<=r)        {            double mid=(l+r)/2;            if(check(mid)) ans=mid,l=mid;            else r=mid;        }        printf("%.4lf\n",ans);    }    return 0;}
时间: 2024-12-28 19:21:08

poj3122 binary search 实数区间的相关文章

uva 10304 - Optimal Binary Search Tree 区间dp

题目链接 给n个数, 这n个数的值是从小到大的, 给出个n个数的出现次数. 然后用他们组成一个bst.访问每一个数的代价是这个点的深度*这个点访问的次数. 问你代价最小值是多少. 区间dp的时候, 如果l >= r, 那么返回0, l == r-1, 返回两个数中小的一个. 其他情况的话枚举分界点进行状态转移. #include <bits/stdc++.h> using namespace std; #define mem1(a) memset(a, -1, sizeof(a)) co

uva 10304 Optimal Binary Search Tree (区间DP)

uva 10304 Optimal Binary Search Tree 题目大意:给出N个结点(已知每个结点的权值)来建树,建树时要满足以下规则:左子树的节点的值要全小于父节点,右子树的节点的值要全大于父节点.要求最后建出的树总权值最小.总权值=各结点乘以层数(从0层开始)之后相加的和. 解题思路:dp[i][j]代表区间第i个结点到第j个结点组成的树最小的总权值.dp[j][i]=min(dp[j][i],dp[j][k?1]+dp[k+1][i]+sum[i]?sum[j?1]?num[k

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n

关于 使用Binary Search (二分法)遇到的一些问题

READ命令使用顺序查找数据表,这会降低处理速度.取而代之,使用binary search的附加命令,可以使用二分查找算法,可以帮助加快内表查找速度. 在使用binary search之前必须首先将内表排序,否则有可能找不到记录,因为二分查找反复将查找区间对半划分,如果要查找的值小于查找区间的中间位置的数据项值,则查找区间将缩小到前半个区间,否则查找将局限于后半区间.然后.................. 今天工作中写了个REPORT,当我进行Read Table 的时候,并且对象数据存在的情

九章算法系列(#2 Binary Search)-课堂笔记

前言 先说一些题外的东西吧.受到春跃大神的影响和启发,推荐了这个算法公开课给我,晚上睡觉前点开一看发现课还有两天要开始,本着要好好系统地学习一下算法,于是就爬起来拉上两个小伙伴组团报名了.今天听了第一节课,说真的很实用,特别是对于我这种算法不扎实,并且又想找工作,提高自己的情况. 那就不多说废话了,以后每周都写个总结吧,就趁着这一个月好好把算法提高一下.具体就从:课堂笔记.leetcode和lintcode相关习题.hdu和poj相关习题三个方面来写吧.希望自己能够坚持下来,给大家分享一些好的东

Binary Search 二分查找总结

Binary Search基本的复杂度为O(logn).如果提示需要对O(n)的算法进行优化,非常可能就是二分,另外二分一般出现在排序数组或者变形后的排序数组(rotated array)当中.二分主要有两种,binary search on index(index上的二分)和binary search on result(结果上的二分).index上的二分主要有 result上的二分主要有Sqrt(x),Wood cut两种. 另外binary search的版本很多,区别在终结条件,比如是l

Leetcode 树 Unique Binary Search Trees

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Unique Binary Search Trees Total Accepted: 13478 Total Submissions: 37858 Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, there

STL之二分查找 (Binary search in STL)

STL之二分查找 (Binary search in STL) Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第45条的一个总结,阐述了各种查找算法的异同以及使用他们的时机. 首先可供查找的算法大致有count,find,binary_search,lower_bound,upper_bound,equal_range.带有判别式的如count_i

LeetCode: Validate Binary Search Tree [098]

[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with