[Solution] [PA 2017] 马赛克

· · 题解

\text{Task}

题目链接:[PA 2017] 马赛克

给定 n 个二维平面上的整数点 (l_i,r_i),要求以它们为正方形左下角构造边长为 l_i(1\leq l_i\leq 2\times 10^9)的正方形,要求:

\text{Solution}

首先,由于所有正方形以给定点为左下角构造,则最终形成的矩形的左下角一定在 n 个点中。找到该点,记为 p,满足:

\forall 1\leq i\leq n,\ x_i\geq x_p \vee y_i\geq y_p

如果不存在这个点,则直接输出 NIE。

钦定一个构造正方形的顺序:优先构造 x_i 较小的,如果相等,优先构造 y_i 较小的。

但只有点 p 的情况下这个构造依然是非常麻烦的,因为我们可以发现某些点上侧和右侧均不存在点或已构造的正方形对它的正方形边长造成限制,这时候就需要枚举边长。

可以考虑找到一个矩形的上边界 R,此时所有点的边长均受到 R 的限制。

接下来要做的就是快速枚举 R,设点 q 为最终矩形左上角矩阵的左下角,也即其坐标 (x_q,y_q) 满足:

\begin{cases} x_q=x_p,\\ \forall 1\leq i\leq n\vee x_i=x_p,\ y_i\leq y_q \end{cases}

显然 q 不受上边界以外任何点或正方形的限制,因此如果上边界为 R,那么 l_q=R-y_q。

此时讨论两种情况:

因此 R 只有 O(n) 种,但实际上不可能抵满,容易通过以下剪枝排除掉情况二中的极大部分 R=y_q+x_i:

填充矩阵时,当前每个点均有了边界的限制,按照前文所说的顺序依次对 i 构造一个与当前所有限制均不冲突的最大正方形。

这个贪心的正确性是显然的,若不填充为最大正方形,留下的缝隙在当前顺序下是没有点可以填充的。

\text{Code}

通过适时的剪枝可以顶着 std::vector 的巨大常数拿下最优解。实则因为没人做。

#include <bits/stdc++.h>

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0); std::cout.tie(0);

    typedef long long i64;
    constexpr int INF = 0x3f3f3f3f;

    int T; std::cin >> T;

    auto NIE = []() { return (void)printf("NIE\n"); };

    auto Solve = [&](int test_case = 0) {

        int n; std::cin >> n;
        std::vector<std::pair<std::pair<i64, i64>, int>> point(n);
        i64 MinX = INF, MinY = INF;
        for (int i = 0; i < n; i++) {
            std::cin >> point[i].first.first >> point[i].first.second;
            point[i].second = i;
            MinX = std::min(MinX, point[i].first.first);
            MinY = std::min(MinY, point[i].first.second);
        }
        for (int i = 0; i < n; i++) {
            point[i].first.first -= MinX;
            point[i].first.second -= MinY;
        }
        sort(point.begin(), point.end());

        if (point[0].first != std::make_pair(0LL, 0LL)) return NIE();
        if (n == 1) return (void)printf("TAK 1\n");


        std::vector<i64> rst(n);
        int idx = 0;
        for (int i = 0; i < n; i++)
            if (point[i].first.first == point[0].first.first) idx = i;
        i64 R = point[idx].first.second, area = 0, MaxX = 0, MaxY = 0;
        bool flg = true;

        auto solve = [&](i64 R, int f = 0) {
            area = MaxX = MaxY = 0;
            flg = true;
            for (int i = 0; i < n; i++) {
                if (f && i == idx) continue;
                i64 d = R-point[i].first.second;
                for (int j = 0; j < i; j++) {
                    if (f && j == idx) continue;
                    if (point[j].first.second <= point[i].first.second && \
                        point[j].first.first+rst[point[j].second] > point[i].first.first && \
                        point[j].first.second+rst[point[j].second] > point[i].first.second) {
                        flg = false; break;
                    }
                }
                if (!flg) break;
                for (int j = i+1; j < n; j++) {
                    if (f && j == idx) continue;
                    if (point[i].first.first != point[j].first.first) break;
                    if (point[i].first.first <= point[j].first.first && \
                        point[i].first.second <= point[j].first.second) {
                            d = std::min(d, std::max(point[j].first.first-point[i].first.first, point[j].first.second-point[i].first.second));
                    }
                }
                for (int j = 0; j < i; j++) {
                    if (f && j == idx) continue;
                    i64 tmp = point[j].first.first;
                    point[j].first.first = std::min(tmp+rst[point[j].second]-1, point[i].first.first);
                    if (point[i].first.first <= point[j].first.first && \
                        point[i].first.second <= point[j].first.second) {
                        d = std::min(d, std::max(point[j].first.first-point[i].first.first, point[j].first.second-point[i].first.second));
                    }
                    point[j].first.first = tmp;
                }
                if (d <= 0) { flg = false; break; }
                rst[point[i].second] = d;
                MaxX = std::max(MaxX, point[i].first.first+d);
                MaxY = std::max(MaxY, point[i].first.second+d);
                area += d*d;
            }
        };
        solve(R, 1);

        if (flg && R*MaxX == area) {
            printf("TAK ");
            rst[point[idx].second] = MaxX;
            for (int i = 0; i < n; i++) printf("%lld ", rst[i]);
            return (void)printf("\n");
        }
        if (idx == n-1) return NIE();

        std::vector<int> candidat;
        for (int i = idx+1; i < n; i++) {
            R = point[idx].first.second+point[i].first.first-point[idx].first.first;
            flg = true;
            if (R-point[i].first.second > 0 && R > point[idx].first.second && R > point[i].first.second) {
                for (int j = 0; j < n; j++) {
                    if (j == i || j == idx) continue;
                    if (point[j].first.second >= R) { flg = false; break; }
                    if (point[idx].first.first <= point[j].first.first && \
                        point[j].first.first < point[i].first.first && \
                        point[j].first.second >= point[idx].first.second)
                        { flg = false; break; }
                    if (point[i].first.first <= point[j].first.first && \
                        point[j].first.first < point[i].first.first+R-point[i].first.second && \
                        point[i].first.second <= point[j].first.second)
                        { flg = false; break; }
                }
                if (flg) candidat.emplace_back(R);
            }
        }

        sort(candidat.begin(), candidat.end());
        candidat.erase(unique(candidat.begin(), candidat.end()), candidat.end());

        for (i64 R : candidat) {
            solve(R, 0);
            if (flg && R*MaxX == area) {
                printf("TAK ");
                for (int i = 0; i < n; i++) printf("%lld ", rst[i]);
                return (void)printf("\n");
            }
        }

        return NIE();

    };

    for (int i = 1; i <= T; i++) Solve(i);

    return 0;
}