Skip to content

Commit f9c76c7

Browse files
committed
add top 1035
1 parent cd68752 commit f9c76c7

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
enum C { RED, BLACK };
5+
// (color, black height)
6+
using P = pair<C, int>;
7+
8+
struct Node
9+
{
10+
int v;
11+
Node *l = nullptr;
12+
Node *r = nullptr;
13+
};
14+
15+
16+
Node *build(const vector<int> &post, int l, int r) {
17+
if (l > r) return nullptr;
18+
int rootVal = post[r], p = l;
19+
while (p < r && post[p] < rootVal) ++p;
20+
Node *n = new Node{rootVal};
21+
n->l = build(post, l, p - 1);
22+
n->r = build(post, p, r - 1);
23+
return n;
24+
}
25+
26+
vector<P> dfs(Node* n){
27+
if (!n) return {{BLACK, 1}};
28+
vector<P> L = dfs(n->l), R = dfs(n->r);
29+
// stateCode: color<<8 | blackHeight
30+
unordered_set<int> s;
31+
for (auto [c1, b1] : L)
32+
for (auto [c2, b2] : R)
33+
if (b1 == b2) {
34+
// parent can set black
35+
s.insert((BLACK<<8)|(b1+1));
36+
// parent can set red
37+
if(c1==BLACK && c2==BLACK) s.insert((RED<<8)|b1);
38+
}
39+
vector<P> res;
40+
for (int x : s) res.push_back({C(x >> 8), x & 255});
41+
return res;
42+
}
43+
44+
int main(){
45+
ios::sync_with_stdio(false);
46+
int K;
47+
cin >> K;
48+
while (K--) {
49+
int N;
50+
cin >> N;
51+
vector<int> post(N);
52+
for (int &x : post) cin >> x;
53+
Node *root = build(post, 0, N - 1);
54+
auto st = dfs(root);
55+
bool ok = any_of(st.begin(), st.end(), [&](const P &p) { return p.first == BLACK; });
56+
cout << (ok ? "Yes" : "No") << endl;
57+
}
58+
59+
return 0;
60+
}

0 commit comments

Comments
 (0)