POJ3122-Pie 解题心得

原题:

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块蛋糕,F+1个人,每人只能分一块(不能拿几块小的拼成大的),求每人最多拿多少体积(高相等,体积即面积)

分析:

非常水的二分题,千万要注意,输入的是朋友的数量f,分pie是分给所有人,包括自己在内共f+1人

下界low=0,即每人都分不到pie

上界high=maxsize,每人都得到整个pie,而且那个pie为所有pie中最大的          (上界就是 n个人n个pie,每个pie还等大)

对当前上下界折中为mid,计算"如果按照mid的尺寸分pie,能分给多少人"

求某个pie(尺寸为size)按照mid的尺寸,能够分给的人数,就直接size / mid,舍弃小数就可以

由于每个pie都是圆的,为了保证精度和减少运算,我的程序在计算过程中把 π 先忽略,仅仅用半径R²去计算,最后的结果再乘π

没难度的二分题,若果WA要多多留意是不是精度问题,因为算法思路是很明确的,精度才是最头疼的

代码:

#include <iostream>
#include <cstdio>
#include<cmath>
using namespace std;
double pi = acos(-1.0);             // 圆周率的表示
int T, n, f;
double b[10003];
int juge(double x)
{
    int total = 0;
    for (int i = 1; i <= n; i++)
    {
        total += int(b[i] / x);
    }
    if (total>f)
        return 1;
    else
        return 0;
}
int main()
{
    double l, r, mid, rad;
    cin >> T;
    while (T--)
    {
        cin >> n >> f;
        double sum = 0;
        for (int i = 1; i <= n; i++)
        {
            cin >> rad;
            b[i] = rad*rad*pi;
            sum += b[i];
        }
        l = 0;
        double r = sum / (f + 1);
        while (r - l>0.0001)           // 此题精度小数点后四位
        {
            mid = (l + r) / 2;
            if (juge(mid))
                l = mid;
            else
                r = mid;
        }
        printf("%.4lf\n", mid);
    }
    return 0;
}
时间: 2024-12-22 19:00:00

POJ3122-Pie 解题心得的相关文章

第四章学习小结 串的模式匹配 解题心得体会

串的模式匹配 解题心得体会 关于串,模式匹配是其一个很重要的问题.针对这个问题,书上讲了两种模式匹配的算法,即BF算法和KMP算法,下面针对这两种算法的实现谈谈我的心得. 一.BF算法的探索 [错误代码1] #include<iostream> #include<string.h> using namespace std; typedef struct{ char ch[1000002]; int length; }SString; void Index_BF(SString S,

括号配对问题——解题心得

Description You are given a string consisting of parentheses () and []. A string of this type is said to be correct: (a)if it is the empty string (b)if A and B are correct, AB is correct, (c)if A is correct, (A ) and [A ] is correct. Write a program

wechall.net/stegano 解题心得

最近迷上了 www.wechall.net 网站,里面都是些与计算机相关的题目挑战.题目又分很多类型,例如:加密与解密.隐写术.网络攻防.趣味编程.数学逻辑等.题目有的简单,有的很难,需要一些知识和技巧.与其他题目挑战的网站不同的是,在其他类似性质的网站注册的用户可以绑定到 WeChall 网站,然后 WeChall 提供排名信息,而且也分得很细,什么按总分全球排名.什么在自己国家的排名.什么解答某种语言网站题目的排名等.可以从解题的人数判断题目的难易程度,有兴趣的朋友可以去注册,解题中也能学到

POJ3122 Pie

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15473   Accepted: 5302   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 vari

Ducci Sequence 解题心得

原题贴上 A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... , an), the next n-tuple in the sequence is formed by taking the absolute differences of neighboring integers: ( a1, a2, ... , an)  (| a1 - a2|,| a2

HDU 4627 The Unsolvable Problem 解题心得

原题: Description There are many unsolvable problem in the world.It could be about one or about zero.But this time it is about bigger number. Given an integer n(2 <= n <= 10 9).We should find a pair of positive integer a, b so that a + b = n and [a, b

POJ3122 Pie(二分)

题目链接:http://poj.org/problem?id=3122 题意:一堆人分蛋糕,每人蛋糕大小一样,求最大能分多少,蛋糕必须是整块整块的,不能两块拼一起.然后注意输入F个人最后要分F+1份. 思路:很简单很水,但是精度处理很恶心,wa了很多发,直接二分蛋糕的半径就行了 AC代码: 1 #include<iostream> 2 #include<vector> 3 #include<cstdio> 4 #include<algorithm> 5 #i

USACO Greedy Gift Givers 解题心得

本题算法不难想出,但是中间还是出现了一些问题. 开始的时候是#11:Execution error,后来把普通的数组改成动态数组后问题消失. 后来又出现了Execution error: Your program had this runtime error: Illegal file open (/dev/tty). 随后google解决方案,多数都是数组开小了.遂开大数组,无效. 突然意识到很有可能是低级错误,于是检查代码. 发现写了个 for(int i = 0; i < n2 ; i++

UVa 1647 - Computer Transformation 解题心得

这个题目.... 想上题意 10935 Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the to