P1439 【模板】最长公共子序列
开始打模板然后发现洛谷出现了一个新模板?
最长上升子序列。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | //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; } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步