QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#179706#5665. AA Country and King Dreamoonarseny_yWA 35ms23212kbC++232.8kb2023-09-15 02:06:432023-09-15 02:06:43

Judging History

你现在查看的是最新测评结果

  • [2023-09-15 02:06:43]
  • 评测
  • 测评结果:WA
  • 用时:35ms
  • 内存:23212kb
  • [2023-09-15 02:06:43]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define int long long

set<int> g[300005];
vector<int> a;
int l = -1, r = -1, cur = 0;

int tin[300005], tout[300005], x = 0;

bool check(int v, int u) { return tin[v] <= tin[u] && tout[u] <= tout[v]; }

bool good[300005];
int cnt[300005];
int d[300005];
int p[300005];

void cfs(int v, int p = -1) {
    ::p[v] = p;
    tin[v] = x++;
    for (auto u: g[v]) if (u != p) d[u] = d[v] + 1, cfs(u, v);
    tout[v] = x++;
}

void fix() { while (cur < a.size() && !a[cur]) ++cur; }

deque<int> add;

deque<int> get_path(int v, int u) {
    deque<int> ans;
    while (v != u) {
        if (check(v, u)) {
            for (auto k : g[v]) {
                if (check(v, k) && check(k, u)) {
                    ans.push_back(v = k);
                    break;
                }
            }
        } else ans.push_back(v = p[v]);
    }
    return ans;
}

int32_t main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        for (int i = 0; i <= n; ++i) cnt[i] = 0;
        a.resize(2 * n - 1);
        add.clear();
        for (auto &i: a) cin >> i;
        a.front() = 1, a.back() = 1;
        l = -1, r = -1;
        cur = 0;
        set<int> wh;
        for (int i = 2; i <= n; ++i) wh.insert(i);
        for (int i = 0; i + 1 < a.size(); ++i)
            if (a[i] && a[i + 1])
                ++cnt[a[i]], ++cnt[a[i + 1]], g[min(a[i], a[i +
                                                            1])].insert(
                 max(a[i], a[i + 1]));
        for (int i = 1; i < a.size(); ++i) {
            if (!a[i]) {
                if (l == -1) l = i;
                r = i;
            } else wh.erase(a[i]);
        }
        if (l == -1) {
            for (auto &i: a) cout << i << ' ';
            cout << '\n';
            continue;
        }
        cfs(1);
        int nw = a[l - 1];
        auto s = get_path(nw, a[r + 1]);
        int cur = l;
        while (cur <= r || !wh.empty()) {
            int next = -1;
            if (!s.empty()) next = s.front(), s.pop_front();
            if (!wh.empty() && (next == -1 || *wh.begin() < next)) {
                int h = *wh.begin();
                wh.erase(wh.begin());
                a[cur] = h;
                ++cur;
                while (!wh.empty() && *wh.begin() < nw) {
                    int h1 = *wh.begin();
                    wh.erase(wh.begin());
                    a[cur] = h1;
                    a[cur + 1] = h;
                    cur += 2;
                }
                a[cur] = nw;
                ++cur;
            } else {
                a[cur] = (nw = next);
                ++cur;
            }
        }
        for (auto &i : a) cout << i << ' ';
        cout << '\n';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 23212kb

input:

9
5
1 2 3 2 0 2 1 5 1
5
1 2 3 0 0 2 1 5 1
5
1 2 0 0 0 2 1 5 1
5
1 2 0 0 0 0 1 5 1
5
1 0 0 0 0 0 1 5 1
5
1 0 0 0 0 0 0 5 1
5
1 0 0 0 0 0 0 0 1
5
1 0 0 0 0 0 0 0 0
5
0 0 0 0 0 0 0 0 0

output:

1 2 3 2 4 2 1 5 1 
1 2 3 2 4 2 1 5 1 
1 2 3 2 4 2 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 
1 2 1 3 1 4 1 5 1 

result:

ok 9 lines

Test #2:

score: -100
Wrong Answer
time: 35ms
memory: 23156kb

input:

28668
2
0 2 1
2
0 0 1
2
0 0 0
2
1 0 1
2
1 0 0
2
1 2 0
3
0 2 1 3 1
3
0 0 1 3 1
3
0 0 0 3 1
3
0 0 0 0 1
3
0 0 0 0 0
3
1 0 1 3 1
3
1 0 0 3 1
3
1 0 0 0 1
3
1 0 0 0 0
3
1 2 0 3 1
3
1 2 0 0 1
3
1 2 0 0 0
3
1 2 1 0 1
3
1 2 1 0 0
3
1 2 1 3 0
3
0 2 3 2 1
3
0 0 3 2 1
3
0 0 0 2 1
3
1 0 3 2 1
3
1 0 0 2 1
3
1 2 ...

output:

1 2 1 
1 2 1 
1 2 1 
1 2 1 
1 2 1 
1 2 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 1 3 1 
1 2 3 2 1 
1 3 3 2 1 
1 2 3 2 1 
1 3 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 1 1 
1 2 3 1 1 
1 2 3...

result:

wrong answer 23rd lines differ - expected: '1 2 3 2 1', found: '1 3 3 2 1 '