Uva10635prince and Princess (LIS)

Source: Internet
Author: User

Question: uva10635prince and Princess (LIS)


The maximum length of a common subsequence is the same.


Solution: because of the large data, 62500 cannot use the previous method of LIS. You can sort the Integers of the first route in a new order (0... p), and then the next route maps its original number to the new number of the first route because it wants to find the same oldest sequence. In this way, you only need to find the lis of the second route.

The LIS algorithm of nlog (n:

When searching, you need to find a number that is smaller than V [I.

The LIS array is used to store the longest Lis of the first I number (top starts from 0), so that the number can be sorted by binary search (log (n )). If V [I]> Lis [Top], you can add V [I] to form top + 1 Lis. Therefore, Lis [++ top] = V [I], DP [I] = Top + 1;

If it is equal to, DP [I] = Top + 1; if it is smaller, find a number in LIS That is closer to V [I] than or equal to V [I] (k Number), indicating that the previous K number is smaller than V [I. Then DP [I] = k + 1; but here the LIS [k] should be updated to V [I ]. Why? Since you want to get k + 1 Length incremental subsequence, the previous K are all the same, so for k + 1, the smaller the better, in this way, the subsequent numbers are more likely to be connected to the subsequence to form a longer subsequence.


Code:

# Include <cstdio> # include <cstring> const int n = 62505; int DP [N]; int V [N]; int vis [N]; int Lis [N]; int P1; int max (const int A, const int B) {return A> B? A: B;} int bsearch (INT s) {int L = 0; int r = p1; int mid; while (L <R) {mid = L + (R-l)/2; If (S = Lis [Mid]) return mid; else if (S> Lis [Mid]) L = Mid + 1; elser = mid;} return l;} int main () {int N, P, Q, T; scanf ("% d", & T ); for (int cas = 1; CAS <= T; CAS ++) {scanf ("% d", & N, & P, & Q ); memset (VIS,-1, sizeof (VIS); For (INT I = 0; I <= P; I ++) {scanf ("% d ", & V [I]); vis [V [I] = I;} p1 =-1; int K; int ans = 0; For (INT I = 0; I <= Q; I ++) {scanf ("% d", & V [I]); V [I] = vis [V [I]; if (V [I] =-1) continue; If (p1 =-1) {DP [I] = 1; lis [++ P1] = V [I];} else {If (V [I]> Lis [P1]) {Lis [++ P1] = V [I]; DP [I] = p1 + 1;} else if (V [I] <Lis [P1]) {k = bsearch (V [I]); DP [I] = k + 1; Lis [k] = V [I];} elsedp [I] = p1 + 1;} ans = max (ANS, DP [I]);} printf ("case % d: % d \ n", Cas, ANS);} return 0 ;}

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.