b003困難

107 全國資訊學科能力競賽(2018)|8. 摩天大樓

枚舉少量設施位置,讓住戶分配交由二分圖匹配。

解題思路

給定滿意度門檻T,設施i放在f時,住戶h只能住在滿足|floor−f|·a[h][i]≥T的樓層。逐個放設施並用位元集合交集更新每戶可用樓層,設施占用的樓層也從全部住戶集合移除。

每放一個設施就檢查所有住戶能否匹配到互異樓層。此時尚未放設施的樓層暫時可住,是放寬問題;若連放寬都無完整匹配,任何後續擺法都不可能,便安全剪枝。放完k個設施時匹配條件即精確充分。匹配前按候選數排序,前綴聯集不足住戶數亦可直接排除。

可行性隨T單調。任何排列中必有相鄰的設施與住戶,該戶滿意度不超過其對該設施的a,所以答案上界是全部a的最大值100。二分此小值域;利用樓層反射令第一個設施位於下半部,相同厭惡列向量的設施按位置遞增,兩種對稱都不改變可行性。

設H=n−k,最壞枚舉P(n,k)位置組合,每個分支做O(H²n)位元匹配工作,總上界O(log A·k·P(n,k)·H²n),實際由必要匹配條件剪枝;儲存O(kHn+Hn),Python輸入O(Hk)。

Step-by-step animation

門檻二分、設施分支與匹配剪枝|n=4,k=1,設施放第1樓。三戶a為10,15,20。

回報動畫錯誤
步驟 1 / 3

設施位置

n=4,k=1,設施放第1樓。三戶a為10,15,20。

設施
第2樓
第3樓
第4樓
門檻
20

Reference solution

回報程式錯誤

完整程式

#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, k;
    cin >> n >> k;
    int H = n - k;
    vector<vector<int>> a(H, vector<int>(k));
    int upper = 0;
    for (auto &r : a)
        for (int &x : r) {
            cin >> x;
            upper = max(upper, x);
        }
    vector<int> order(k);
    iota(order.begin(), order.end(), 0);
    sort(order.begin(), order.end(), [&](int x, int y) {
        int sx = 0, sy = 0;
        for (auto &r : a) {
            sx += r[x];
            sy += r[y];
        }
        return sx < sy;
    });
    vector<vector<int>> b = a;
    for (int h = 0; h < H; h++)
        for (int c = 0; c < k; c++)
            b[h][c] = a[h][order[c]];
    a = b;
    vector<int> prior(k, -1);
    for (int c = 0; c < k; c++)
        for (int j = 0; j < c; j++) {
            bool same = true;
            for (int h = 0; h < H; h++)
                if (a[h][c] != a[h][j])
                    same = false;
            if (same)
                prior[c] = j;
        }
    vector<int> floors(n);
    iota(floors.begin(), floors.end(), 0);
    stable_sort(floors.begin(), floors.end(),
                [&](int x, int y) { return min(x, n - 1 - x) < min(y, n - 1 - y); });
    using U = unsigned;
    U all = (1U << n) - 1;
    auto feasible = [&](int T) {
        vector<vector<vector<U>>> good(k, vector<vector<U>>(H, vector<U>(n)));
        for (int c = 0; c < k; c++)
            for (int h = 0; h < H; h++)
                for (int f = 0; f < n; f++)
                    for (int x = 0; x < n; x++)
                        if (abs(x - f) * a[h][c] >= T)
                            good[c][h][f] |= 1U << x;
        auto matching = [&](vector<U> mask) {
            sort(mask.begin(), mask.end(), [](U x, U y) {
                return __builtin_popcount(x) < __builtin_popcount(y);
            });
            U total = 0;
            for (int i = 0; i < H; i++) {
                total |= mask[i];
                if (__builtin_popcount(total) < i + 1)
                    return false;
            }
            vector<int> match(n, -1);
            U seen;
            function<bool(int)> dfs = [&](int u) {
                U bits = mask[u] & ~seen;
                while (bits) {
                    U bit = bits & -bits;
                    bits -= bit;
                    seen |= bit;
                    int v = __builtin_ctz(bit);
                    if (match[v] < 0 || dfs(match[v])) {
                        match[v] = u;
                        return true;
                    }
                }
                return false;
            };
            for (int h = 0; h < H; h++) {
                seen = 0;
                if (!dfs(h))
                    return false;
            }
            return true;
        };
        vector<int> placed;
        function<bool(int, const vector<U> &, U)> search =
            [&](int c, const vector<U> &mask, U used) {
                if (c == k)
                    return true;
                for (int f : floors) {
                    if ((used >> f & 1) || (c == 0 && f > (n - 1) / 2) ||
                        (prior[c] >= 0 && f <= placed[prior[c]]))
                        continue;
                    vector<U> next(H);
                    bool ok = true;
                    for (int h = 0; h < H; h++) {
                        next[h] = mask[h] & good[c][h][f] & ~(1U << f);
                        if (!next[h])
                            ok = false;
                    }
                    if (!ok || !matching(next))
                        continue;
                    placed.push_back(f);
                    if (search(c + 1, next, used | 1U << f))
                        return true;
                    placed.pop_back();
                }
                return false;
            };
        return search(0, vector<U>(H, all), 0);
    };
    int lo = 0, hi = upper + 1;
    while (lo + 1 < hi) {
        int mid = (lo + hi) / 2;
        if (feasible(mid))
            lo = mid;
        else
            hi = mid;
    }
    cout << lo << "\n";
}

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

更多