QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#134118#5665. AA Country and King DreamoonNicolas125841WA 32ms3592kbC++174.0kb2023-08-03 07:00:172023-08-03 07:00:19

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:00:19]
  • 评测
  • 测评结果:WA
  • 用时:32ms
  • 内存:3592kb
  • [2023-08-03 07:00:17]
  • 提交

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.empty() && path[i] == pstack.back())
                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];

        //cout << bp << "\n";
        
        while(fjmp[path[bp - 1]] != -1){
            snode = path[bp - 1];

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

            //cout << bp << "\n";
        } 

        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(path[bp - 1] == 0){
                    if(*fnodes.begin() < pstack.back()){
                        pstack.push_back(*fnodes.begin());

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

                        fnodes.erase(fnodes.begin());
                    }else{
                        pstack.pop_back();
                        
                        path[ind] = pstack.back();
                    }
                }else{
                    if(*fnodes.begin() < path[bp - 1] && *fnodes.begin() < pstack.back()){
                        pstack.push_back(*fnodes.begin());

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

                        fnodes.erase(fnodes.begin());
                    }else if(path[bp - 1] < pstack.back() && path[bp - 1] < *fnodes.begin()){
                        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();
                    }
                }                
            }else{
                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 if(path[bp - 1] == 0 || pstack.back() < path[bp - 1]){
                    pstack.pop_back();
                        
                    path[ind] = pstack.back();
                }else{
                    path[ind] = path[bp - 1];
                    snode = path[bp - 1];

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

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

            ind++;
        }

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

        cout << "\n";
    }
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3500kb

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: 32ms
memory: 3592kb

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 34th lines differ - expected: '1 3 1 2 1', found: '1 3 2 2 1 '