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;}