Slow Solution
Time Complexity:
See here.
Efficient Solution
In the solution, we increase the DP array only if , but since both arrays are permutations of length , for each , there must be only one matching element in .
Let us create an array where is the index of in (). Then, we can create another array, where stores .
Notice that every increasing subsequence in corresponds to a common subsequence between and . Increasing subsequence corresponds to the the common subsequence , which is equivalent to . Thus, the length of the longest common subsequence between and is the longest increasing subsequence of .
Implementation
Time Complexity:
from bisect import bisect_leftn = int(input())a = list(map(int, input().split()))b = list(map(int, input().split()))# pos is the inverse of apos = [0] * (n + 1)c = [0] * n
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!