There are two sequences with the length of p + 1 and q + 1, each of which has different elements and is 1 ~ An integer between n ^ 2. The first element of the two sequences is 1. The longest length of A and B is obtained.
Analysis: This question is about LCS, but p * q <= 62500, the O (pq) algorithm will obviously LE. Here is A condition where each element in each sequence is different from each other. Therefore, you can rename the elements in A as 1 ~ P + 1. For example, in the example, A = {, 5, 4, 8, 3, 9}, B = {, 5, 6, 5, 6, 7}, then B is {, 6, 3, 5, 7} (the elements that have not appeared in A must not be elements in the common subsequence ), 0 indicates that A does not appear. You can delete it directly. At this time, B = {,} indicates the position in A where B is the same as the element value in original. The position of the subsequence must be monotonically incrementing. In this way, the obtained oldest sequence is equivalent to the longest common subsequence of the original A and B. As a result, it is successfully converted to LIS question '(* ← _ ← *)′. Find the LIS of B, and the time complexity can be optimized to O (nlogn.
The following code is attached (for reference, =-= Of The lrj giant example)
# Include
# Include
# Define deusing namespace std; const int maxn = 250*250; const int INF = 1e9; int s [maxn], g [maxn], d [maxn]; int num [maxn]; // num [x] is the new number of integer x. num [x] = 0 indicates that x has not encountered int main () in () {int T; cin> T; for (int kase = 1; kase <= T; kase ++) {int N, p, q, x; cin> N> p> q; memset (num, 0, sizeof (num); for (int I = 1; I <= p + 1; I ++) {cin> x; num [x] = I;} int n = 0; for (int I = 0; I
> X; if (num [x]) s [n ++] = num [x];} // solve s [0]... s [n-1] LIS for (int I = 1; I <= n; I ++) g [I] = INF; int ans = 0; for (int I = 0; I
About lower_bound function (Binary Search function), is the STL library, do not understand the shoes please see http://blog.csdn.net/u012198382/article/details/24887181 (lower_bound usage)