Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define endl "\n"
- using namespace std;
- using ll = long long;
- using pii = pair<int, int>;
- constexpr int N = 3e5+5;
- int n, mp[N], pm[N], el;
- vector<pii> ans;
- void sw2(int i, int j)
- {
- ans.push_back({i, j});
- swap(pm[mp[i]], pm[mp[j]]);
- swap(mp[i], mp[j]);
- }
- void sw1(int id)
- {
- int i = 1, temp;
- if (id<n/2)
- i = n;
- sw2(i, id);
- }
- void Solve()
- {
- cin >> n;
- for (int i = 1; i <= n; i++)
- {
- cin >> el;
- mp[i] = el;
- pm[el] = i;
- }
- int id1 = n/4+1, id2 = n - n/4;
- for (int i = id1; i < id2; i++)
- {
- if (pm[i]!=i)
- {
- if (abs(pm[i]-i)<n/2)
- sw1(pm[i]);
- sw2(pm[i], i);
- }
- }
- for (int i = 1; i < id1; i++)
- {
- if (pm[i]!=i)
- {
- if (abs(pm[i]-i)<n/2)
- {
- sw1(pm[i]);
- }
- sw2(pm[i], i);
- }
- }
- for (int i = id2; i <= n; i++)
- {
- if (pm[i]!=i)
- {
- if (abs(pm[i]-i)<n/2)
- {
- sw1(pm[i]);
- }
- sw2(pm[i], i);
- }
- }
- cout << ans.size() << endl;
- for (auto [a, b]:ans)
- {
- cout << a << " " << b << endl;
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- Solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement