用DP直接求LCS明显超时,这题和平时的区别在与给定了数,所以从这入手可以直接记录各数字的位置
然后依次填入dp[i]=max{dp[j]}+1(1<=j<=i)用BIT维护最大值O(nlogn)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //#include<bits/stdc++.h> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 #include<iostream> 7 #define inc(i,l,r) for(int i=l;i<=r;i++) 8 #define dec(i,l,r) for(int i=l;i>=r;i--) 9 #define link(x) for(edge *j=h[x];j;j=j->next) 10 #define mem(a) memset(a,0,sizeof(a)) 11 #define ll long long 12 #define succ(x) (1<<x) 13 #define lowbit(x) (x&(-x)) 14 #define NM 20000+5 15 using namespace std; 16 int read(){ 17 int x=0,f=1;char ch=getchar(); 18 while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} 19 while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); 20 return x*f; 21 } 22 int n,c[5*NM],_x,b[NM][6],ans; 23 void upd(int x,int t){ 24 for(;x<=5*n;x+=lowbit(x))c[x]=max(c[x],t); 25 } 26 int sum(int x){ 27 int s=0; 28 for(;x;x-=lowbit(x))s=max(s,c[x]); 29 return s; 30 } 31 int main(){ 32 freopen("data.in","r",stdin); 33 n=read(); 34 inc(i,1,5*n){ 35 _x=read();b[_x][++b[_x][0]]=i; 36 } 37 inc(i,1,5*n){ 38 _x=read(); 39 dec(j,5,1){ 40 int t=b[_x][j]; 41 upd(t,sum(t-1)+1); 42 } 43 // inc(i,1,5*n)printf("%d ",c[i]);printf(" "); 44 } 45 printf("%d ",sum(5*n)); 46 return 0; 47 }