QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#134124 | #5665. AA Country and King Dreamoon | Nicolas125841 | RE | 1ms | 3472kb | C++17 | 3.0kb | 2023-08-03 07:41:50 | 2023-08-03 07:41:53 |
Judging History
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 ...