1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
#include <bits/stdc++.h> #define ll long long #define debug(...) ((void)0) #ifndef debug #define debug(...) fprintf(stderr,__VA_ARGS__) #endif namespace ringo { template <class T> inline void read(T &x) { x = 0; register char c = getchar(); register bool f = 0; while (!isdigit(c)) f ^= c == '-', c = getchar(); while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); if (f) x = -x; } template <class T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar('0' + x % 10); } template <class T> inline void print(T x, char c) { print(x), putchar(c); } template <class T> inline void print(T a, int l, int r, std::string s = "") { if (s != "") std::cout << s << ": "; for (int i = l; i <= r; i++) print(a[i], " \n"[i == r]); }
const int N = 1e6 + 10, L = 21; int n, m; int a[N], del[N], use[N], log[N]; std::vector<int> v[N], G[N];
struct info { int val, id; inline info() {} inline info(int k) { val = a[k], id = k; } inline info(int a, int b) { val = a, id = b; } }; std::vector<info> s, h, t, bkt[N]; std::priority_queue<int, std::vector<int>, std::greater<int>> q;
void noSolution() { puts("no"), exit(0); }
struct minimax { int min, max; inline minimax operator^(const minimax &other) const { return {std::min(min, other.min), std::max(max, other.max)}; } };
namespace seg { minimax b[N]; struct node { int l, r, mid; minimax x; } p[N << 2]; void build(int u, int l, int r) { p[u].l = l, p[u].r = r, p[u].mid = (l + r) >> 1; if (l == r) { p[u].x = b[l]; return; } build(u << 1, l, p[u].mid); build(u << 1 | 1, p[u].mid + 1, r); p[u].x = p[u << 1].x ^ p[u << 1 | 1].x; } minimax query(int u, int l, int r) { if (p[u].l == l && p[u].r == r) return p[u].x; if (r <= p[u].mid) return query(u << 1, l, r); else if (l > p[u].mid) return query(u << 1 | 1, l, r); else return query(u << 1, l, p[u].mid) ^ query(u << 1 | 1, p[u].mid + 1, r); } void init() { static int tmp[N]; memset(tmp, 0, sizeof tmp); for (int i = 1; i <= m; i++) if (a[i]) { if (!tmp[a[i]]) tmp[a[i]] = i; b[i].min = tmp[a[i]]; } else { b[i].min = i; } memset(tmp, 0, sizeof tmp); for (int i = m; i >= 1; i--) if (a[i]) { if (!tmp[a[i]]) tmp[a[i]] = i; b[i].max = tmp[a[i]]; } else { b[i].max = i; } build(1, 1, m); } minimax query(int l, int r) { return query(1, l, r); } } using namespace seg;
inline bool possible(int l, int r) { if (!((r - l + 1) & 1)) return false; if (l + 1 > r - 1) return true; auto it = query(l + 1, r - 1); return it.max <= r && it.min >= l; }
inline int newNode() { static int cur = 1; while (use[cur]) cur++; if (cur > n) std::cerr << "[No enougth nodes] No solution.\n", noSolution(); return use[cur] = 1, cur; }
void solve(int x) { std::vector<std::pair<int, int>> jump; for (auto y : G[x]) { solve(y); jump.push_back(std::make_pair(*v[y].begin(), *--v[y].end())); } std::reverse(jump.begin(), jump.end()); for (int i = 0, L, R, at = 0; i + 1 < v[x].size(); i++) { L = v[x][i], R = v[x][i + 1]; if (!((R - L + 1) & 1)) noSolution(); s.clear(), s.push_back(info(L)); for (int i = L + 1; i <= R - 1; i++) { s.push_back(info(i)); if (at < jump.size() && i == jump[at].first) i = jump[at++].second; } s.push_back(info(R));
int c0 = 0, c1 = 0; for (int i = 1; i + 1 < s.size(); i++) s[i].val ? ++c1 : ++c0; if (c0 < c1 - 1) noSolution();
int tl = 0, tr = s.size() - 1; while (tl < s.size() && s[tl].val) tl++; while (tr >= 0 && s[tr].val) tr--; if (tl == s.size()) continue; for (int i = tl; i <= tr; i++) if (s[i].val) { if (t.size() >= 2 && (--t.end())->val && !(----t.end())->val) { a[(----t.end())->id] = s[i].val; del[(----t.end())->id] = del[(--t.end())->id] = 1; t.pop_back(), t.pop_back(), t.push_back(s[i]); } else { t.push_back(s[i]); } } else { if (t.size() >= 2 && (--t.end())->val && (----t.end())->val) { a[s[i].id] = (----t.end())->val; del[s[i].id] = del[(--t.end())->id] = 1; t.pop_back(); } else { t.push_back(s[i]); } }
t.clear(), std::swap(t, s); for (int i = 0; i < t.size(); i++) if (del[t[i].id]) del[t[i].id] = false; else s.push_back(t[i]); t.clear();
for (int i = 0, j = s.size() - 1; i <= j; ) { if (s[i].val) { h.push_back(s[i++]); } else if (s[j].val) { t.push_back(s[j--]); } else { if (h.size() >= 2) { a[s[i].id] = s[i].val = (----h.end())->val; h.pop_back(), i++; } else if (t.size() >= 2) { a[s[j].id] = s[j].val = (----t.end())->val; t.pop_back(), j--; } else { a[s[i].id] = s[i].val = newNode(); h.push_back(s[i++]); } } } h.clear(), t.clear(), s.clear(); } }
void main() { read(n), m = (n << 1) - 1, log[0] = -1; for (int i = 1; i <= m; i++) log[i] = log[i >> 1] + 1; for (int i = 1; i <= m; i++) { read(a[i]); if (a[i]) use[a[i]] = 1; } if (a[1] && a[m] && a[1] != a[m]) noSolution(); else if (!a[1] && !a[m]) { init(); int any = 0; for (int i = 3; i <= m - 2; i++) { if (a[i] && a[i] != a[2] && a[i] != a[m - 1] && possible(1, i) && possible(i, m)) any = std::max(any, a[i]); } std::cerr << "any = " << any << std::endl; a[1] = a[m] = any ? any : newNode(); } else a[1] = a[m] = a[1] | a[m]; init(); for (int i = 1; i <= m; i++) if (a[i]) v[a[i]].push_back(i); for (int i = 1; i <= n; i++) if (v[i].size() > 1) { for (int j = 0; j + 1 < v[i].size(); j++) if (!possible(v[i][j], v[i][j + 1])) { std::cerr << "[Impossible section] " << v[i][j] << " " << v[i][j + 1] << std::endl; noSolution(); } bkt[*--v[i].end()].push_back({*v[i].begin(), i}); } for (int i = 1; i <= m; i++) { for (auto it : bkt[i]) { while (s.size() && (--s.end())->val >= it.val) { G[it.id].push_back((--s.end())->id); s.pop_back(); } s.push_back(it); } } s.clear(); solve(a[1]); puts("yes"); print(a, 1, m); }
} signed main() { #ifdef memset0 freopen("1.in", "r", stdin); #endif return ringo::main(), 0; }
|