b318困難

96 臺北市資訊學科能力競賽(2007)|積木的拼疊問題

求兩個實體積木可拼成的最大矩形面積。

解題思路

求兩個實體積木可拼成的最大矩形面積。

將每塊高度序列展成精確單位方格集合,產生四種旋轉加鏡射共八種姿態,每種平移到最小x與y為0並去重。

任意拼成矩形的兩塊中,至少一塊占有矩形左下角。枚舉是哪塊、其姿態、總面積的整數因數矩形寬高;占左下角者的正規化位置必在原點。

從矩形全格集合扣掉這塊,將補集正規化,若等於另一塊某種姿態即成功。此枚舉涵蓋每個可能矩形,取成功的最大總面積。

令P≤900為單塊格數、D為兩塊總面積的因數數量,最壞時間 O(N²DP log P),空間 O(NP)保存姿態;完整方格比對另有常數16。

Step-by-step animation

八種方塊姿態與矩形補集逐步追蹤

回報動畫錯誤
步驟 1 / 3

步驟 1

示例兩塊高度[4,1],每塊面積5。

總面積10
步驟
1

Reference solution

回報程式錯誤

完整程式

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Shape = vector<pair<int, int>>;
Shape norm(Shape a) {
    int x = INT_MAX, y = INT_MAX;
    for (auto p : a) {
        x = min(x, p.first);
        y = min(y, p.second);
    }
    for (auto &p : a) {
        p.first -= x;
        p.second -= y;
    }
    sort(a.begin(), a.end());
    return a;
}
int main() {
    int n;
    cin >> n;
    string line;
    getline(cin, line);
    vector<set<Shape>> all(n);
    vector<int> area(n);
    for (int i = 0; i < n; i++) {
        getline(cin, line);
        stringstream ss(line);
        int h, x = 0;
        Shape a;
        while (ss >> h) {
            for (int y = 0; y < h; y++)
                a.push_back({x, y});
            x++;
        }
        area[i] = a.size();
        for (int flip = 0; flip < 2; flip++)
            for (int rot = 0; rot < 4; rot++) {
                Shape b;
                for (auto [u, v] : a) {
                    if (flip)
                        u = -u;
                    for (int r = 0; r < rot; r++) {
                        int t = u;
                        u = -v;
                        v = t;
                    }
                    b.push_back({u, v});
                }
                all[i].insert(norm(b));
            }
    }
    int ans = 0;
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++) {
            int total = area[i] + area[j];
            if (total <= ans)
                continue;
            bool ok = false;
            for (int order = 0; order < 2 && !ok; order++) {
                int u = order ? j : i, v = order ? i : j;
                for (auto a : all[u]) {
                    if (!binary_search(a.begin(), a.end(), make_pair(0, 0)))
                        continue;
                    int w = 0, h = 0;
                    for (auto [x, y] : a) {
                        w = max(w, x + 1);
                        h = max(h, y + 1);
                    }
                    for (int W = w; W <= total; W++)
                        if (total % W == 0) {
                            int H = total / W;
                            if (H < h)
                                continue;
                            Shape complement;
                            for (int x = 0; x < W; x++)
                                for (int y = 0; y < H; y++)
                                    if (!binary_search(a.begin(), a.end(),
                                                       make_pair(x, y)))
                                        complement.push_back({x, y});
                            if (all[v].count(norm(complement))) {
                                ok = true;
                                break;
                            }
                        }
                    if (ok)
                        break;
                }
            }
            if (ok)
                ans = total;
        }
    cout << ans << '\n';
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多