Ultraviolet A 10635 Prince and Princess (LCS problems are converted to LIS problems O (nlogn ))

Source: Internet
Author: User

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)
     

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.