Solution: First sort one of the attributes in ascending order, then make the longest ascending subsequence for another attribute, and finally DFS outputs the sequence. The time complexity should be O(N^2)
For the typical deformation of the longest ascending subsequence (LIS), I' m considering if we can change the time complexity from n ^ 2 to nlogn algorithm.
Definition d [k]: the last element of the ascending subsequence with a length of K. if there are multiple ascending subsequences with a length of K, record the smallest one.
Note that the elements in D are monotonically increasing. We will use this property next.
First, len = 1, d [1] = a [1], then for a [i]: if a [i] > d [len], then len + +, d [len] = a [i];
Otherwise, we need to find a j from D [1] to d [len-1], which satisfies the requirement of D [j-1] < a [i] < d [j]. According to the definition of D, we need to update the last element of the ascending subsequence with a length of J, that is, d [j] = a [i];
The final answer is len
Using the monotonicity of D, we can find j in two parts, so the time complexity is nlogn.
For example,
First, put d [1] in B in order, and make B [1] = 2, that is to say, when there is only one digit 2, the minimum end of LIS with length 1 is 2. At this time, len = 1
Then, put d [2] in B in order, make B [1] = 1, that is to say, the minimum end of LIS with length of 1 is 1, d [1] = 2 is useless, it's easy to understand. At this time, len = 1
Then, d [3] = 5, d [3] > b [1], so let B [1 + 1] = B [2] = D [3] = 5, that is to say, the minimum end of LIS with length of 2 is 5, which is easy to understand. In this case, B [1.. 2] = 1, 5, len = 2
Next, d [4] = 3, which is just added between 1 and 5, so it is obviously not appropriate to put it in the position of 1, because 1 is less than 3, and the minimum end of LIS with length of 1 should be 1, so it is easy to deduce that the minimum end of LIS with length of 2 is 3, so 5 can be eliminated. At this time, B [1.. 2] = 1, 3, len = 2
Continue, d [5] = 6, it's behind 3, because B [2] = 3, and 6 is behind 3, so it's easy to deduce that B [3] = 6, and then B [1.. 3] = 1, 3, 6, or it's easy to understand? Len = 3.
The sixth one, d [6] = 4, you see it's between 3 and 6, so we can replace 6 and get B [3] = 4. B [1.. 3] = 1, 3, 4, len continues to equal 3
The seventh one, d [7] = 8, it's big, bigger than 4, HMM. So B [4] = 8. Len becomes 4
The eighth one, d [8] = 9, gets B [5] = 9, HMM. Len continues to grow to 5.
The last one, d [9] = 7, is between B [3] = 4 and B [4] = 8, so we know that the latest B [4] = 7, B [1.. 5] = 1, 3, 4, 7, 9, len = 5.
So we know that the length of LIS is 5.
This 1,3,4,7,9 is not lis, it is just the minimum end of the corresponding length LIS stored. With this end, we can insert data one by one. Although the last d [9] = 7 update has no meaning for this group of data, if there are two more numbers 8 and 9, then you can update 8 to d [5], 9 to d [6], and get the length of LIS as 6.
Then it should be found that inserting data into B is orderly and can be replaced without moving - that is to say, we can use binary search to optimize the insertion time of each number to o (logn) ~ ~ ~ ~ so the time complexity of the algorithm is reduced to o (nlogn) ~!