Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- int main() {
- #ifdef LOCAL
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif // LOCAL
- ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
- int n;
- cin >> n;
- int a[n], b[n], c[n];
- for (int i = 0; i < n; ++i) {
- cin >> a[i];
- b[i] = a[i];
- }
- sort(b, b + n);
- vector<pair<int, int>> res;
- auto reshuf = [&](int l, int r) {
- int m = (l + r + 1) / 2;
- for (int i = l; i < m; ++i) {
- c[l + 1 + (i - l) * 2] = b[i];
- }
- for (int i = m; i <= r; ++i) {
- c[l + (i - m) * 2] = b[i];
- }
- for (int i = l; i <= r; ++i)
- b[i] = c[i];
- };
- vector<pair<int, int>> qwerty;
- for (int i = 0; i < n; ++i) {
- while (a[i] != b[i]) {
- int j = i + 1;
- while (b[j] != a[i]) j++;
- int x = n - j, y = j - i;
- int t = min(x, y);
- reshuf(j - t, j + t - 1);
- qwerty.push_back({j - t, j + t - 1});
- }
- }
- reverse(qwerty.begin(), qwerty.end());
- for (auto i: qwerty)
- res.push_back(i);
- cout << res.size() << endl;
- for (auto i: res)
- cout << i.first + 1 << " " << i.second + 1 << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement