QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#84218#5665. AA Country and King DreamoonHOLICWA 109ms5424kbC++202.3kb2023-03-06 00:26:042023-03-06 01:00:40

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-06 01:00:40]
  • 评测
  • 测评结果:WA
  • 用时:109ms
  • 内存:5424kb
  • [2023-03-06 00:26:04]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 1009;
int n, a[N], vis[N], fa[N], dep[N];
void work() {
    cin >> n;
    for(int i = 1; i < 2 * n; i ++) cin >> a[i];
    a[1] = a[2 * n - 1] = 1;
    int l = 2 * n, r = 0;
    for(int i = 1; i < 2 * n; i ++) {
        if(a[i] == 0) {
            l = min(l, i);
            r = max(r, i);
        }
    }
    for(int i = 1; i <= n; i ++) dep[i] = vis[i] = fa[i] = 0;
    int now = 0;
    for(int i = 1; i < l; i ++) {
        if(vis[a[i]]) now = a[i];
        else fa[a[i]] = now, dep[a[i]] = dep[now] + 1, now = a[i];
        vis[a[i]] = 1;
    }
    set<int> s;
    for(int i = 1; i <= n; i ++) s.insert(i), vis[i] = 0;
    int Now = 0;
    for(int i = 2 * n - 1; i > r; i --) {
        if(vis[a[i]]) Now = a[i];
        else fa[a[i]] = Now, dep[a[i]] = dep[Now] + 1, Now = a[i];
        vis[a[i]] = 1;
    }
    for(int i = 1; i <= n; i ++)
        s.erase(a[i]);
    int num = r - l + 1;
    vector<int> a1, a2;
    int x = now, X = Now;
    //cout << x << " " << X << " " << dep[x] << " " << dep[X] << endl;
    while(x != X) {
        if(dep[x] >= dep[X]) {
            x = fa[x];
            if(x != X) a2.push_back(x);
        }
        else {
            X = fa[X];
            if(x != X) a2.push_back(X);
        }
        //cout << x << " " << X << endl;
    }
    if(now != Now)a2.push_back(Now);
    reverse(a2.begin(), a2.end());
    for(auto v : a2) a1.push_back(v);
    //for(auto v : a1) cout << v << " ";
    //cout << endl;

    reverse(a1.begin(), a1.end());

    for(int i = 1; i < l; i ++) cout << a[i] << " ";
   // cout << l << " " <<r << endl;
    for(int i = l; i <= r; i ++) {
        if(!s.empty() && (a1.empty() || *s.begin() < a1.back())) {
            a1.push_back(now);
            now = *s.begin();
            s.erase(s.begin());
            cout << now << " ";
        } else {
            cout << a1.back() << " ";
            now = a1.back();
            a1.pop_back();
        }
    }
    for(int i = r + 1; i < 2 * n; i ++) cout << a[i] << " ";
    cout << endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int Case;
    cin >> Case;
    while(Case --) work();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 5308kb

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: 109ms
memory: 5424kb

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 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 3 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 2 3 2 1 
1 2 3 2 1 
1 2 2 2 1 
1 2 3 2 1 
1 2 2 2...

result:

wrong answer 1st lines differ - expected: '1 2 1', found: '1 2 1 1 2 1 '