fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5.  
  6. const long double PI = 3.14159265359;
  7.  
  8. void solve()
  9. {
  10. int n;
  11. cin >> n;
  12. vector<int> a(n), b(n);
  13. for (auto &val : a)
  14. cin >> val;
  15. for (auto &val : b)
  16. cin >> val;
  17.  
  18. int l = -1, r = -1;
  19. for (int i = 0; i < n; i++)
  20. {
  21. if (a[i] != b[i])
  22. {
  23. l = i;
  24. break;
  25. }
  26. }
  27.  
  28. if (l == -1)
  29. return void(cout << "Yes" << '\n');
  30.  
  31. for (int i = n - 1; i >= 0; i--)
  32. {
  33. if (a[i] != b[i])
  34. {
  35. r = i;
  36. break;
  37. }
  38. }
  39.  
  40. vector<int> temp = a;
  41. // op1
  42. int val = temp[l];
  43. temp.erase(temp.begin() + l);
  44. temp.insert(temp.begin() + r, val);
  45. if (temp == b)
  46. return void(cout << "Yes" << '\n');
  47. // op2
  48. val = a[r];
  49. a.erase(a.begin() + r);
  50. a.insert(a.begin() + l, val);
  51. if (a == b)
  52. return void(cout << "Yes" << '\n');
  53. cout << "No" << '\n';
  54. }
  55.  
  56. signed main(void)
  57. {
  58. ios_base::sync_with_stdio(0);
  59. cin.tie(0);
  60. cout.tie(0);
  61. if (fopen("input.txt", "r"))
  62. {
  63. freopen("input.txt", "r", stdin);
  64. freopen("output.txt", "w", stdout);
  65. }
  66. int t = 1;
  67. // cin >> t;
  68. while (t--)
  69. {
  70. solve();
  71. }
  72. return 0;
  73. }
Success #stdin #stdout 0s 544KB
stdin
6
1 4 1 4 2 1
1 4 1 1 4 2
stdout
Yes