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;
    }
    

      

  • 相关阅读:
    音律入门
    [转]MIDI常识20条
    Java使用代理服务器
    java8日期时间
    误删课表系统
    Uncaught Error: Bootstrap tooltips require Tether (http://github.hubspot.com/tether/)
    SpringBoot应用部署[转]
    如何学习新技术
    Maven使用archetype迅速生成项目骨架
    两个月打工总结
  • 原文地址:https://www.cnblogs.com/Achenchen/p/7799448.html
Copyright © 2011-2022 走看看