Prince uva10635 and Princess (convert LCS to LIS)

Source: Internet
Author: User

There are two sequences of length p+1 and q+1, each element in each sequence is different, and all are integers between 1~n*n, the first element of the two series is 1, and the length of the longest common subsequence of A and B is obtained.

The idea is very easy to think of LCS, but because O (PQ) algorithm will definitely time out, so you can not use, notice that the elements in A and B are different, it is possible to preprocess the elements in a, the trans array records a each element value corresponding position, and then process the elements in B, Convert each element to the position of the element in a, if it does not appear so zero in a, so that the LCS problem into the LIS

#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack& Gt  #include <string> #include <map> #include <set> #define EPS 1e-6 #define LL Long long using namespace std; const INT MAXN = $ * + 50;const int INF = 0x3f3f3f3f;int N, p, Q;int B[MAXN], TRANS[MAXN], G[MAXN], D[maxn];int Kas E = 0;void init () {scanf ("%d%d%d", &n, &p, &q), memset (trans, 0, sizeof (trans)); for (int i = 1; I <= p+1; i++ {int tmp; SCANF ("%d", &tmp); trans[tmp] = i;} for (int i = 0; I <= q; i++) {int tmp; SCANF ("%d", &tmp); B[i] = trans[tmp];}} void Solve () {int ans =-inf;for (int i = 1; I <= q+1; i++) G[i] = inf;for (int i = 0; I <= q; i++) {int k = Lower_bou nd (g+1, g+q+2, B[i])-g;d[i] = k;g[k] = B[i];} for (int i = 0; I <= q; i++) ans = max (ans, d[i]);p rintf ("Case%d:%d\n", ++kase, ans);} InchT Main () {freopen ("Input.txt", "R", stdin); int t; scanf ("%d", &t), while (t--) {init (); Solve ();} return 0;}

Prince uva10635 and Princess (convert LCS to LIS)

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.