UVA 10635 Prince and Princess (DP)
In an n x n chessboard, Prince and Princess plays a game. The squares in the chessboard is numbered1, 2, 3 ... n*n, as shown below:
Prince stands in square 1, make p jumps and finally reach Squaren*n. He enters a square at the most once. So if we use xp to denote thep-th Square He enters, then x1,x 2, ... xp+1 is all different. Note thatx1 = 1 and xp+1 = N*n. Princess does the similar thing-stands in square1, make Q jumps and finally reach Square n*n< /c21>. We use y1, y2, ... yq+1 to denote the sequence, and all q+1 numbers is different.
Figure 2 belows show a 3x3 Square, a possible route for Prince and a different route for Princess.
The Prince moves along the sequence: 1 --7--and 5--and 4---8---3 (Black arrows), whil E The princess moves along this sequence:1 --4--and 3--and 5---6--and 2--and 8 (white Arrow).
The King--their father, has just come. "Why move separately?" You're brother and sister! "said the King," Ignore some jumps and make sure this you ' re always together. "
For example, if the Prince ignores him 2nd, 3rd,6th Jump, he ' ll follow the route: 1--4--8--9. If the princess ignores her3rd, 4th, 5th, 6th Jump, she ' ll follow the same RO Ute:1--and 4--and 8--9, (the common route is shown and Figure 3) Thus satisfies the King, s Hown above. The King wants to know the longest route they can move together, could your tell him?
Input
The first line of the input contains a single integert (1 <= t <=), the number of test cases followed. For each case, the first line contains three integersn, p, q (2 <= n <=, 1 <= p, q < n*n). The second line containsp+1 different integers in the range [1..n*n], the sequence of the Prince. The third line containsq+1 different integers in the range [1..n*n], the sequence of the princess.
Output
For each test case, print the case number and the length of longest route. Look at the output for sample input for details.
Sample InputOutput for Sample Input
|
1
3 6 7
1 7 5 4 8 3 9
1 4 3 5 6 2 8 9 |
Case 1:4
|
Title: Two number sequence, the longest common sub-sequence of two sequences is obtained. problem-solving ideas: Because of the large amount of data, so to use N*logn method, generate b corresponding to a mapping table, and then in the mapping table to find the longest ascending subsequence, is two series of the longest common sub-sequence.
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #define N 250 * 5 using namespace Std;const int INF = 100000000;int Num[n], s[n], G[n], D[n];int main () {int T, case = 1;scanf ("%d", &T); while (t--) {int n, p, q;scanf ("%d%d%d", &n, &p, &q), memset (num, 0, sizeof (num)), int A, CNT = 0, an s = 0;for (int i = 1; I <= p + 1; i++) {scanf ("%d", &a); num[a] = i;} for (int i = 0; i < q + 1; i++) {scanf ("%d", &a), if (Num[a]) {s[cnt++] = Num[a];//generate B for a mapping table}}for (int i = 1; I &l T;= CNT; i++) G[i] = inf;/* finds the longest ascending subsequence in B, which is the longest common sub-sequence of A and b */for (int i = 0; i < cnt; i++) {int temp = Lower_bound (g + 1, G + CNT + 1, S[i])-g;/* Find the first larger number in G than s[i], replace it */d[i] = temp;g[temp] = S[i];ans = max (ans, d[i]);} printf ("Case%d:%d\n", case++, ans);} return 0;}
UVa 10635 Prince and Princess (DP)