QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#89548 | #5665. AA Country and King Dreamoon | phtniit | WA | 37ms | 31704kb | C++11 | 3.6kb | 2023-03-20 15:46:20 | 2023-03-20 15:46:22 |
Judging History
answer
#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast","-funroll-loops")
//#pragma GCC target("sse4.1","sse4.2","ssse3","sse3","sse2","sse","avx2","avx","popcnt","tune=native")
using namespace std;
typedef long double ldb;
typedef long long i64;
typedef unsigned long long u64;
typedef unsigned int u32;
typedef pair<int, int> pii;
// priority_queue<int, vector<int>, greater<int>> minq;
// ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// fflush(stdout);
const int inf = 1000000007;
const i64 prm = 998244353;
const i64 inf2 = ((i64)inf) * inf;
const i64 bone = 1;
const int maxn = 600010;
inline int read(){
int x=0,f=0; char ch=getchar();
while(!isdigit(ch)) f|=(ch==45),ch=getchar();
while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
return f?-x:x;
}
/*
1. 请再三确认题意
2. 对于复杂的数据结构+贪心/结论/计数题,可以考虑先写暴力,确认想法正确,再逐步优化
*/
set<int> g[maxn];
void once() {
int n = read();
static int a[maxn];
for (int i = 1; i <= n; ++i) {
g[i].clear();
}
for (int i = 1; i < n*2; ++i) {
a[i] = read();
}
if (n == 1) {
puts("1");
return;
}
a[1] = a[n*2-1] = 1;
int F = 0, L = 0;
for (int i = 1; i < n*2; ++i) if (a[i] == 0) {
L = i;
}
for (int i = n*2-1; i > 0; --i) if (a[i] == 0) {
F = i;
}
if (F == 0) {
for (int i = 1; i < n*2; ++i) {
printf("%d ", a[i]);
}
puts("");
return;
}
for (int i = 2; i < F; ++i) {
g[a[i-1]].insert(a[i]);
g[a[i]].insert(a[i-1]);
}
for (int i = L+1; i+1 < n*2; ++i) {
g[a[i]].insert(a[i+1]);
g[a[i+1]].insert(a[i]);
}
vector<int> route;
std::function<bool(int, int)> dfs = [&](int u, int fa) {
route.push_back(u);
if (u == a[L+1]) {
return true;
}
for (auto v: g[u]) if (v != fa) {
if (dfs(v, u)) {
return true;
}
}
route.pop_back();
return false;
};
dfs(a[F-1], 0);
static int vis[maxn];
for (int i = 1; i <= n; ++i) vis[i] = 0;
for (int i = 1; i < n*2; ++i) {
vis[a[i]] = 1;
}
queue<int> q;
for (int i = 1; i <= n; ++i) if (!vis[i]) {
q.push(i);
}
vector<int> ans;
ans.push_back(route[0]);
for (int i = 1; i <= route.size(); ++i) {
vector<int> vt;
while (not q.empty() && (i == route.size() || q.front() < route[i] || q.front() < route[i-1])) {
vt.emplace_back(q.front());
q.pop();
}
if (vt.size() > 0) {
int pre = ans.back();
ans.push_back(vt[0]);
for (int j = 1; j < vt.size(); ++j) {
if (vt[j] < pre) {
ans.push_back(vt[j]);
ans.push_back(vt[0]);
} else {
ans.push_back(pre);
ans.push_back(vt[j]);
}
}
ans.push_back(pre);
}
if (i < route.size()) {
ans.push_back(route[i]);
}
}
assert(ans.size() == L+1 - (F-1) + 1);
for (int k = 0, i = F-1; k < ans.size(); ++k, ++i) {
a[i] = ans[k];
}
for (int i = 1; i < n*2; ++i) {
printf("%d ", a[i]);
}
puts("");
}
int main() {
int tes = 1;
tes = read();
/*
if (tes > 10000) {
for (int h = 1; h <= tes; ++h) {
int n = read();
static int a[maxn];
for (int i = 1; i < n*2; ++i) {
a[i] = read();
}
if (h == 3689) {
for (int i = 1; i < n*2; ++i) {
printf("%d ", a[i]);
}
puts("");
return 0;
}
}
}*/
while (tes--) {
once();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 31552kb
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 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