QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#95718 | #5665. AA Country and King Dreamoon | SGColin# | RE | 2ms | 7628kb | C++20 | 1.9kb | 2023-04-11 16:17:17 | 2023-04-11 16:17:19 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
inline int rd() {
int x = 0;
bool f = 0;
char c = getchar();
for (; !isdigit(c); c = getchar()) f |= (c == '-');
for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
return f ? -x : x;
}
#define N 1000007
int a[N], pos[N], fa[N];
set<int> s;
vector<int> seq;
deque<int> tmp;
inline void work() {
int n = rd();
s.clear();
for (int i = 1; i <= n; ++i) {
s.insert(i); pos[i] = 0; fa[i] = 0;
}
int L = 0, R = 0;
for (int i = 1; i < 2 * n; ++i) a[i] = rd();
a[1] = a[2 * n - 1] = 1;
for (int i = 1; i < 2 * n; ++i) s.erase(a[i]);
for (int i = 2; i < 2 * n; ++i) if (!a[i]) {if (!L) L = i; R = i;}
if (!L) {
for (int i = 1; i < 2 * n; ++i) printf("%d ", a[i]);
puts(""); return;
}
int lca = 0, posr = 0, posl = 0;
for (int i = 1; i < L; ++i) pos[a[i]] = i;
for (int i = R + 1; i < 2 * n; ++i)
if (pos[a[i]]) {lca = a[i]; posl = pos[a[i]]; posr = i; break;}
for (int i = 1; i < L; ++i) if (!fa[a[i]]) fa[a[i]] = a[i - 1];
for (int i = 2 * n - 1; i; --i) if (!fa[a[i]]) fa[a[i]] = a[i + 1];
seq.clear(); tmp.clear();
int nw = a[L - 1];
while (nw != lca) {
nw = fa[nw];
tmp.push_back(nw);
}
nw = a[R + 1];
while (nw != lca) {
nw = fa[nw];
seq.push_back(nw);
}
if (!seq.empty() && seq.back() == lca) seq.pop_back();
reverse(seq.begin(), seq.end());
nw = a[L - 1];
for (int i = L; i <= R; ++i) {
//if (!s.empty()) printf("%d %d\n", *s.begin(), nw);
if (s.empty()) {
nw = a[i] = tmp.front(); tmp.pop_front();
} else if (tmp.empty() || (*s.begin() < tmp.front())) {
a[i] = *s.begin(); s.erase(s.begin());
tmp.push_front(nw); nw = a[i];
} else {
nw = a[i] = tmp.front(); tmp.pop_front();
}
}
for(int i = 1; i < 2 * n; ++i) printf("%d ", a[i]);
puts("");
}
int main() {
for (int t = rd(); t; --t) work();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 7628kb
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:
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 1 3 2 1 1 3 1 2 1 1 1 3 2 1 1 3 1 2 1 1 2 3 2 1 1 2 3 2 1 1 2 3 2 1 1 2 3...