P1439 【模板】最长公共子序列

开始打模板然后发现洛谷出现了一个新模板?

最长上升子序列。

//Twenty
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<ctime>
const int maxn=100005;
using namespace std;
int n,dp[maxn],a[maxn],b[maxn],p[maxn],que[maxn],sz;

template<typename T> void read(T &x) {
    char ch=getchar(); T f=1; x=0;
    while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    if(ch=='-') f=-1,ch=getchar();
    for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}

int ef(int x) {
    int res=1,l=1,r=sz;
    while(l<=r) {
        int mid=(l+r)>>1;
        if(p[b[que[mid]]]<x) res=mid+1,l=mid+1;
        else r=mid-1;
    } 
    return res;
} 
 
void work() {
    for(int i=1;i<=n;i++) {
        int tp=ef(p[b[i]]);
        dp[i]=dp[que[tp-1]]+1;
        if(tp>sz) {que[++sz]=i;}
        while(tp>=1&&p[b[i]]<p[b[que[tp]]]) {
            que[tp]=i;
            tp--;
        }
    }
    printf("%d\n",sz);
}

void init() {
    read(n);
    for(int i=1;i<=n;i++) {
        read(a[i]);
        p[a[i]]=i;
    }
    for(int i=1;i<=n;i++) read(b[i]);
}

int main() {
#ifdef DEBUG
    freopen(".in","r",stdin);
    freopen(".out","w",stdout);
#endif
    init();
    work();
    return 0;
}

  

posted @ 2017-11-07 16:06  啊宸  阅读(235)  评论(0编辑  收藏  举报