fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define lli long long int
  4. #define inf 1000000000
  5. #define pb push_back
  6. #define mp make_pair
  7. #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  8. #define endl "\n"
  9. #define yoi cout<<"yo"<<endl;
  10. #define debug(i) cout<<i<<endl;
  11. int have[200001]={0};
  12. const int mod=1e9 +7;
  13. int main(){
  14. int n,i,j;
  15. cin>>n;
  16. int a[n],b[n];
  17. for(i=0;i<n;i++){
  18. cin>>a[i];
  19. }
  20. for(i=0;i<n;i++){
  21. cin>>b[i];
  22. }
  23. int zero=0;
  24. for(i=0;i<n;i++){
  25. if(!a[i])
  26. zero++;
  27. }
  28. int start=0,last=zero,middle,index=0;
  29. int have[n+1];
  30. while(start<=last){
  31. memset(have,0,sizeof(have));
  32. for(i=0;i<n;i++){
  33. if(a[i])
  34. have[a[i]]++;
  35. }
  36. middle=(start+last)/2;
  37. for(j=0;j<middle;j++){
  38. if(b[j])
  39. have[b[j]]++;
  40. }
  41. //cout<<"yo"<<" "<<middle<<endl;
  42. for(i=1;i<=n;i++){
  43. if(!have[i])
  44. break;
  45. else{
  46. if(b[j])
  47. have[b[j]]++;
  48. j=(j+1)%n;
  49. }
  50. }
  51. //cout<<i<<" "<<middle<<endl;
  52. if(i==n+1){
  53. last=middle-1;
  54. index=middle;
  55. }
  56. else start=middle+1;
  57. }
  58. //cout<<index<<endl;
  59. if(index){
  60. cout<<index+n<<endl;
  61. }
  62. else{
  63. for(j=0;j<n;j++){
  64. if(b[j]==1)
  65. break;
  66. }
  67. cout<<j<<endl;
  68. }
  69. }
  70.  
Success #stdin #stdout 0s 16016KB
stdin
11
0 0 0 5 0 0 0 4 0 0 11
9 2 6 0 8 1 7 0 3 0 10
stdout
18