Advertisement
cincout

Untitled

Feb 1st, 2022 (edited)
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <set>
  5. #include <cmath>
  6. #include <iomanip>
  7. #include <complex>
  8. #include <queue>
  9. #include <cassert>
  10. #include <stack>
  11. #include <random>
  12. #include <chrono>
  13. #include <map>
  14. #define forn(i, n) for (int i = 0; i < n; ++i)
  15. #define all(a) begin(a), end(a)
  16. #define int long long
  17. #define sqr(x) ((x) * (x))
  18.  
  19. #ifndef _debug
  20. #define debug(...) 42
  21. #define debr(...) 42
  22. #else
  23. #define debug(x) cout << #x << " = " << x << endl
  24. #define debr(...) { cout << #__VA_ARGS__ << " = "; _debr(__VA_ARGS__); }
  25. #endif
  26.  
  27. using namespace std;
  28.  
  29. template<class Iter>
  30. void _debr(Iter l, Iter r) {
  31. cout << "[";
  32. for (auto it = l; it != r; ++it) {
  33. if (it != l) {
  34. cout << ", ";
  35. }
  36. cout << *it;
  37. }
  38. cout << "]" << endl;
  39. }
  40.  
  41. template<class C, class F>
  42. ostream& operator<<(ostream &out, pair<C, F> p) {
  43. return out << "(" << p.first << ", " << p.second << ")";
  44. }
  45.  
  46. using ll = long long;
  47.  
  48. mt19937_64 rnd(std::chrono::system_clock::now().time_since_epoch().count());
  49.  
  50. void solve() {
  51. int n;
  52. cin >> n;
  53. vector<int> a(n);
  54. forn(i, n) cin >> a[i], --a[i];
  55.  
  56. auto make = [&] (int i) {
  57. assert(a[i] != i);
  58. assert(a[i + 1] != i + 1);
  59. swap(a[i], a[i + 1]);
  60. cout << i + 1 << " " << i + 2 << "\n";
  61. cout << endl;
  62. };
  63.  
  64. for (int i = 0; i < n; ) {
  65. if (a[i] == i) {
  66. ++i;
  67. continue;
  68. }
  69.  
  70. int j = i;
  71. while (j < n && a[j] != j) ++j;
  72.  
  73. for (int k = j - 1; k >= i; --k) {
  74. int ps = -1;
  75. for (int c = k; c >= i; --c) {
  76. if (a[c] == k) ps = c;
  77. }
  78. debr(all(a));
  79. debug(ps);
  80. if (ps == -1) {
  81. cout << "impossible\n";
  82. return;
  83. }
  84. while (ps < k) {
  85. if (ps + 1 < k && a[ps + 1] == ps) {
  86. int r = ps + 1;
  87. while (r <= k && a[r] == r - 1) ++r;
  88. debug(r);
  89. if (r != k + 1) {
  90. while (r > ps + 1) {
  91. make(r - 1);
  92. --r;
  93. }
  94. }
  95. }
  96. make(ps);
  97. debr(all(a));
  98. ++ps;
  99. }
  100. }
  101.  
  102. i = j + 1;
  103. }
  104.  
  105. // forn(i, n) {
  106. // cout << a[i] << " ";
  107. // }
  108. // cout << "\n";
  109. }
  110.  
  111. signed main() {
  112. ios::sync_with_stdio(false);
  113. cin.tie(0);
  114.  
  115. solve();
  116. return 0;
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement