QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#510688#5665. AA Country and King DreamoonchimeraWA 88ms3764kbC++112.3kb2024-08-09 11:03:252024-08-09 11:03:26

Judging History

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

  • [2024-08-09 11:03:26]
  • 评测
  • 测评结果:WA
  • 用时:88ms
  • 内存:3764kb
  • [2024-08-09 11:03:25]
  • 提交

answer

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

vector<ll> process(ll N, vector<ll>& ops, set<ll>& avail) {
    vector<bool> istk(N, false);
    vector<ll> stk;

    for(auto x: ops) {
        if(istk[x]) {
            stk.pop_back();
        } else {
            if(avail.find(x) != avail.end()) avail.erase(x);
            stk.push_back(x); istk[x] = true;
        }
    }

    return stk;
}


void solve() {
    ll N; cin >> N; vector<ll> P(2*N-1); for(ll i = 0; i < 2*N-1; i++) cin >> P[i];

    if(P[0] == 0) P[0] = 1;
    if(P.back() == 0) P.back() = 1;

    set<ll> avail; for(ll i = 1; i <= N; i++) avail.insert(i);

    vector<ll> prefix;
    for(ll i = 0; i < N; i++) {
        if(P[i]) prefix.push_back(P[i]); else break;
    }

    vector<ll> suffix;
    for(ll i = 2*N-2; i >= 0; i--) {
        if(P[i]) suffix.push_back(P[i]); else break;
    }

    auto sstk = process(N, suffix, avail);
    auto pstk = process(N, prefix, avail);

    
    ll NCP = 0;
    while(NCP < pstk.size() && NCP < sstk.size() && pstk[NCP] == sstk[NCP]) NCP++;

    bool ICP = pstk.size() == NCP;

    for(ll i = 0; i < 2*N - 1; i++) {
        if(!P[i]) {
            ll obligation = 3*N;

            if(ICP) {
                if(sstk.size() > pstk.size()) {
                    obligation = sstk[pstk.size()];
                }

                if(!avail.size() || ((*avail.begin()) > obligation)) {
                    P[i] = obligation; pstk.push_back(obligation);
                } else {
                    P[i] = (*avail.begin()); pstk.push_back(*avail.begin()); avail.erase(avail.begin()); ICP = false;
                }
            } else {
                if(pstk.size() > 1) obligation = pstk[pstk.size()-2];
                if(!avail.size() || ((*avail.begin()) > obligation)) {
                    P[i] = obligation; pstk.pop_back();
                    ICP = (pstk.size() == 0) || (pstk.size() <= sstk.size() && pstk.back() == sstk[pstk.size()-1]);
                } else {
                    P[i] = (*avail.begin()); pstk.push_back(*avail.begin()); avail.erase(avail.begin()); ICP = false;
                }
            }


        }

        cout << P[i] << " ";
    }

    cout << "\n";


}


int main() {
    ll T; cin >> T;
    while(T--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 88ms
memory: 3764kb

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

result:

wrong answer 72nd lines differ - expected: '1 2 1 3 1 4 1', found: '1 2 1 3 1 1 1 '