QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#186335#5665. AA Country and King DreamoonBUET_POTATOESWA 34ms3832kbC++204.1kb2023-09-23 17:26:112023-09-23 17:26:12

Judging History

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

  • [2023-09-23 17:26:12]
  • 评测
  • 测评结果:WA
  • 用时:34ms
  • 内存:3832kb
  • [2023-09-23 17:26:11]
  • 提交

answer

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

void solv(int ces){
    int n;
    cin >> n;
    vector<int> v;
    vector<int> ase(n + 1);
    for(int i = 0; i < 2 * n - 1; i++){
        int x;
        cin >> x;
        ase[x] = 1;
        v.push_back(x);
    }

    vector<int> nai;
    for(int i = n; i > 0; i--)if(ase[i] == 0)nai.push_back(i);

    vector<int> left;
    vector<int> right;
    int l, r;

    for(int i = 0; i < 2 * n - 1; i++){
        if(v[i] == 0){
            l = i;
            break;
        }
    }
    for(int i = 2 * n - 2; i >= 0; i--){
        if(v[i] == 0){
            r = i;
            break;
        }
    }
    if(l == 0){
        v[0] = 1;
        l++;
    }

    if(r == 2 * n - 2){
        v[r] = 1;
        r--;
    }
    if(n == 1){
        cout << 1 << "\n";
        return;
    }
    if(l > r){
        for(int i = 0; i < 2 * n - 1; i++){
            if(i != 0)cout << ' ';
            cout << v[i];
        }
        cout << "\n";
        return;
    }
    if(!nai.empty() and nai.back() == 1)nai.pop_back();


    int N = 2*n-1;
    vector<int>leftmost(n+2, -1);
    for(int i=r+1; i<N; i++){
        if(leftmost[ v[i] ] == -1){
            leftmost[ v[i] ] = i;
        }
    }

    vector<int>stak = {1};
    vector<bool>vis(n+2);
    vis[1] = 1;
    for(int cur=1; cur<l; cur++){
        if(vis[ v[cur] ]){
            stak.pop_back();
        }
        else{
            stak.push_back( v[cur] );
            vis[ v[cur] ] = true;
        }
    }

    reverse(stak.begin(), stak.end());
    vector<int>lej;
    for(int x : stak){
        if(leftmost[x]!=-1){
            lej.push_back( leftmost[x] );
            break;
        }
    }
    reverse(stak.begin(), stak.end());

    while(true){
        if(lej.back()-1>r){
            int val = v[ lej.back()-1 ];
            int pos = leftmost[ val ];
            lej.push_back(pos);
        }
        else break;
    }

//    cout<<"lej == ";
//    cout<<endl;

    vector<int>lejvals;
    for(int x : lej) {
        lejvals.push_back( v[x] );
    }
    reverse(lejvals.begin(), lejvals.end());
    reverse(lej.begin(), lej.end());

//    cout<<"stak == ";
//    for(int x : stak){
//        cout<<x<<" ";
//    }
//    cout<<endl;
//
//    cout<<"lej == ";
//    for(int x : lejvals){
//        cout<<x<<" ";
//    }
//    cout<<endl;

    for(int i=l; i<=r; i++){
        if(stak.back()==lejvals.back()){
            if(!nai.empty()){
                if(lejvals.size()<=1 or nai.back() < lejvals[lejvals.size()-2] ){
                    v[i] = nai.back();
                    stak.push_back(nai.back());
                    nai.pop_back();
                }
                else{
                    v[i] = lejvals[lejvals.size()-2];
                    stak.push_back(lejvals[lejvals.size()-2]);
                    lejvals.pop_back();
                }
            }
            else{
                v[i] = lejvals.back();
                stak.push_back(lejvals.back());
                lejvals.pop_back();
            }
        }
        else{
            if(!nai.empty()){
                if(nai.back() < stak[ stak.size()-2 ]){
                    v[i] = nai.back();
                    stak.push_back(nai.back());
                    nai.pop_back();
                }
                else{
                    v[i] = stak[ stak.size()-2 ];
                    stak.pop_back();
                }
            }
            else{
                v[i] = stak[ stak.size()-2 ];
                stak.pop_back();
            }
        }
    }

    for(int i=0; i<N; i++){
        cout<<v[i]<<" ";
    }
    cout<<"\n";

}
/*
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
*/
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);


    int tc = 1;
    cin>>tc;
    for(int ces=1; ces<=tc; ces++){
        solv(ces);
    }

    return 0;
}



Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 34ms
memory: 3608kb

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

result:

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