QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#533180 | #5665. AA Country and King Dreamoon | ttbchimbu999# | WA | 44ms | 3764kb | C++20 | 4.1kb | 2024-08-25 18:11:09 | 2024-08-25 18:11:09 |
Judging History
answer
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define all(x) x.begin(), x.end()
using namespace std;
template <class X, class Y> bool minimize(X &a, Y b) {
if (a > b) return a = b, true;
return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
if (a < b) return a = b, true;
return false;
}
const int N = 300005;
int a[2 * N];
void run_case() {
int n; cin >> n;
int m = 2 * n - 1;
vector<bool> flag(n + 1, false);
vector<int> par(n + 1, 0);
vector<int> nxt(n + 1, 0);
for (int i = 1; i <= m; i++) {
cin >> a[i];
flag[a[i]] = true;
}
if (n == 1) {
cout << "1\n";
return;
}
a[1] = a[m] = 1;
flag[1] = 1;
int pos[2];
for (int iter = 0; iter < 2; ++iter) {
for (int i = 1; i <= m; i++) {
if (a[i + 1] == 0) {
pos[iter] = i;
break;
}
}
map<pair<int, int>, bool> MP;
for (int i = 1; i <= m; i++) {
if (a[i + 1] == 0) {
break;
}
int u = a[i];
int v = a[i + 1];
if (MP.find(mp(u, v)) != MP.end()) {
continue;
}
par[v] = u;
MP[mp(u, v)] = MP[mp(v, u)] = true;
}
reverse(a + 1, a + m + 1);
}
pos[1] = m - pos[1] + 1;
pair<int, int> p(1, 1);
vector<int> l(n + 1, 0);
map<int, int> MP;
for (int i = m; i >= 1; --i) {
if (a[i] == 0) break;
MP[a[i]] = i;
}
for (int i = 1; i <= m; ++i) {
if (a[i] == 0) break;
if (MP.count(a[i])) {
p = {i, MP[a[i]]};
}
}
vector<int> node, not_in;
for (int i = 1; i <= n; i++) {
if (!flag[i]) {
not_in.push_back(i);
}
}
sort(all(not_in), greater<int>());
for (int u = a[pos[0]]; u != a[p.fi]; u = par[u]) {
node.push_back(u);
}
node.push_back(a[p.fi]);
int tmp = node.size();
for (int u = a[pos[1]]; u != a[p.fi]; u = par[u]) {
node.push_back(u);
}
if (node.begin() + tmp != node.end()) {
reverse(node.begin() + tmp, node.end());
}
for (int i = 0; i + 1 < (int) node.size(); i++) {
nxt[node[i]] = node[i + 1];
}
nxt[node.back()] = -1;
int cur = node[0];
vector<int> res;
// cerr << "##\n" << '\n';
// for (int x: node) {
// cerr << x << ' ';
// }
// cout << '\n';
while (true) {
if (nxt[cur] > 0) {
int tmp = nxt[cur];
if (!not_in.empty() && not_in.back() < tmp) {
res.push_back(not_in.back());
par[not_in.back()] = cur;
not_in.pop_back();
cur = res.back();
continue;
}
cur = tmp;
res.push_back(cur);
continue;
}
if (nxt[cur] == 0) {
int tmp = par[cur];
if (!not_in.empty() && not_in.back() < tmp) {
res.push_back(not_in.back());
par[not_in.back()] = cur;
not_in.pop_back();
cur = res.back();
continue;
}
cur = tmp;
res.push_back(cur);
continue;
}
if (not_in.empty()) {
break;
}
res.push_back(not_in.back());
par[not_in.back()] = cur;
cur = not_in.back();
not_in.pop_back();
}
// for (int x: res) cerr << x << ' ';
// cerr << "#####\n";
for (int i = 1, t = 0; i <= m; i++) {
if (a[i] == 0) {
if (t == 0) {
for (int x: res) {
cout << x << ' ';
}
t = 1;
}
continue;
}
if (i != pos[1]) cout << a[i] << ' ';
}
cout << '\n';
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t; cin >> t;
while (t --) {
run_case();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3488kb
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 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