You are given two sequences of numbers named $$A$$ and $$B$$. Your task is to find the longest common increasing subsequence.
In other words, let us assume that $$A$$ has the length of $$n$$ and can be shown as $$A_1, A_2, ..., A_n$$. Also, $$B$$ has the length of $$m$$ and can be shown as $$B_1, B_2, ..., B_m$$.
You are required to find a number $$l$$ and two sequences of indices named $$C$$ and $$D$$ of length $$l$$ such that the following conditions hold:
- $$1 \le C_1 < C_2 < ... < C_l \le n$$
- $$1 \le D_1 < D_2 < ... < D_l \le m$$
- For each $$1 \le i \le l$$, you are given $$A_{C_{i}} = B_{D_{i}}$$ (common subsequence condition)
- $$A_{C_1} \le A_{C_2} \le ... \le A_{C_l}$$ (increasing subsequence condition)
- $$B_{D_1} \le B_{D_2} \le ... \le B_{D_l}$$ (increasing subsequence condition)
Input format
- The first line contains two integers $$n$$ and $$m$$ respectively where $$n$$ is the length of sequence $$A$$ and $$m$$ is the length for $$B$$.
- The second line contains $$n$$ space-separated integers that represent the elements of the sequence $$A$$.
- The third line contains $$m$$ space-separated integers that represent the elements of the sequence $$B$$.
Output format
The output contains three lines:
In the first line, print an integer $$l$$ that denotes the length of the longest common increasing subsequence.
In the second line, print $$l$$ integers that represent elements of $$C$$ (chosen indices of subsequence from $$A$$).
In the third line, print $$l$$ integers that represent elements of $$D$$ (chosen indices of subsequence from $$B$$).
Note
- The elements of $$C$$ and $$D$$ must be in increasing order.
- The answer to the problem could be $$0$$, in this case, you just need to print $$0$$ in a single line.
Constraints
$$1 \le n, m \le 10^{5}$$
$$1 \le A_i, B_i \le 10^{9}$$
4 5 1 2 1 4 1 4 1 1 4
2 1 4 1 2
As you can see, $$A_1 = B_1$$ and $$A_4 = B_2$$, $$A_1 \le A_4$$ and $$B_1 \le B_2$$. So this is a common increasing subsequence. However, this is not the best possible.
Consider the following indices from $$A$$ and $$B$$:
- C = {1, 3, 4}
- D = {1, 4, 5}
Since following condition hold:
- $$A_1 = B_1$$, $$A_3 = B_4$$ and $$A_4 = B_5$$
- $$A_1 \le A_3 \le A_4$$
- $$B_1 \le B_4 \le B_5$$
this is also a common increasing subsequence with length 3.
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor