QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#214313 | #5665. AA Country and King Dreamoon | Jeffrey# | RE | 3ms | 10640kb | C++14 | 2.8kb | 2023-10-14 18:45:06 | 2023-10-14 18:45:06 |
Judging History
answer
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <queue>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;
vector<int> g[300005];
int main() {
int t;
cin >> t;
while (t--) {
int n, u, v;
cin >> n;
for (int i = 1; i <= n; i++) g[i].clear();
vector<int> a(n * 2 + 1), c(n * 2 + 1);
for (int i = 1; i < n * 2; i++) cin >> a[i], c[a[i]] = 1;
a[1] = a[n * 2 - 1] = 1; c[1] = 1;
set<pii> s;
for (int i = 2; i < n * 2; i++) if (a[i] && a[i - 1]) {
pii x = {min(a[i], a[i - 1]), max(a[i], a[i - 1])};
if (s.find(x) == s.end()) s.insert(x);
else s.erase(x);
}
for (pii i : s) g[i.first].push_back(i.second), g[i.second].push_back(i.first);
queue<int> q;
int e = 0, f = 0, y = 0, w = 0;
for (int i = 1; i < n * 2; i++) {
if (!a[i]) y = 1;
if (!a[i] && !w) w = i;
if (a[i] && y == 1) y = 2;
if (!y) e = a[i];
if (y == 2 && !f) f = a[i];
}
vector<int> b = {e}, z;
q.push(e);
while (b.back() != f) {
int x = q.front();
q.pop();
for (int i : g[x]) {
if (b.size() == 1 || i != b[(int)b.size() - 2]) {
q.push(i);
b.push_back(i);
break;
}
}
}
set<int> t;
for (int i = 1; i <= n; i++) if (!c[i]) t.insert(i);
//for (int i : t) cout << i << ' '; cout << '\n';
for (int i = 0; i < b.size(); i++) {
z.push_back(b[i]);
if (t.empty()) continue;
while (i == (int)b.size() - 1 || *t.begin() < b[i + 1]) {
vector<int> x = {b[i], *t.begin()};
z.push_back(*t.begin());
t.erase(t.begin());
while (x.size() > 1) {
int u = (!t.empty() ? *t.begin() : n + 1), v = x[(int)x.size() - 2];
if (u < v) {
x.push_back(u);
z.push_back(u);
t.erase(t.begin());
} else {
x.pop_back();
z.push_back(x.back());
}
}
if (t.empty()) break;
}
}
//for (int i : b) cout << i << ' '; cout << ',';
//for (int i : z) cout << i << ' '; cout << '\n';
for (int i = 1; i + 1 < z.size(); i++) a[i + w - 1] = z[i];
for (int i = 1; i < n * 2; i++) cout << a[i] << " \n"[i == n * 2 - 1];
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 10640kb
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
Runtime Error
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 ...