b157困難

107 臺中區資訊學科能力競賽(2018)|動動腦

回答min[L,R]=a要求整段每格≥a,且至少一格恰為a。因此此回答矛盾,恰在它的區間被較大回答值的區間聯集完全覆蓋時。

解題思路

回答min[L,R]=a要求整段每格≥a,且至少一格恰為a。因此此回答矛盾,恰在它的區間被較大回答值的區間聯集完全覆蓋時。

極小矛盾集合由一個低值區間及一個不可再刪的高值覆蓋組成。高值區間j參與某個極小覆蓋,等價於它能保留一個私人位置x,而其他高區間不含x且能覆蓋j外的左右兩側。

對低區間壓縮端點並掃描,E(t)為覆蓋位置t的最小右端點,S(t)為最大左端點。設左側E最大值為A、右側S最小值為B,j可參與恰在j內存在整數A<x<B。前綴最大與後綴最小可逐j常數查詢,所有參與者取最小原編號。超出值域的回答本身就是單元素矛盾。

Q個回答,時間 O(Q² log Q),每個低區間重建端點、兩個heap與前後綴,額外空間 O(Q);輸入 O(Q),不建立長度N數列。

Step-by-step animation

教學小例:矛盾的區間覆蓋見證與私人位置

回報動畫錯誤
步驟 1 / 3

N=4,回答1為[4,4]最小5

它與其他區間不相交

它與其他區間不相交
步驟
1

Reference solution

回報程式錯誤

完整程式

#include <bits/stdc++.h>
using namespace std;
struct Query {
    int l, r, a, id;
};

int main() {
    int n, q;
    while (cin >> n >> q && n) {
        vector<Query> v(q);
        int ans = q + 1;
        for (int i = 0; i < q; i++) {
            cin >> v[i].l >> v[i].r >> v[i].a;
            v[i].id = i + 1;
            if (v[i].a < 1 || v[i].a > 1000000)
                ans = min(ans, i + 1);
        }
        for (auto low : v) {
            if (low.a < 1 || low.a > 1000000)
                continue;
            vector<Query> hi;
            vector<int> bounds = {low.l, low.r + 1};
            for (auto z : v)
                if (z.a > low.a && z.a <= 1000000) {
                    int l = max(low.l, z.l), r = min(low.r, z.r);
                    if (l <= r) {
                        hi.push_back({l, r, z.a, z.id});
                        bounds.push_back(l);
                        bounds.push_back(r + 1);
                    }
                }
            sort(bounds.begin(), bounds.end());
            bounds.erase(unique(bounds.begin(), bounds.end()), bounds.end());
            sort(hi.begin(), hi.end(), [](auto a, auto b) { return a.l < b.l; });
            int k = bounds.size() - 1, j = 0;
            vector<int> E(k), S(k);
            priority_queue<int, vector<int>, greater<int>> ends;
            priority_queue<pair<int, int>> starts;
            bool cover = true;
            for (int t = 0; t < k; t++) {
                int x = bounds[t];
                while (j < (int)hi.size() && hi[j].l <= x) {
                    ends.push(hi[j].r);
                    starts.push({hi[j].l, hi[j].r});
                    j++;
                }
                while (!ends.empty() && ends.top() < x)
                    ends.pop();
                while (!starts.empty() && starts.top().second < x)
                    starts.pop();
                if (ends.empty()) {
                    cover = false;
                    break;
                }
                E[t] = ends.top();
                S[t] = starts.top().first;
            }
            if (!cover)
                continue;
            ans = min(ans, low.id);
            vector<int> pref(k + 1, low.l - 1), suf(k + 1, low.r + 1);
            for (int t = 0; t < k; t++)
                pref[t + 1] = max(pref[t], E[t]);
            for (int t = k - 1; t >= 0; t--)
                suf[t] = min(suf[t + 1], S[t]);
            for (auto z : hi) {
                int l = lower_bound(bounds.begin(), bounds.end(), z.l) - bounds.begin(),
                    r = lower_bound(bounds.begin(), bounds.end(), z.r + 1) -
                        bounds.begin();
                if (max(z.l, pref[l] + 1) <= min(z.r, suf[r] - 1))
                    ans = min(ans, z.id);
            }
        }
        cout << (ans == q + 1 ? 0 : ans) << '\n';
    }
}

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

更多