其他分享
首页 > 其他分享> > POJ-3122 Pie

POJ-3122 Pie

作者:互联网

Pie

有f+1个人分n个蛋糕,并且要求每个人分到的蛋糕得是从同一块上切下来的,并且要求每个人分到的蛋糕大小是一样的,问最多能切多大的蛋糕

二分

直接枚举答案,复杂度\(O(nlogn)\)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <functional>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
const ll maxn = 2e5 + 10;
const ll inf = 1e17 + 10;
const double eps = 1e-8;
const double pi = acos(-1.0);
double num[maxn];
int n, m;

int dcmp(double x)
{
    return (x > eps) - (x < -eps);
}

bool query(double now)
{
    int ans = m;
    for(int i=0; i<n && ans>0; i++)
    {
        double x = num[i];
        while(ans > 0 && dcmp(x - now) >= 0)
        {
            x -= now;
            ans--;
        }
    }
    return ans == 0;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        double dif = 1e-5;
        scanf("%d%d", &n, &m);
        m++;
        double l = 0, r = 0;
        for(int i=0; i<n; i++)
        {
            double x;
            scanf("%lf", &x);
            num[i] = x * x * pi;
            r = num[i] > r ? num[i] : r;
        }
        while(r - l > dif)
        {
            double mid = (l + r) / 2;
            if(query(mid))
                l = mid;
            else
                r = mid;
        }
        printf("%.4f\n", l);
    }

    return 0;
}

标签:const,3122,double,Pie,mid,int,POJ,ans,include
来源: https://www.cnblogs.com/dgsvygd/p/16191517.html