Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long ll;
  5. typedef long double ld;
  6.  
  7. int main() {
  8. #ifdef LOCAL
  9.     freopen("input.txt", "r", stdin);
  10.     freopen("output.txt", "w", stdout);
  11. #endif // LOCAL
  12.     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  13.  
  14.  
  15.     int n;
  16.     cin >> n;
  17.  
  18.     int a[n], b[n], c[n];
  19.  
  20.     for (int i = 0; i < n; ++i) {
  21.         cin >> a[i];
  22.         b[i] = a[i];
  23.     }
  24.     sort(b, b + n);
  25.  
  26.     vector<pair<int, int>> res;
  27.  
  28.     auto reshuf = [&](int l, int r) {
  29.                       int m = (l + r + 1) / 2;
  30.  
  31.                       for (int i = l; i < m; ++i) {
  32.                           c[l + 1 + (i - l) * 2] = b[i];
  33.                       }
  34.                       for (int i = m; i <= r; ++i) {
  35.                           c[l + (i - m) * 2] = b[i];
  36.                       }
  37.  
  38.                       for (int i = l; i <= r; ++i)
  39.                           b[i] = c[i];
  40.                   };
  41.  
  42.     vector<pair<int, int>> qwerty;
  43.    
  44.     for (int i = 0; i < n; ++i) {
  45.         while (a[i] != b[i]) {
  46.             int j = i + 1;
  47.  
  48.             while (b[j] != a[i]) j++;
  49.  
  50.             int x = n - j, y = j - i;
  51.             int t = min(x, y);
  52.             reshuf(j - t, j + t - 1);
  53.             qwerty.push_back({j - t, j + t - 1});
  54.            
  55.         }
  56.     }
  57.  
  58.     reverse(qwerty.begin(), qwerty.end());
  59.    
  60.     for (auto i: qwerty)
  61.         res.push_back(i);
  62.  
  63.     cout << res.size() << endl;
  64.  
  65.     for (auto i: res)
  66.         cout << i.first + 1 << " " << i.second + 1 << "\n";
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement