uva 10635 - Prince and Princess(线段树)

Problem D
Prince and Princess
Input: 
Standard Input

Output: Standard Output

Time Limit: 3 Seconds

 


In an n x n chessboard, Prince and Princess plays a game. The squares in the chessboard are numbered 1, 2, 3 ... n*n, as shown below:

uva 10635 - Prince and Princess(线段树)

Prince stands in square 1, make p jumps and finally reach square n*n. He enters a square at most once. So if we use xp to denote 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 in square 1, make q jumps and finally reach square n*n. We use y1, y, ... yq+1 to denote the sequence, and all q+1 numbers are different.

 

Figure 2 belows show a 3x3 square, a possible route for Prince and a different route for Princess.

uva 10635 - Prince and Princess(线段树)

The Prince moves along the 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).

The King -- their father, has just come. "Why move separately? You are brother and sister!" said the King, "Ignore some jumps and make sure that you‘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 the same route: 1 --> 4 --> 8 --> 9, (The common route is shown in figure 3) thus satisfies the King, shown above. The King wants to know the longest route they can move together, could you tell him?

 

Input 

The first line of the input contains a single integer t(1 <= t <= 10), the number of test cases followed. For each case, the first line contains three integers n, p, q(2 <= n <= 250, 1 <= p, q < n*n). The second line contains p+1 different integers in the range [1..n*n], the sequence of the Prince. The third line contains q+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 Input                           Output 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


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."


题意:
给你你两个序列。叫你找出他们的最长公共子序列。序列最长为62500.n^2的算法肯定要超时。但这题有一些特点。那就是每个序列.1~n^2的每个数字最多出现一次。根据这个特点可以把时间复杂度降到O(n*log2(n))。
思路:
先线性扫描第一个序列。记录每个数字出现的位置pos[i]。然后扫描第二个序列。如过第二个序列中出现过且第一个序列中也出现过。由于每个数字顶多出现一次所以对于每个位置与之配对的位置也是确定的。如果序列1的i位置和序列2的j位置配对。则他们配对产生的最长公共子序列为。序列1的[1,i-1]的位置与序列2的[1,j-1]位置最长公共子序列长度+1。而这个长度正好可以用线段树动态维护。这样时间复杂度就下来了。
详细见代码:
#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
//#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
const double PI=acos(-1.0);
const int maxn=63000;
typedef __int64 ll;
int val[maxn<<2],pos[maxn];
int pc[maxn],pcs[maxn];
void btree(int L,int R,int k)
{
    int ls,rs,mid;
    val[k]=0;
    if(L==R)
        return ;
    ls=k<<1;
    rs=ls|1;
    mid=(L+R)>>1;
    btree(L,mid,ls);
    btree(mid+1,R,rs);
}
void update(int L,int R,int p,int k,int v)
{
    int ls,rs,mid;
    if(L==R)
    {
        val[k]=v;
        return ;
    }
    ls=k<<1;
    rs=ls|1;
    mid=(L+R)>>1;
    if(p>mid)
        update(mid+1,R,p,rs,v);
    else
        update(L,mid,p,ls,v);
    val[k]=max(val[ls],val[rs]);
}
int qu(int L,int R,int l,int r,int k)
{
    int ls,rs,mid;
    if(l==L&&r==R)
        return val[k];
    ls=k<<1;
    rs=ls|1;
    mid=(L+R)>>1;
    if(l>mid)
        return qu(mid+1,R,l,r,rs);
    else if(r<=mid)
        return qu(L,mid,l,r,ls);
    else
        return max(qu(L,mid,l,mid,ls),qu(mid+1,R,mid+1,r,rs));
}
int main()
{
    int cas=1,t,n,nc,ns,i,tp,ans;

    scanf("%d",&t);
    while(t--)
    {

        scanf("%d%d%d",&n,&nc,&ns);
        nc++,ns++;
        memset(pos,-1,sizeof pos);
        btree(1,nc,1);
        ans=-1;
        for(i=0;i<nc;i++)
        {
            scanf("%d",&pc[i]);
            pos[pc[i]]=i+1;//记录序列1每个数字的位置
        }
        for(i=0;i<ns;i++)
            scanf("%d",&pcs[i]);
        for(i=0;i<ns;i++)
        {
            if(pos[pcs[i]]==-1)
                continue;
            if(pos[pcs[i]]==1)
                tp=1;
            else
                tp=qu(1,nc,1,pos[pcs[i]]-1,1)+1;//关键部分
            update(1,nc,pos[pcs[i]],1,tp);
            ans=max(tp,ans);
        }
        printf("Case %d: %d\n",cas++,ans);
    }
    return 0;
}





uva 10635 - Prince and Princess(线段树)

上一篇:前端代码优化: 使用YUI Compressor


下一篇:JS 两数之和 (力扣++)