Title Link: Codeforces 490F treeland Tour
An ascending ascending sub-sequence algorithm similar to Nlogn.
#include <cstdio> #include <cstring> #include <vector> #include <algorithm>using namespace std; const int MAXN = 6005;const int inf = 0x3f3f3f3f;int N, R[MAXN], d[maxn], ans = 0;vector<int> g[maxn];void init () { scanf ("%d", &n), for (int i = 1; I <= n; i++) scanf ("%d", &r[i]), for (int i = 0; i < N; i++) D[i] = Inf;int A, b;for (int i = 1; i < N; i++) {scanf ("%d%d", &a, &b); G[a].push_back (b); G[b].push_back (a);}} void Dfs (int u, int f) {int idx = Lower_bound (d, D + N, R[u])-D;ans = max (ans, idx + 1); int tmp = D[IDX];D [idx] = R[u];f or (int i = 0; i < g[u].size (); i++) {if (g[u][i] = = f) Continue;dfs (G[u][i], u);} D[IDX] = tmp;} int main () {init (); for (int i = 1; I <= N; i++) Dfs (i, 0);p rintf ("%d\n", ans); return 0;}
Codeforces 490F Treeland Tour (DP)