[8] template longest common subsequence

 

 

 

 

 

 

#include<iostream>
#include<cstdio>
using namespace std;
int stark[100001],a[100001],map[100001],maxn,maxnn=0,x,n;
int l,r,top,p;
void fins(int y)
{
    l=1;
    r=top;
    while(l!=r)
    {
        int xx=(l+r)/2;
        if(stark[xx]>=y)
        {        
            r=xx;
        }
        if(stark[xx]<y)
        {
            l=xx+1;
        }
    }
    if(l==r)
    {
        stark[r]=y;
        return;
    }
}
void lis(int y)
{
    top=1;
    stark[top]=map[a[y]];
    for(int k=y+1;k<=n;k++)
    {
        if(map[a[k]]>stark[top])
        {
            stark[++top]=map[a[k]];
        }
        else 
        {
            fins(map[a[k]]);
        }    
    }
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        map[x]=i;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    lis(1);
    cout<<top;
}

                    2019-09-09 By the fourteenth day of summer

Guess you like

Origin www.cnblogs.com/north-star/p/11489445.html