Josif_tepe

Untitled

Feb 22nd, 2026
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <map>
  4. using namespace std;
  5.  
  6. int main () {
  7.     ios_base::sync_with_stdio(false);
  8.     int n;
  9.     cin >> n;
  10.    
  11.     vector<int> A(n), B(n), idx(n + 1);
  12.    
  13.     for(int i = 0; i < n; i++) {
  14.         cin >> A[i];
  15.         idx[A[i]] = i;
  16.     }
  17.    
  18.     for(int i = 0; i < n; i++) {
  19.         cin >> B[i];
  20.     }
  21.    
  22.     int res = 0;
  23.     for(int i = 0; i < n; i++) {
  24.         if(A[i] != B[i]) {
  25.             int tmp = idx[B[i]], tmp2 = A[i];
  26.             swap(A[i], A[tmp]);
  27.             idx[A[i]] = i;
  28.             idx[tmp2] = tmp;
  29.             res++;
  30.         }
  31.     }
  32.    
  33.     cout << res << endl;
  34.    
  35.     return 0;
  36. }
  37.  
  38.  
Advertisement
Add Comment
Please, Sign In to add comment