Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef int64_t lld;
  4. typedef uint64_t llu;
  5. typedef long double llf;
  6. #define PB push_back
  7. #define ALL(x) begin(x), end(x)
  8. #define WC(x) while(x--)
  9. #define FF first
  10. #define SS second
  11. const int N = 3000000 + 5;
  12.  
  13. int a[N], b[N], pos[N];
  14.  
  15. int get_array(int[]);
  16.  
  17. int main(){
  18. int n = get_array(a);
  19. int m = get_array(b);
  20. if(n != m){
  21. puts("-1");
  22. return 0;
  23. }
  24. for(int i=0;i<n;i++) pos[b[i]] = i;
  25. int ans = 0;
  26. for(int i=0;i<m;i++){
  27. if(b[i]==a[i]) continue;
  28. int x = i, y = pos[a[i]];
  29. pos[b[x]] = y; pos[b[y]] = x;
  30. swap(b[x], b[y]);
  31. ans ++;
  32. }
  33. printf("%d\n", ans);
  34. return 0;
  35. }
  36.  
  37. int get_array(int arr[]){
  38. int p = 0;
  39. while(true){
  40. int x = 0;
  41. char c = getchar_unlocked();
  42. if(c == '\n') return 0;
  43. while(c == ' ') c=getchar_unlocked();
  44. while(isdigit(c)){
  45. x = x*10+c-'0';
  46. c = getchar_unlocked();
  47. }
  48. arr[p++] = x;
  49. if(c == '\n') break;
  50. }
  51. return p;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement