QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#732726#5665. AA Country and King DreamoonnekoyellowWA 0ms3592kbC++231.5kb2024-11-10 15:41:522024-11-10 15:41:54

Judging History

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

  • [2024-11-10 15:41:54]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3592kb
  • [2024-11-10 15:41:52]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

void solve() {
    int n;
    cin >> n;
    vector<int> a(n*2-1);
    for (auto &e: a)
        cin >> e;

    set<int> rem;
    for (int i = 2; i <= n; i++) {
        rem.emplace(i);
    }

    a[0] = 1;

    vector<int> cnt(n+1);
    for (auto e: a) {
        cnt[e]++;
        if (rem.count(e)) rem.extract(e);
    }

    vector<bool> ins(n+1);
    ins[1] = 1;
    vector<int> stk;
    stk.push_back(1);

    auto sback = [&]() {
        assert(stk.size() > 1);
        return stk[stk.size() - 2];
    };

    for (int i = 1; i < n*2-1; i++) {
        cnt[a[i]]--;
        if (a[i]) {
            if (ins[a[i]]) {
                ins[stk.back()] = 0;
                stk.pop_back();
            } else {
                stk.push_back(a[i]);
                ins[a[i]] = 1;
            }
        } else {
            if (rem.size() && rem.size() == cnt[0]+1 || stk.size() == 1 || sback() > *rem.begin()) {
                int nxt = *rem.begin();
                a[i] = nxt;
                stk.push_back(nxt);
                ins[nxt] = 1;
                rem.extract(nxt);
            } else {
                ins[stk.back()] = 0;
                stk.pop_back();
                a[i] = stk.back();
            }
        }
    }

    for (int i = 0; i < n*2-1; i++) {
        cout << a[i] << " \n"[i == n*2-2];
    }
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    
    int t;
    cin >> t;
    for (; t; t--) solve();

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3592kb

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 1 3 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 0 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 0
1 2 1 3 1 4 1 5 0

result:

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