求最长公共子序列->映射每一个数->求最长上升组序列->线性dp,二分
#include<iostream>
#include<algorithm>
#include<numeric>//accumulate(be,en,0)
#include<cstring>//rfind("string"),s.find(string,begin)!=s.npos,find_first _of(),find_last_of()
#include<string>//to_string(value)
#include<cstdio>
#include<cmath>
#include<vector>//res.erase(unique(res.begin(), res.end()), res.end()),reverse(q.begin(),q.end());
#include<queue>//priority_queue(big) /priority_queue<int, vector<int>, greater<int>> q(small)
#include<stack>
#include<map>//unordered_map
#include<set>//iterator,insert(),erase(),lower(>=)/upper_bound(>)(value)/find()return end()
#define ll long long
#define PII pair<int,int>
#define f first
#define s second
using namespace std;
const int inf = 0x3f3f3f3f, N = 1e6 + 5;
int a[N], b[N], qq[N];
int q[N];
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0);
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++) {
q[b[i]] = i;
}
for (int i = 1; i <= n; i++) {
a[i] = q[a[i]];
}
memset(qq, 0x3f, sizeof qq);
qq[0] = -inf;
int maxx = 0;
for (int i = 1; i <= n; i++) {
int pos = lower_bound(qq, qq + n, a[i]) - qq - 1;
qq[pos + 1] = a[i];
maxx = max(maxx, pos + 1);
}
cout << n - maxx;
}