QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#553934 | #5665. AA Country and King Dreamoon | BongoCatEnjoyer# | WA | 36ms | 3540kb | C++20 | 3.6kb | 2024-09-08 23:19:03 | 2024-09-08 23:19:03 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define forn(i,n) for(int i = 0; i <(int)n; ++i)
#define vi vector<int>
#define debug(x) cout << #x << " : " << x << endl;
const int MOD = 1000000007;
//const int MOD = 998244353;
int n;
int peek(stack<int>& st, int x) {
stack<int> tmp;
forn(i, x-1) {
tmp.push(st.top());
st.pop();
}
int ans = st.top();
while (!tmp.empty()) {
st.push(tmp.top());
tmp.pop();
}
return ans;
}
int peek(vi& v, int x) {
return v[v.size() - x];
}
void init() {
}
void solve() {
cin >> n;
init();
vi udah(n+5);
vi vec(2*n-1);
forn(i, 2*n-1) {
cin >> vec[i];
vec[0] = vec[2*n-2] = 1;
}
stack<int> st, en;
forn(i, 2*n-1) {
udah[vec[i]] = 1;
if (vec[i] == 0) break;
}
forn(i, 2*n-1) {
if (vec[i] == 0) break;
if (st.size() >= 2 && peek(st, 2) == vec[i]) {
st.pop();
}
else {
st.push(vec[i]);
}
}
for (int i = 2*n-2; i >= 0; i--) {
if (vec[i] == 0) break;
if (en.size() >= 2 && peek(en, 2) == vec[i]) {
en.pop();
}
else {
en.push(vec[i]);
}
}
vi stv, env;
while (!st.empty()) {
stv.push_back(st.top());
st.pop();
}
reverse(stv.begin(), stv.end());
while (!en.empty()) {
env.push_back(en.top());
en.pop();
}
reverse(env.begin(), env.end());
vi posen(n+5, -1);
forn(i, env.size()) {
posen[env[i]] = i;
}
// cout << "\n\n\n";
// forn(i, stv.size()) cout << stv[i] << ' '; cout << endl;
// forn(i, env.size()) cout << env[i] << ' '; cout << endl;
set<int> havent;
forn(i, n) {
if (!udah[i+1]) havent.insert(i+1);
}
int nx = 0;
for (int i = 0; i < stv.size() && i < env.size(); i++) {
if (stv[i] == env[i]) {
nx++;
}
else {
break;
}
}
forn(i, 2*n-1) {
if (vec[i] == 0) {
if (nx < stv.size() && stv.size() >= 2) {
// boleh pop | push
int peekval = peek(stv, 2);
int pushval;
if (havent.empty()) pushval = LLONG_MAX;
for (auto pp: havent) {
if (posen[pp] == -1 || posen[pp] == stv.size()) {
pushval = pp;
break;
}
}
if (peekval < pushval) {
stv.pop_back();
vec[i] = peekval;
}
else {
stv.push_back(pushval);
vec[i] = pushval;
havent.erase(pushval);
}
}
else {
// boleh push doang (+ cek di bawah)
int pushval = *havent.begin();
stv.push_back(pushval);
vec[i] = pushval;
havent.erase(pushval);
}
while (nx < stv.size() && nx < env.size() && stv[nx] == env[nx]) {
nx++;
}
}
}
forn(i, 2*n-1) {
cout << vec[i] << ' ';
}cout << '\n';
}
int32_t main()
{
ios::sync_with_stdio(false); cin.tie(NULL);
int tc = 1;
cin >> tc;
while(tc--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3532kb
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: 36ms
memory: 3540kb
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 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