You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
948 B

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 3010;
int a[N], b[N];
int f[N][N];
int res;
// O(n^2)
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 1; i <= n; i++) {
1 year ago
int mx = 1; // 将mx提到二层循环的外面因为O(n^3)之所以慢就是因为存在重复计算每次都需要从头找一遍可以接在后面的同时上升序列最长的那个k
// 由于循环从左到右进行时每次这个mx是可以记录下来下次不用重复计算的所以可以复用
2 years ago
for (int j = 1; j <= n; j++) {
f[i][j] = f[i - 1][j];
1 year ago
if (a[i] == b[j]) f[i][j] = mx;
if (a[i] > b[j]) mx = max(mx, f[i - 1][j] + 1);
2 years ago
}
}
for (int i = 1; i <= n; i++) res = max(res, f[n][i]);
printf("%d\n", res);
return 0;
}