【解题报告】
最长公共子序列转最长升。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1000005;
int n,m,ans,a[maxn],que[maxn];
int Abs(int x) {if (x<0) return (-x); return x;}
int Find(int x)
{
int L=1,R=n;
while (L<=R)
{
int mid=(R-L>>1)+L;
if (x>que[mid]) L=mid+1; else R=mid-1;
}
return R;
}
int main()
{
freopen("4990.in","r",stdin);
freopen("4990.out","w",stdout);
scanf("%d",&n); m=ans=0;
for (int i=1,x; i<=n; i++) scanf("%d",&x),a[x]=i;
memset(que,63,sizeof(que));
for (int i=1; i<=n; i++)
{
int x,c[10],cnt=0; scanf("%d",&x);
for (int k=-4; k<=4; k++)
if (x+k>=1&&x+k<=n) c[++cnt]=a[x+k];
sort(c+1,c+1+cnt);
for (int j=cnt; j; j--)
{
x=Find(c[j]);
que[x+1]=c[j];
if (x+1>ans) ans=x+1;
}
}
printf("%d",ans);
return 0;
}