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 numbered1, 2, 3 ... n*n, as shown below:
Prince stands in square 1, make pjumps and finally reach squaren*n. He enters a square at most once. Soif we usexp todenote thep-th square he enters, thenx1, x2, ... xp+1 are all different. Note thatx1 = 1 andxp+1 = n*n. Princess does the similar thing - stands insquare1, makeq jumps and finally reach square n*n. Weuse y1, y2 , ... yq+1 to denote the sequence, and allq+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 her3rd, 4th, 5th, 6th jump, she'll follow thesame route:1 --> 4 --> 8 --> 9, (The common route is shown infigure3) 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,q <= 250^2。
如果用普通的最长公共子序列,时间复杂度O(pq),无法承受。
但是假如我们使序列a有序,使序列b按照相同的法则一一映射,得到新的序列b',此时即求b'和{1,2,3,4,5……max{p,q}}的最长公共子序列,我们发现这和求最长上升子序列是等价问题。因此可以采用O(nlgn) (n = max(p,q))的二分法实现。
#include <cstdio>
#include <cstring>
int A[62510];
int B[62510];
int ans;
int s[62510];
int hash[62510];
int max(int a,int b)
{
if (a > b)
return a;
return b;
}
int min(int a,int b)
{
if (a < b)
return a;
return b;
}
int find(int nowh)
{
int rs = 0;
int l = 0;
int r = ans;
while (l <= r)
{
int mid = ((l+r)>>1);
if (s[mid] < nowh)
{
if (rs < mid)
rs = mid;
l = mid + 1;
}
else
r = mid - 1;
}
return rs;
}
int main()
{
freopen("pap.in","r",stdin);
freopen("pap.out","w",stdout);
int T;
int n,p,q;
scanf("%d",&T);
for (int tt=1;tt<=T;tt++)
{
memset(s,0x3f,sizeof s);
s[0] = 0;
memset(hash,0,sizeof hash);
ans = 0;
scanf("%d%d%d",&n,&p,&q);
for (int i=1;i<=p+1;i++)
scanf("%d",A+i);
for (int i=1;i<=q+1;i++)
scanf("%d",B+i);
for (int i=1;i<=p+1;i++)
{
hash[A[i]] = i;
}
for (int i=1;i<=q+1;i++)
{
B[i] = hash[B[i]];
}
/*
for (int i=1;i<=q+1;i++)
{
printf("B[%d] = %d\n",i,B[i]);
}
*/
for (int i=1;i<=q+1;i++)
{
int t = find(B[i]);
//printf("s[%d] = %d\n",t,s[t]);
s[t+1] = min(s[t+1],B[i]);
ans = max(ans,t+1);
}
printf("Case %d: %d\n",tt,ans);
}
return 0;
}