选点
选点
https://ac.nowcoder.com/acm/problem/22494
选点
如果选了根节点的话,在这棵子树内选的其他的点都要比根节点的值大,
如果在左子树选了一个点,在右子树中选的其他点要比它小,
根节点比子节点都要小,右子树比左子树都要大
所以可以先遍历右儿子,然后用先序遍历dfs序维护最长上升子序列来写。
/*
Author : lifehappy
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int value[N], son[N][2], a[N], b[N], tot, n;
void dfs(int rt) {
a[++tot] = value[rt];
if(son[rt][1]) dfs(son[rt][1]);
if(son[rt][0]) dfs(son[rt][0]);
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &value[i]);
}
for(int i = 1; i <= n; i++) {
scanf("%d %d", &son[i][0], &son[i][1]);
}
dfs(1);
int ans = 0;
b[++ans] = a[1];
for (int i = 2; i <= n; ++i) {
if (a[i] > b[ans]) b[++ans] = a[i];
else b[lower_bound(b + 1, b + ans + 1, a[i]) - b] = a[i];
}
printf("%d\n", ans);
return 0;
} 
传音控股公司福利 306人发布