Advertisement
limimage

TASKC

Jun 1st, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define endl "\n"
  3. using namespace std;
  4. using ll = long long;
  5. using pii = pair<int, int>;
  6.  
  7. constexpr int N = 3e5+5;
  8.  
  9. int n, mp[N], pm[N], el;
  10. vector<pii> ans;
  11.  
  12. void sw2(int i, int j)
  13. {
  14. ans.push_back({i, j});
  15. swap(pm[mp[i]], pm[mp[j]]);
  16. swap(mp[i], mp[j]);
  17. }
  18.  
  19. void sw1(int id)
  20. {
  21. int i = 1, temp;
  22. if (id<n/2)
  23. i = n;
  24. sw2(i, id);
  25. }
  26.  
  27.  
  28. void Solve()
  29. {
  30. cin >> n;
  31. for (int i = 1; i <= n; i++)
  32. {
  33. cin >> el;
  34. mp[i] = el;
  35. pm[el] = i;
  36. }
  37. int id1 = n/4+1, id2 = n - n/4;
  38. for (int i = id1; i < id2; i++)
  39. {
  40. if (pm[i]!=i)
  41. {
  42. if (abs(pm[i]-i)<n/2)
  43. sw1(pm[i]);
  44. sw2(pm[i], i);
  45. }
  46. }
  47. for (int i = 1; i < id1; i++)
  48. {
  49. if (pm[i]!=i)
  50. {
  51. if (abs(pm[i]-i)<n/2)
  52. {
  53. sw1(pm[i]);
  54. }
  55. sw2(pm[i], i);
  56. }
  57. }
  58. for (int i = id2; i <= n; i++)
  59. {
  60. if (pm[i]!=i)
  61. {
  62. if (abs(pm[i]-i)<n/2)
  63. {
  64. sw1(pm[i]);
  65. }
  66. sw2(pm[i], i);
  67. }
  68. }
  69. cout << ans.size() << endl;
  70. for (auto [a, b]:ans)
  71. {
  72. cout << a << " " << b << endl;
  73. }
  74. }
  75.  
  76. int main()
  77. {
  78. ios_base::sync_with_stdio(false);
  79. cin.tie(nullptr);
  80. cout.tie(nullptr);
  81. Solve();
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement