AcWing 5063. 关键节点
原题链接
简单
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 10;
int t, n, x;
int f[N], idx;
char ch;
int dfs(int r, int x) {
int ans = (f[r] >= x);
if (f[r * 2])ans+=dfs(r * 2, max(f[r * 2],x));
if (f[r * 2 + 1])ans+=dfs(r * 2 + 1, max(f[r * 2 + 1],x));
return ans;
}
void solve() {
while (cin >> ch) {
if (ch == ',') {
f[++idx] = x;
x = 0;
} else {
if (ch == 'n') {
cin >> ch >> ch >> ch;
x = 0;
} else {
x = x * 10 + ch - '0';
}
}
}
f[++idx] = x;
cout << dfs(1, f[1]) << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}