前言 发现这场还有两题要补,还没做,备忘一下。
今晚希望渡劫成功,明天还要早八,不能太兴奋…
虽然说这些题解应该是打完比赛就更的,但是补题补了好久,还发现这场的鸽了(
复盘 H - War of Inazuma (Easy Version) 二次元检测题
题解说这是个二分图,所以直接按二进制中$1$的位数染色就可以了。
更直接的,可以通过构造格雷码染色,是一定符合题意的。
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 #include <bits/stdc++.h> #define maxn 5000005 #define int long long using namespace std ;template <typename T>void Print (T value) { std ::cout << value << '\n' ; } template <typename Head, typename ... Rail>void Print (Head head, Rail... rail) { std ::cout << head << ", " ; Print(rail...); } int ans[maxn];int g (int n) { return n ^ (n >> 1 ); }int read () { int x = 0 ; char ch = getchar(); while (ch < '0' || ch > '9' ) ch = getchar(); while (ch >= '0' && ch <= '9' ) x = x * 10 + ch - '0' , ch = getchar(); return x; } signed main (void ) { int n = read (); int flag = 0 ; for (int i = 0 ; i < (1 << n); i++) ans[g(i)] = flag, flag ^= 1 ; for (int i = 0 ; i < (1 << n); i++) printf ("%d" , ans[i]); putchar ('\n' ); return 0 ; }
F - Train Wreck 入栈时不能出现相同的颜色序列 $\Rightarrow$ 一颗树上同一深度的点都不能有相同颜色
既然如此,我们先根据括号序列建树,然后统计每一层有多少节点需要染色。
在染色时,根据贪心,我们尽可能选择颜色数量较多的进行染色,这个可以用一个优先队列维护。
由此从上往下染色即可。
代码:
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 #include <bits/stdc++.h> #define maxn 2000005 #define int long long using namespace std ;clock_t START_TIME, END_TIME;template <typename T>void Print (T value) { std ::cout << value << '\n' ; } template <typename Head, typename ... Rail>void Print (Head head, Rail... rail) { std ::cout << head << ", " ; Print(rail...); } int read () { int x = 0 ; char ch = getchar(); while (ch < '0' || ch > '9' ) ch = getchar(); while (ch >= '0' && ch <= '9' ) x = x * 10 + ch - '0' , ch = getchar(); return x; } struct Node { int c; int v; }; bool operator < (const Node &A, const Node &B){ return A.v == B.v ? A.c < B.c : A.v < B.v; } vector <int > v1[maxn];priority_queue<Node> q1; int n, c[maxn], k[maxn], ins[maxn], ans[maxn]; char o[maxn];signed main (void ) { int flag = 0 ; n = read (); for (int i = 1 ; i <= n * 2 ; i++) { char ch = getchar(); while (ch != '(' && ch != ')' ) ch = getchar(); o[i] = ch; } for (int i = 1 ; i <= n; i++) { k[i] = read (); c[k[i]]++; } for (int i = 1 ; i <= n; i++) { if (ins[k[i]]) continue ; ins[k[i]] = 1 ; q1.push((Node){k[i], c[k[i]]}); } int d = 1 , mxd = -1 , cnt = 0 ; for (int i = 1 ; i <= n * 2 ; i++) { if (o[i] == ')' ) d--; else v1[d].push_back(++cnt), d++; mxd = max (d, mxd); } for (int i = 1 ; i <= mxd; i++) { if (v1[i].size () > q1.size ()) { printf ("NO\n" ); return 0 ; } for (auto x : v1[i]) { Node mx = q1.top(); q1.pop(); ans[x] = mx.c; } for (auto x : v1[i]) { int nc = ans[x]; int nv = --c[nc]; if (nv) q1.push((Node){nc, nv}); } } printf ("YES\n" ); for (int i = 1 ; i <= cnt; i++) printf ("%lld " , ans[i]); return 0 ; }