Problem D
Prince and Princess
Input: Standard Input
Output: Standard Output
Time Limit: 3 Seconds
Inan n x n chessboard, Prince and Princess plays a game. The squares inthe chessboard are numbered 1, 2, 3 ... n*n, as shown below:
Prince stands in square 1, make pjumps and finally reach square n*n. He enters a square at most once. Soif we use xp todenote the p-th square he enters, then x1, x2, ... xp+1 are all different. Note that x1 = 1 and xp+1 = n*n. Princess does the similar thing - stands insquare 1, make q jumps and finally reach square n*n. Weuse y1, y2 , ... yq+1 to denote the sequence, and all q+1numbers are different.
Figure 2 belows show a 3x3square, a possible route for Prince and a different route for Princess.
The Prince moves alongthe sequence: 1 --> 7 --> 5 --> 4 --> 8 --> 3 --> 9(Black arrows), while the Princess moves along this sequence: 1 --> 4--> 3 --> 5 --> 6 --> 2 --> 8 --> 9 (White arrow).
TheKing -- their father, has just come. "Why move separately? You are brotherand sister!" said the King, "Ignore some jumps and make sure thatyou're always together."
For example, if the Prince ignores his 2nd,3rd, 6th jump, he'll follow the route: 1 --> 4 --> 8 --> 9.If the Princess ignores her 3rd, 4th, 5th, 6th jump, she'll follow thesame route: 1 --> 4 --> 8 --> 9, (The common route is shown infigure 3) thus satisfies the King, shown above. The King wants to knowthe longest route they can move together, could you tell him?
Input
Output
Foreach test case, print the case number and the length of longest route. Look atthe output for sample input for details.
Sample Input Output for SampleInput
1 3 6 7 1 7 5 4 8 3 9 1 4 3 5 6 2 8 9 | Case 1: 4 |
Problemsetter: Rujia Liu, Member of Elite Problemsetters' Panel
Pictures drawn by Shahriar Manzoor, Member of Elite Problemsetters' Panel
题目大意:两个长度为p+1,q+1的序列,每个序列中元素各不相同且都是1-n^2之间的整数,两个序列的第一个元素均为1,求A和B的最长公共子序列长度。
因为序列元素各不相同,得到q中每个元素在p中出现的位置,得到一个序列,结果就是这个序列的LIS。
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cstring>
#include <map>
#include <cmath>
#include <string>
#include <queue>
#include <set>
using namespace std;
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#endif
#define S64I(a) scanf(iform, &a)
#define P64I(a) printf(oform, a)
const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const int maxn = 250 * 250 + 20;
int ord[maxn];
int qa[maxn];
int ans[maxn];
int g[maxn];
int main() {
int T;
scanf("%d", &T);
for(int kase=1; kase<=T; kase++) {
int n, p, q;
scanf("%d%d%d", &n, &p, &q);
memset(ord, -1, sizeof(ord));
for(int i=0; i<=p; i++) {
int ti;
scanf("%d", &ti);
ord[ti] = i;
}
for(int i=0; i<=q; i++) {
int ti;
scanf("%d", &ti);
qa[i] = ord[ti];
}
memset(g, 0x3f, sizeof(g));
int _a = 0;
for(int i=0; i<=q; i++) {
int k = lower_bound(g+1, g+q+2, qa[i]) - g;
ans[i] = k;
_a = max(ans[i], _a);
g[k] = qa[i];
}
printf("Case %d: %d\n", kase, _a);
}
return 0;
}