Dispatch pie (dichotomy)

2. Problem Description

My birthday is coming up! According to custom, I need to give you some pie. I have N different tastes, different sizes of pie. F have a friend who would come to my party, everyone will get a pie ( 必须一个派的一块,不能由几个派的小块拼成; can be a whole pie).

My friends are especially stingy, if someone got bigger piece, will begin to complain. 因此所有人拿到的派是同样大小的(But need not be the same shape), although some of this faction will be wasted, but better than a good screw up the whole party. Of course, I have to give yourself one, and this one should be the same size and others.

I ask each of us to get the pie is the largest number? Each high school is a 1, a radius ranging from a cylinder.

Entry

The first line contains two positive integers N and F, 1 ≤ N, F ≤ 10 000, the number indicates the number of friends and school.

The second row contains an integer number between N 1-10000, represents the radius of each faction.

Export

The output of each person can get the maximum volume faction, 精确到小数点后三位.

输入样例

3 3
4 3 3

输出样例

25.133

---- dichotomy points pie

Ideas are as follows

Meaning of the questions: to give you a pie n, m + 1 person, the same as the height of each pie, but they have different radii,
but the m + 1 individual very picky, each sub-pie must be divided blocks can only come from n pie in which an
idea: this question is a simple dichotomy, I assume that everyone points to as sub-pie mid, and then shrinking precisely this, assuming that the value of the molecular pie by half ,,,,,,,? in this way I can get the value we want
this question ideas can also refer to a similar problem to the problem

Solution to a problem as follows

#include<iostream>
#include<cmath>
using namespace std;

const double pi = acos(-1);    //求圆周率
const int Len = 10005;
const double cha = 0.00001;		//⚠️精读尽量开大一点,防止出错
int n,m;
int ar[Len];
double ans;					  //每个人所能分的最大子pie的大小

bool judge(double mid)
{
    int m_cnt = m;            //总共需要分割的数量
    for(int i = 1; i <= n; i ++)
    {
        double V = pi * ar[i] * ar[i];
        if(V >= mid)
        {
            m_cnt -= int(V / mid);
        }
        if(m_cnt <= 0)
        {
            return true;
        }
    }
    return false;
}

void Binary_search(double l ,double r)
{
    while(r - l > cha)
    {
        double mid = (l + r) / 2;
        if(judge(mid))           //如果我这个假设的 子pie的大小mid,是可以完全由 n 张 pie 分割出来,那么我们要考虑 能不能把 假设子pie大小mid 调大一些(通过调整下限 mn = mid),这样再进行尝试
        {
            ans = mid;
            l = mid;
        }
        else           //不能够分割出来m块大小为 mid的子pie,那么我们就缩小,mid值(通过调整 上线mx = mid)
        {
            r = mid;
        }
    }
    printf("%.3lf\n",ans);
}

int main()
{
    //freopen("T.txt","r",stdin);
    scanf("%d %d", &n, &m);
    m ++;       //包括自己,所以总人数 + 1
    for(int i = 1; i <= n; i ++)
        scanf("%d",&ar[i]);
    Binary_search(0 , pi * Len * Len);

    return 0;
}
Published 73 original articles · won praise 100 · Views 2690

Guess you like

Origin blog.csdn.net/qq_34261446/article/details/103971649