P13378 [GCJ 2011 #3] Irregular Cakes 题解

· · 题解

解题思路:优雅解决闲得没事干的Mary不规则蛋糕均分问题

这道题的核心挑战在于处理不规则形状的蛋糕,通过竖直切割将其分成等面积的若干份。

题目难度差不多⭐⭐⭐,属于计算几何与二分搜索的结合题型。下面是本菜鸡的解题思路:

🔍 关键洞察

1. 面积计算:蛋糕区域可视为上下两条折线之间的区域

2. 梯形分解:将蛋糕沿x轴方向分解为若干小梯形

3. 线性插值:任意位置的y值可通过相邻点线性计算

4. 二分定位:切割点位置可通过二分搜索精确确定

🚀 解决步骤

收集关键点:

1. 合并上下边界所有x坐标

2. 去重排序得到分割点序列

边界插值:

面积累加:

每日废话:梯形面积公式:(上底+下底)×高/2

切割定位:

Code:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <set>
using namespace std;

struct P
{
    double x, y;
};

double cal(double x, double bpt[], double cum[], double lbr[], double ubr[],
    int M)
{
    if (x < 0)
        return 0;
    if (x >= bpt[M - 1])
        return cum[M - 1];
    int p = upper_bound(bpt, bpt + M, x) - bpt;
    int i = p - 1;
    if (i < 0)
        return 0;
    double a = bpt[i];
    double b = bpt[i + 1];
    double dx = x - a;
    double ha = ubr[i] - lbr[i];
    double hb = ubr[i + 1] - lbr[i + 1];
    double hx = ha + (hb - ha) * dx / (b - a);
    double seg = dx * (ha + hx) / 2.0;
    return cum[i] + seg;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    for (int t = 1; t <= T; ++t)
    {
        int W, L, U, G;
        cin >> W >> L >> U >> G;
        P low[110], upp[110];

        for (int i = 0; i < L; ++i)
            cin >> low[i].x >> low[i].y;
        for (int i = 0; i < U; ++i)
            cin >> upp[i].x >> upp[i].y;

        set<double> bst;
        for (int i = 0; i < L; i++)
            bst.insert(low[i].x);
        for (int i = 0; i < U; i++)
            bst.insert(upp[i].x);

        double bpt[210];
        int M = 0;
        for (double x : bst)
            bpt[M++] = x;
        sort(bpt, bpt + M);

        double lbr[210] = {}, ubr[210] = {};
        int il = 0;
        for (int i = 0; i < M; i++)
        {
            while (il < L - 1 && low[il + 1].x < bpt[i])
                il++;
            if (il < L - 1)
            {
                double x0 = low[il].x, y0 = low[il].y;
                double x1 = low[il + 1].x, y1 = low[il + 1].y;
                lbr[i] = (x1 == x0)
                ? y0
                : y0 + (y1 - y0) * (bpt[i] - x0) / (x1 - x0);
            }
            else
            {
                if (L < 2)
                    lbr[i] = low[0].y;
                else
                {
                    double x0 = low[L - 2].x, y0 = low[L - 2].y;
                    double x1 = low[L - 1].x, y1 = low[L - 1].y;
                    lbr[i] = (x1 == x0)
                    ? y0
                    : y0 + (y1 - y0) * (bpt[i] - x0) / (x1 - x0);
                }
            }
        }

        int iu = 0;
        for (int i = 0; i < M; i++)
        {
            while (iu < U - 1 && upp[iu + 1].x < bpt[i])
                iu++;
            if (iu < U - 1)
            {
                double x0 = upp[iu].x, y0 = upp[iu].y;
                double x1 = upp[iu + 1].x, y1 = upp[iu + 1].y;
                ubr[i] = (x1 == x0)
                ? y0
                : y0 + (y1 - y0) * (bpt[i] - x0) / (x1 - x0);
            }
            else
            {
                if (U < 2)
                    ubr[i] = upp[0].y;
                else
                {
                    double x0 = upp[U - 2].x, y0 = upp[U - 2].y;
                    double x1 = upp[U - 1].x, y1 = upp[U - 1].y;
                    ubr[i] = (x1 == x0)
                    ? y0
                    : y0 + (y1 - y0) * (bpt[i] - x0) / (x1 - x0);
                }
            }
        }

        double cum[210] = {};
        for (int i = 1; i < M; i++)
        {
            double w = bpt[i] - bpt[i - 1];
            double ha = ubr[i - 1] - lbr[i - 1];
            double hb = ubr[i] - lbr[i];
            cum[i] = cum[i - 1] + w * (ha + hb) / 2.0;
        }

        double tot = cum[M - 1];
        double per = tot / G;

        cout << "Case #" << t << ":\n";
        for (int g = 1; g < G; g++)
        {
            double tar = g * per;
            double le = 0.0, ri = W;
            for (int it = 0; it < 60; it++)
            {
                double mi = (le + ri) / 2.0;
                double f = cal(mi, bpt, cum, lbr, ubr, M);
                f < tar ? le = mi : ri = mi;
            }
            cout << fixed << setprecision(6) << (le + ri) / 2.0 << '\n';
        }
    }
    return 0;
}