UVA 10635 - Prince and Princess


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 

The first line of the input contains a singleinteger t(1 <= t <= 10), the number of test cases followed. Foreach case, the first line contains three integers n, p, q(2 <= n <=250, 1 <= p, q < n*n). The second line contains p+1 differentintegers in the range [1..n*n], the sequence of the Prince. The thirdline contains q+1 different integers in the range [1..n*n], thesequence of the Princess.

 

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


"What was lost was found; what was found was never lost."


题目大意:两个长度为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;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值