Submission #466027

# Submission time Handle Problem Language Result Execution time Memory
466027 2021-08-17T15:03:08 Z SamAnd Xor Sort (eJOI20_xorsort) C++17
100 / 100
11 ms 1004 KB
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 1003, m = 20;

int n, ty;
int a[N];

vector<pair<int, int> > ans;
void ubd(int x, int y)
{
    ans.push_back(m_p(x, y));
    a[x] ^= a[y];
}

void solv2()
{
    int nn = n;
    for (int j = m - 1; j >= 0; --j)
    {
        for (int i = 1; i < nn; ++i)
        {
            if ((a[i] & (1 << j)))
            {
                if ((a[i + 1] & (1 << j)))
                    ubd(i, i + 1);
                else
                {
                    ubd(i + 1, i);
                    ubd(i, i + 1);
                }
            }
        }
        if ((a[nn] & (1 << j)))
            --nn;
    }

    for (int i = 1; i < n; ++i)
        assert(a[i] <= a[i + 1]);
}

void solv1()
{
    vector<int> v;
    for (int i = 1; i <= n; ++i)
        v.push_back(a[i]);
    sort(all(v));

    reverse(all(v));
    for (int i = 0; i < sz(v); ++i)
    {
        int u = -1;
        for (int j = 1; j <= n - i; ++j)
        {
            if (i)
            {
                if (a[j] == (v[i] ^ v[i - 1]))
                    u = j;
            }
            else
            {
                if (a[j] == v[i])
                    u = j;
            }
        }
        for (int j = 1; j < n - i; ++j)
        {
            ubd(j, j + 1);
        }
        for (int j = u + 1; j <= n - i; ++j)
        {
            ubd(j, j - 1);
        }
        for (int j = u - 2; j >= 1; --j)
        {
            ubd(j, j + 1);
        }
    }

    for (int i = n - 1; i >= 1; --i)
        ubd(i, i + 1);

    for (int i = 1; i < n; ++i)
        assert(a[i] < a[i + 1]);
}

void solv()
{
    cin >> n >> ty;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];

    if (ty == 2)
    {
        solv2();
    }
    else
    {
        solv1();
    }

    cout << sz(ans) << "\n";
    for (int i = 0; i < sz(ans); ++i)
        cout << ans[i].fi << ' ' << ans[i].se << "\n";
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    ios_base::sync_with_stdio(false), cin.tie(0);

    int tt = 1;
    //cin >> tt;
    while (tt--)
    {
        solv();
    }
    return 0;
}
# 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 332 KB Output is correct
4 Correct 5 ms 720 KB Output is correct
5 Correct 5 ms 720 KB Output is correct
6 Correct 5 ms 720 KB Output is correct
7 Correct 5 ms 720 KB Output is correct
8 Correct 5 ms 720 KB Output is correct
9 Correct 5 ms 720 KB Output is correct
10 Correct 5 ms 780 KB Output is correct
11 Correct 5 ms 720 KB Output is correct
12 Correct 5 ms 720 KB Output is correct
13 Correct 5 ms 744 KB Output is correct
14 Correct 5 ms 716 KB Output is correct
15 Correct 5 ms 720 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 332 KB Output is correct
4 Correct 5 ms 720 KB Output is correct
5 Correct 5 ms 720 KB Output is correct
6 Correct 5 ms 720 KB Output is correct
7 Correct 5 ms 720 KB Output is correct
8 Correct 5 ms 720 KB Output is correct
9 Correct 5 ms 720 KB Output is correct
10 Correct 5 ms 780 KB Output is correct
11 Correct 5 ms 720 KB Output is correct
12 Correct 5 ms 720 KB Output is correct
13 Correct 5 ms 744 KB Output is correct
14 Correct 5 ms 716 KB Output is correct
15 Correct 5 ms 720 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 5 ms 732 KB Output is correct
18 Correct 8 ms 1004 KB Output is correct
19 Correct 8 ms 976 KB Output is correct
20 Correct 8 ms 976 KB Output is correct
21 Correct 11 ms 976 KB Output is correct
22 Correct 8 ms 976 KB Output is correct
23 Correct 9 ms 984 KB Output is correct
24 Correct 8 ms 976 KB Output is correct
25 Correct 8 ms 984 KB Output is correct
26 Correct 9 ms 976 KB Output is correct
27 Correct 8 ms 976 KB Output is correct
28 Correct 8 ms 976 KB Output is correct
29 Correct 8 ms 976 KB Output is correct
30 Correct 8 ms 976 KB Output is correct
# 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 332 KB Output is correct
4 Correct 2 ms 460 KB Output is correct
5 Correct 6 ms 848 KB Output is correct
6 Correct 7 ms 848 KB Output is correct
7 Correct 6 ms 848 KB Output is correct
8 Correct 6 ms 848 KB Output is correct
9 Correct 6 ms 848 KB Output is correct
10 Correct 6 ms 848 KB Output is correct
11 Correct 6 ms 848 KB Output is correct
12 Correct 7 ms 848 KB Output is correct
13 Correct 7 ms 848 KB Output is correct
14 Correct 7 ms 908 KB Output is correct
15 Correct 8 ms 976 KB Output is correct