QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#184516 | #5665. AA Country and King Dreamoon | ucup-team1508# | RE | 3ms | 34572kb | C++14 | 2.6kb | 2023-09-20 20:27:58 | 2023-09-20 20:27:59 |
Judging History
answer
#include<bits/stdc++.h>
#define F(i, a, b) for(int i = a; i <= b; i ++)
#define Fd(i, a, b) for(int i = a; i >= b; i --)
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
using namespace std;
const int N = 1e6 + 10;
const int inf = 1e7 + 2;
int n, a[N], fa[N], vst[N], now[N];
vector<int> son[N];
void sol() {
cin >> n;
F(i, 1, 2 * n - 1) cin >> a[i];
a[1] = a[2 * n - 1] = 1;
F(i, 1, n) fa[i] = vst[i] = now[i] = 0;
F(i, 1, n) son[i].clear();
vst[1] = 3;
int pos = 0;
F(i, 2, 2 * n - 1) {
if(a[i] == 0) {
pos = i;
break;
}
if(! vst[a[i]]) vst[a[i]] = 1, fa[a[i]] = a[i - 1];
else if(fa[a[i - 1]] == a[i]) vst[a[i - 1]] = 3;
}
Fd(i, 2 * n - 2, 2) {
if(a[i] == 0) break;
if(vst[a[i]] == 0) vst[a[i]] = 2, fa[a[i]] = a[i + 1];
if(vst[a[i]] == 2) {
if(fa[a[i]] == a[i - 1]) vst[a[i]] = 3;
}
if(vst[a[i]] == 1) {
if(fa[a[i]] == a[i + 1]) vst[a[i]] = 3;
}
}
// F(i, 1, n) cout << fa[i] << " \n" [i == n];
// F(i, 1, n) cout << vst[i] << " \n" [i == n];
priority_queue<int, vector<int>, greater<int> > q;
F(i, 1, n) {
if(vst[i] == 2) {
son[fa[i]].pb(i);
}
if(! vst[i]) q.push(i);
}
F(i, pos, 2 * n - 1) {
if(a[i]) break;
int x = inf, y = fa[a[i - 1]], z = inf;
// while(q.size() && (vst[q.top()] == 1 || vst[q.top()] == 3)) q.pop();
if(q.size()) x = q.top();
if(now[a[i - 1]] < son[a[i - 1]].size()) z = son[a[i - 1]][now[a[i - 1]]];
// cout << i << " " << x << " " << y << " " << z << "\n";
if((vst[a[i - 1]] == 2 || vst[a[i - 1]] == 3 || z < y) && z < x) {
now[a[i - 1]] ++;
a[i] = z;
vst[a[i]] = 3;
continue;
}
if(vst[a[i - 1]] == 2 || vst[a[i - 1]] == 3 || x < y) {
a[i] = q.top();
fa[a[i]] = a[i - 1];
vst[a[i]] = 1;
q.pop();
continue;
}
vst[a[i - 1]] = 3;
a[i] = fa[a[i - 1]];
}
F(i, 1, 2 * n - 1) cout << a[i] << " \n" [i == 2 * n - 1];
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t; cin >> t;
while(t --) sol();
}
/*
1
5
1 0 0 0 3 4 3 2 1
*/
/*
1
5
1 5 0 0 0 0 2 3 1
*/
/*
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
*/
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 34572kb
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 ...