zoukankan      html  css  js  c++  java
  • 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
    ",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;
    }
    

      

  • 相关阅读:
    基于spec评论作品
    Alpha版发布
    软件工程第七次作业
    软件工程第六次作业
    软件工程第五次作业
    django-rest-framework笔记-序列化篇
    django restframework系列笔记
    rpyc 文件监控
    python subprocess select 读取
    Python 多线程 类和方法
  • 原文地址:https://www.cnblogs.com/Achenchen/p/7799448.html
Copyright © 2011-2022 走看看