Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <set>
- #include <cmath>
- #include <iomanip>
- #include <complex>
- #include <queue>
- #include <cassert>
- #include <stack>
- #include <random>
- #include <chrono>
- #include <map>
- #define forn(i, n) for (int i = 0; i < n; ++i)
- #define all(a) begin(a), end(a)
- #define int long long
- #define sqr(x) ((x) * (x))
- #ifndef _debug
- #define debug(...) 42
- #define debr(...) 42
- #else
- #define debug(x) cout << #x << " = " << x << endl
- #define debr(...) { cout << #__VA_ARGS__ << " = "; _debr(__VA_ARGS__); }
- #endif
- using namespace std;
- template<class Iter>
- void _debr(Iter l, Iter r) {
- cout << "[";
- for (auto it = l; it != r; ++it) {
- if (it != l) {
- cout << ", ";
- }
- cout << *it;
- }
- cout << "]" << endl;
- }
- template<class C, class F>
- ostream& operator<<(ostream &out, pair<C, F> p) {
- return out << "(" << p.first << ", " << p.second << ")";
- }
- using ll = long long;
- mt19937_64 rnd(std::chrono::system_clock::now().time_since_epoch().count());
- void solve() {
- int n;
- cin >> n;
- vector<int> a(n);
- forn(i, n) cin >> a[i], --a[i];
- auto make = [&] (int i) {
- assert(a[i] != i);
- assert(a[i + 1] != i + 1);
- swap(a[i], a[i + 1]);
- cout << i + 1 << " " << i + 2 << "\n";
- cout << endl;
- };
- for (int i = 0; i < n; ) {
- if (a[i] == i) {
- ++i;
- continue;
- }
- int j = i;
- while (j < n && a[j] != j) ++j;
- for (int k = j - 1; k >= i; --k) {
- int ps = -1;
- for (int c = k; c >= i; --c) {
- if (a[c] == k) ps = c;
- }
- debr(all(a));
- debug(ps);
- if (ps == -1) {
- cout << "impossible\n";
- return;
- }
- while (ps < k) {
- if (ps + 1 < k && a[ps + 1] == ps) {
- int r = ps + 1;
- while (r <= k && a[r] == r - 1) ++r;
- debug(r);
- if (r != k + 1) {
- while (r > ps + 1) {
- make(r - 1);
- --r;
- }
- }
- }
- make(ps);
- debr(all(a));
- ++ps;
- }
- }
- i = j + 1;
- }
- // forn(i, n) {
- // cout << a[i] << " ";
- // }
- // cout << "\n";
- }
- signed main() {
- ios::sync_with_stdio(false);
- cin.tie(0);
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement