Portal
LCS problem with no repeating elements
N2 practice did not say.
Nlogn Practices--
Because the LCS problem is the common subsequence, the order does not affect the answer, the answer is only two strings of the elements are the same, so you can exchange element position.
To simplify the problem first, assuming that P1 happens to be a monotonically increasing,... N, then obviously the answer is the length of the longest ascending subsequence of P2.
The problem is that P1 is not monotonically increasing, but we can assume that it is a one-way,..., N, mapping p1[1] to 1,p1[2] maps to 2 ... The P2 is then transformed in the same way, which requires only the longest ascending subsequence of the P2.
--code
1#include <cstdio>2#include <algorithm>3#include <cstring>4 5 using namespacestd;6 7 Const intMAXN =100001;8 intN, ans;9 intA[MAXN], B[MAXN], C[MAXN];Ten OneInlineintQueryintx) A { - intAns =0; - for(; x; x-= x & x) ans =Max (ans, c[x]); the returnans; - } - -InlinevoidUpdateintXintd) + { - for(; x <= n; x + = x & x) c[x] =Max (c[x], d); + } A at intMain () - { - intI, J, X, y; -scanf"%d", &n); - for(i =1; I <= N; i++) scanf ("%d", &x), a[x] =i; - for(i =1; I <= N; i++) scanf ("%d", &x), b[i] =A[x]; in for(i =1; I <= N; i++) - { toy = query (B[i]-1) +1; + Update (b[i], y); -Ans =Max (ans, y); the } *printf"%d", ans); $ return 0;Panax Notoginseng}
View Code
[luoguP1439] Arranging LCS problems (DP + tree Array)