Submission #442160

# Submission time Handle Problem Language Result Execution time Memory
442160 2021-07-07T08:41:31 Z elazarkoren Xor Sort (eJOI20_xorsort) C++17
65 / 100
14 ms 1220 KB
#include <iostream>
#include <vector>
#include <algorithm>
#define x first
#define y second
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;

vi a;
vii ans;

void Xor(int i, int j) {
    a[i] ^= a[j];
    ans.push_back({i, j});
}

void Swap(int i, int j) {
    Xor(j, i);
    Xor(i, j);
    Xor(j, i);
//    ans.push_back({j, i});
//    ans.push_back({i, j});
//    ans.push_back({j, i});
//    swap(a[i], a[j]);
}

int main() {
    int n, s;
    cin >> n >> s;
    a.resize(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i];
    if (s == 1) {
        while (true) {
            bool is_new = false;
            for (int i = 1; i < n; i++) {
                if (a[i] > a[i + 1]) {
                    Swap(i, i + 1);
                    is_new = true;
                }
            }
            if (!is_new) break;
        }
    } else {
        int end = n;
        for (int bit = 19; bit >= 0; bit--) {
            bool visited = false;
            for (int i = 1; i < end; i++) {
                if ((a[i] >> bit) & 1) {
                    if (!((a[i + 1] >> bit) & 1)) {
                        Xor(i + 1, i);
                    }
                    Xor(i, i + 1);
                    visited = true;
                }
            }
            end -= visited;
        }
    }
    cout << ans.size() << '\n';
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i].x << ' ' << ans[i].y << '\n';
    }
}

Compilation message

xorsort.cpp: In function 'int main()':
xorsort.cpp:62:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |     for (int i = 0; i < ans.size(); i++) {
      |                     ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 4 ms 588 KB Output is correct
5 Correct 6 ms 588 KB Output is correct
6 Correct 4 ms 656 KB Output is correct
7 Correct 5 ms 624 KB Output is correct
8 Correct 5 ms 588 KB Output is correct
9 Correct 5 ms 588 KB Output is correct
10 Correct 5 ms 588 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 8 ms 840 KB Output is correct
13 Correct 8 ms 868 KB Output is correct
14 Correct 8 ms 840 KB Output is correct
15 Correct 10 ms 844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 4 ms 588 KB Output is correct
5 Correct 6 ms 588 KB Output is correct
6 Correct 4 ms 656 KB Output is correct
7 Correct 5 ms 624 KB Output is correct
8 Correct 5 ms 588 KB Output is correct
9 Correct 5 ms 588 KB Output is correct
10 Correct 5 ms 588 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 8 ms 840 KB Output is correct
13 Correct 8 ms 868 KB Output is correct
14 Correct 8 ms 840 KB Output is correct
15 Correct 10 ms 844 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 5 ms 588 KB Output is correct
18 Correct 9 ms 852 KB Output is correct
19 Correct 7 ms 812 KB Output is correct
20 Correct 7 ms 840 KB Output is correct
21 Correct 8 ms 840 KB Output is correct
22 Correct 7 ms 840 KB Output is correct
23 Correct 8 ms 840 KB Output is correct
24 Correct 7 ms 840 KB Output is correct
25 Correct 7 ms 840 KB Output is correct
26 Incorrect 14 ms 1220 KB Integer 59568 violates the range [0, 40000]
27 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 7 ms 840 KB Output is correct
6 Correct 7 ms 840 KB Output is correct
7 Correct 7 ms 828 KB Output is correct
8 Correct 8 ms 840 KB Output is correct
9 Correct 8 ms 804 KB Output is correct
10 Correct 8 ms 840 KB Output is correct
11 Correct 8 ms 840 KB Output is correct
12 Correct 11 ms 832 KB Output is correct
13 Correct 8 ms 840 KB Output is correct
14 Correct 9 ms 840 KB Output is correct
15 Correct 10 ms 988 KB Output is correct