题解:P10954 LCIS
Solution P10954
Idea
我们设 为 序列考虑了前 个数,以 结尾的最长 LCIS 长度。注意并不一定以 结尾。
不设 为 序列以 和 结尾的最长 LCIS 长度(要求必须以 结尾)的原因是不保证 ;不设 为 序列考虑了前 个数, 序列考虑了前 个数的最长 LCIS 长度的原因是极难转移。
显然有:
- 当 时,;
- 当 时,,其中 。
这样的复杂度是 的,可以通过 CF10D。
然后不难发现 是一段前缀,可以直接预处理优化。
这样就省去了枚举 的步骤,复杂度减为 。
Code
#include<bits/stdc++.h>
using namespace std;
const int N=3005;
int n;
long long a[N],b[N],dp[N][N],ans,maxx;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
for(int i=1;i<=n;i++)scanf("%lld",&b[i]);
for(int i=1;i<=n;i++){
maxx=0;
for(int j=1;j<=n;j++){
if(a[i]!=b[j])dp[i][j]=dp[i-1][j];
else dp[i][j]=maxx+1;
if(a[i]>b[j])maxx=max(maxx,dp[i-1][j]);//先计算后处理,因为 k<j
ans=max(ans,dp[i][j]);
}
}
printf("%lld",ans);
}

浙公网安备 33010602011771号