要点
- 没想到的一点是,对于堆里的某牌,最好情况是它出来时后边都准备就绪了,答案是(p[i] + (n - i + 1)),所有的这个取最大的即可
- 一发结束的情况先特判一下
const int maxn = 2e5 + 5;
int n, pos, ans;
int a[maxn], b[maxn], In[maxn], p[maxn];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
rep(i, 1, n) {
cin >> a[i];
}
rep(i, 1, n) {//in pile
cin >> b[i];
if (b[i] == 1) pos = i;
}
if (pos) {//special
int flag = 1, k = 1;
rep(i, pos, n)//1 2 3......until the end
if (b[i] != k++) {
flag = 0; break;
}
if (flag) {
rep(i, 1, n)//in hand
In[a[i]] = 1;
int need = n - pos + 2;
rep(i, 1, pos - 1) {
if (!In[need]) {
flag = 0; break;
}
In[b[i]] = 1;
need++;
}
if (flag) {//一气呵成
cout << pos - 1 << endl;
exit(0);
}
}
}
rep(i, 1, n)
p[b[i]] = i;
rep(i, 1, n)
ans = max(ans, p[i] + (n - i + 1));//all ready for those behind i
cout << ans << endl;
return 0;
}