Wednesday, October 31, 2012

LCS With LIS Trick

#include <iostream>
#include <cstdio>
#include <climits>
#include <algorithm>
#include <map>
using namespace std;
#define siz 1000005
#define INF INT_MAX

int I[siz],a[siz];
int n;
map <int,int>mp;

int LIS(){
    int i;
    I[0]=-INF;
    for(i=1;i<=n;i++)
        I[i]=INF;

    int LisLength=0;

    for(i=0;i<n;i++){
        int low,high,mid;
        low=0;
        high=LisLength;

        while(low<=high){
            mid=(low+high)/2;
            if(I[mid]<a[i]) low=mid+1;
            else high=mid-1;
        }

        I[low]=a[i];
        if(LisLength<low) LisLength=low;
    }

    return LisLength;
}

int main()
{
    int N,p,q,m,x,k=1;
    //freopen("input.txt","r",stdin);
    scanf("%d",&N);
    for(int t=0;t<N;t++){
        scanf("%d%d%d",&m,&p,&q);
        if(p>q) swap(p,q);

        for(int i=1;i<=p+1;i++){
            scanf("%d",&x);
            mp[x]=i;
        }

        for(int i=0;i<=q;i++){
            scanf("%d",&x);
            a[i]=mp[x];
        }

        n=q+1;
        printf("Case %d: %d\n",k++,LIS());
        mp.clear();
    }
    return 0;
}

No comments:

Post a Comment