Submission #804475

# Submission time Handle Problem Language Result Execution time Memory
804475 2023-08-03T08:57:51 Z otarius Xor Sort (eJOI20_xorsort) C++17
100 / 100
6 ms 992 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std;

#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair <int, int>
#define ull unsigned long long

// #define int long long
// #define int unsigned long long

const ll inf = 1e9 + 7;
const ll weirdMod = 998244353;

vector<pii> ans;
bool get(int n, int k) {
    return ((n >> k) & 1);
}
void opr(int a, int b) {
    ans.pb({a, b});
}
void solve() {
    int n, s;
    cin >> n >> s;
    int arr[n + 1];
    for (int i = 1; i <= n; i++)
        cin >> arr[i];
    if (s == 2) {
        int lst = n;
        for (int bt = 19; bt >= 0; bt--) {
            int pos = -1;
            for (int i = 1; i <= lst; i++)
                if (get(arr[i], bt)) pos = i;
            if (pos == -1) continue;
            for (int i = pos + 1; i <= lst; i++) {
                if (!get(arr[i], bt)) {
                    opr(i, i - 1); arr[i] ^= arr[i - 1];
                }
            } for (int i = pos - 1; i >= 1; i--) {
                if (!get(arr[i], bt)) {
                    opr(i, i + 1); arr[i] ^= arr[i + 1];
                }
            }
            for (int i = 2; i <= lst; i++) {
                opr(i - 1, i); arr[i - 1] ^= arr[i];
            } --lst;
        }
    } else {
        int b[n + 1];
        for (int i = 1; i <= n; i++)
            b[i] = arr[i];
        for (int i = 1; i < n; i++) {
            opr(i, i + 1); arr[i] ^= arr[i + 1];
        } int lst = n;
        for (int i = 1; i <= n; i++) {
            int pos = 1;
            for (int j = 2; j <= lst; j++) {
                if (b[pos] < b[j]) pos = j;
            } for (int j = pos; j < lst; j++) {
                opr(j + 1, j); arr[j + 1] ^= arr[j];
            } for (int j = max(pos - 1, 1); j < lst; j++) {
                opr(j, j + 1); arr[j] ^= arr[j + 1];
            } for (int j = pos; j < lst; j++)
                swap(b[j], b[j + 1]);
            --lst;
        }
    }
    cout << ans.size() << '\n';
    for (auto [a, b] : ans) cout << a << ' ' << b << '\n';
}
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
        cout << '\n';
    }
    return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 2 ms 584 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 1 ms 224 KB Output is correct
12 Correct 3 ms 728 KB Output is correct
13 Correct 5 ms 704 KB Output is correct
14 Correct 3 ms 652 KB Output is correct
15 Correct 3 ms 704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 256 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 2 ms 596 KB Output is correct
6 Correct 2 ms 584 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 1 ms 224 KB Output is correct
12 Correct 3 ms 728 KB Output is correct
13 Correct 5 ms 704 KB Output is correct
14 Correct 3 ms 652 KB Output is correct
15 Correct 3 ms 704 KB Output is correct
16 Correct 1 ms 320 KB Output is correct
17 Correct 2 ms 576 KB Output is correct
18 Correct 3 ms 728 KB Output is correct
19 Correct 4 ms 728 KB Output is correct
20 Correct 3 ms 728 KB Output is correct
21 Correct 3 ms 728 KB Output is correct
22 Correct 3 ms 728 KB Output is correct
23 Correct 3 ms 676 KB Output is correct
24 Correct 3 ms 728 KB Output is correct
25 Correct 3 ms 728 KB Output is correct
26 Correct 5 ms 968 KB Output is correct
27 Correct 6 ms 980 KB Output is correct
28 Correct 5 ms 980 KB Output is correct
29 Correct 5 ms 980 KB Output is correct
30 Correct 6 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 5 ms 856 KB Output is correct
6 Correct 6 ms 856 KB Output is correct
7 Correct 4 ms 856 KB Output is correct
8 Correct 4 ms 856 KB Output is correct
9 Correct 4 ms 856 KB Output is correct
10 Correct 4 ms 864 KB Output is correct
11 Correct 4 ms 856 KB Output is correct
12 Correct 4 ms 860 KB Output is correct
13 Correct 4 ms 928 KB Output is correct
14 Correct 4 ms 856 KB Output is correct
15 Correct 5 ms 992 KB Output is correct