Uva10635 prince and princess (converting lcs to lis), uva10635lcs

Source: Internet
Author: User

Uva10635 prince and princess (converting lcs to lis), uva10635lcs

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 * n. The first element of the two sequences is 1, and the length of the longest common subsequence of a and B is obtained.

The idea is easy to think of lcs, but the O (pq) algorithm will definitely time out, so it cannot be used. Note that the elements in a and B are different, so we can pre-process the elements in, record the location corresponding to each element value of a in the trans array, process the elements in B, and convert each element into the position of the element in a. If it does not appear in a, it is zero, in this way, the lcs problem is transformed into lis.

#include<cstdio>  #include<cstring>  #include<cmath>  #include<cstdlib>  #include<iostream>  #include<algorithm>  #include<vector>  #include<map>  #include<queue>  #include<stack> #include<string>#include<map> #include<set>#define eps 1e-6 #define LL long long  using namespace std;  const int maxn = 250 * 250 + 50;const int INF = 0x3f3f3f3f;int n, p, q;int B[maxn], trans[maxn], g[maxn], d[maxn];int kase = 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_bound(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]);printf("Case %d: %d\n", ++kase, ans);}int main() {freopen("input.txt", "r", stdin);int t; scanf("%d", &t);while(t--) {init();solve();}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.