#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read(){
int x = 0,f = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){
if(ch == '-')
f = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9'){
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
inline void write(int x)
{
char F[200];
int tmp = x > 0 ? x : -x;
if(x<0) putchar('-');
int cnt = 0;
while(tmp > 0){
F[cnt++] = tmp%10+'0';
tmp /= 10;
}
while(cnt > 0) putchar(F[--cnt]);
putchar('\n');
}
int n, a[1000005], b[1000005], c[1000005], ok[1000005], t[1000005];
int ans, k;
vector<int> d[1000005];
int main(){
// freopen("mex01.in", "r", stdin);
n = read();
for(int i = 1; i <= n; ++i){
a[i] = read(); c[i] = -1;
}
for(int i = 1; i <= n; ++i){
b[i] = read();
}
for(int i = 1; i <= n; ++i){
if(a[i] == b[i]) c[i] = a[i], d[a[i]].push_back(i);
}
int L = 0, R = 0, ans = 0;
for(int i = 0; i <= n; ++i){
if(!d[i].size()){
ans = i;
break;
}
}
k = n - ans;
for(int i = ans - 1; i >= 0; --i){
for(int j = 0; j < d[i].size() - 1; ++j){
k = max(k, d[i][j + 1] - d[i][j] - 1 - i);
}
}
printf("%d\n", k);
return 0;
}
思路是先求 1-n 的最大值,然后记录 ai=bi 的情况,将下标扔到一个类似于桶的 vector ti 里,后面就对于从大到小对 ti 中相邻两个下标计算长度,且默认 mex 为 i。
有几个 hack 过不去,但是我没搞懂这个思路错在哪里,所以请大佬证伪一下我的做法。感谢。