#2020.01.14训练题解#二分入门(C题)

题源POJ-3122

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

题意

  • 输入T,代表有T组测试数据
  • 对于每组测试数据,输入N和F,代表有N块派和F个朋友
  • 给出的每块派都是圆柱体,高度为1
  • 要求把每块派都分成某体积,满足所有朋友和自己一人一块,每块体积相同
  • 对于每组测试数据,输出大家拿到手的派,体积最大为多少,保留四位小数

题解

  • 本题需要用到数学中的π值,由于手动简化到3或3.14都有可能造成精确度问题
  • 所以在全局变量处可定义常量→ const double PI=acos(-1.0); 这里的PI就是 标准的π
  • 注意二分的对象不是半径因为切割派没要求形状,所以只要对面积进行二分即可
  • 由于高度统一是1,所以体积问题可当做面积问题来看
  • 为简化计算,不用每次数据都乘以PI,二分的对象输入数据(半径)的平方 即可
  • 对于每次二分结果,遍历所有的派,进行整除切割,统计一共得到的sum判断是否满足题意数量
  • 注意每组测试数据中,都要对sum进行初始化0
  • 注意题意要求的总数量是输入的朋友数+1,因为还要包括除朋友以外的自己
  • 输出时,勿忘结果保留四位小数

涉及知识点

  • 二分 算法(此题属于实数二分)
  • 对于二分的算法-详见链接博客介绍二分

AC代码

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const double PI=acos(-1.0);
double a[10010];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n,f,num;
        double max=0.0;
        cin>>n>>f;
        num=f+1;
        for(int i=0;i<n;i++)
        {
            cin>>a[i];
            a[i]=a[i]*a[i];
            if(a[i]>max) max=a[i];
        }
        double left=0.0;
        double right=max;
        double mid;
        while(right-left>1e-6)
        {
            mid=(left+right)/2;
            int sum=0;
            for(int i=0;i<n;i++)
            {
            	sum=sum+a[i]/mid;
			}
            if(sum<num) right=mid;
            else left=mid;
        }
        printf("%.4f\n",mid*PI);
    }
    return 0;
}
发布了22 篇原创文章 · 获赞 0 · 访问量 404

猜你喜欢

转载自blog.csdn.net/qq_46184427/article/details/104073751