The meaning of problems
Total 2n cards, there are n n Zhang and Zhang empty card marked with the numbers 1-n cards, and now you have got n cards, cards pile of n cards, you can take a card from your hand to put under the deck, and then removed the top of a card deck. The minimum number of operations required to the deck plate 1-n in the order of arrangement
Thinking
Two cases:
Ⅰ 1 in the sequence number and sequence directly to the card
Must satisfy the condition: 1 in the sequence number 1
2. From a strictly increasing sequence start (check1 ())
3. turn any digital numbers are in the hands of (check2 ())
Answer: number to be added, i.e., nb [n]
Does not satisfy Ⅰ Ⅱ
The answer: By adding time to time added n + 1 digits
#include<bits/stdc++.h> using namespace std; #define ll long long #define M (1000000007) #define N (1000010) int n,mx,ans,a[N],b[N],id[N]; bool out[N]; bool check1(){ for (int i=id[1]+1;i<=n;i++) if (b[i]-b[i-1]!=1) return 0; return 1; } bool check2(){ for (int i=1;i<=id[1]-1;i++) if (b[i]!=0&&b[i]-b[n]<=i) return 0; return 1; } int main(){ scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&a[i]),out[a[i]]=1; for (int i=1;i<=n;i++){ scanf("%d",&b[i]); id[b[i]]=i; } if (id[1]!=0&&check1()&&check2()){ printf("%d\n",n-b[n]); return 0; } for (int i=1;i<=id[1];i++) out[i]=1; ans=id[1]; for (int i=id[1]+1;i<=n;i++) if (b[i]!=0) mx=max(mx,(i-id[1])-(b[i]-1)); printf("%d\n",ans+mx+n); }