QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#134124#5665. AA Country and King DreamoonNicolas125841RE 1ms3472kbC++173.0kb2023-08-03 07:41:502023-08-03 07:41:53

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-03 07:41:53]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3472kb
  • [2023-08-03 07:41:50]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

int main(){
    cin.tie(NULL)->sync_with_stdio(false);

    int t;
    cin >> t;

    while(t--){
        int n;
        cin >> n;

        vector<int> path(2 * n - 1);
        
        for(int i = 0; i < path.size(); i++)
            cin >> path[i];

        path[0] = path[path.size() - 1] = 1;

        vector<int> fjmp(n + 1, -1);
        vector<int> bjmp(n + 1, -1);
        vector<int> pstack;
        set<int> fnodes;
        int ind = 0;

        for(int i = 1; i <= n; i++)
            fnodes.insert(i);

        for(int i = 0; i < path.size(); i++){
            if(path[i] == 0)
                break;

            ind = i + 1;
            
            fjmp[path[i]] = i;

            if(pstack.size() > 1 && path[i] == pstack[pstack.size() - 2])
                pstack.pop_back();
            else
                pstack.push_back(path[i]);

            fnodes.erase(path[i]);
        }

        for(int i = path.size()-1; i >= 0; i--){
            if(path[i] == 0)
                break;
            
            fnodes.erase(path[i]);

            bjmp[path[i]] = i;
        }

        int snode = 1;
        int bp = bjmp[1];
        
        while(fjmp[path[bp - 1]] != -1){
            snode = path[bp - 1];

            bp = bjmp[path[bp - 1]];
        } 

        while(path[ind] == 0){
            if(!fnodes.empty()){
                if(pstack.back() == snode){
                    if(path[bp - 1] == 0 || *fnodes.begin() < path[bp - 1]){
                        pstack.push_back(*fnodes.begin());

                        path[ind] = *fnodes.begin();

                        fnodes.erase(fnodes.begin());
                    }else{
                        path[ind] = path[bp - 1];
                        snode = path[bp - 1];

                        pstack.push_back(path[bp - 1]);

                        bp = bjmp[path[bp - 1]];
                    }
                }else{
                    if(*fnodes.begin() < pstack[pstack.size() - 2]){
                        pstack.push_back(*fnodes.begin());

                        path[ind] = *fnodes.begin();

                        fnodes.erase(fnodes.begin());
                    }else{
                        pstack.pop_back();
                        
                        path[ind] = pstack.back();
                    }
                }               
            }else{
                assert(!(pstack.back() == snode && path[bp - 1] == 0));

                if(pstack.back() == snode){
                    path[ind] = path[bp - 1];
                    snode = path[bp - 1];

                    pstack.push_back(path[bp - 1]);

                    bp = bjmp[path[bp - 1]];
                }else{
                    pstack.pop_back();
                        
                    path[ind] = pstack.back();
                }
            }

            ind++;
        }

        for(int &v : path)
            cout << v << " ";

        cout << "\n";
    }
}

详细

Test #1:

score: 100
Accepted
time: 1ms
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
Dangerous Syscalls

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:


result: