제출 #367486

#제출 시각아이디문제언어결과실행 시간메모리
367486alextodoranXor Sort (eJOI20_xorsort)C++17
100 / 100
11 ms1132 KiB
/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 1002;

int n;

int S;

int initial[N_MAX];

int v[N_MAX];

vector <pair <int, int>> ans;

void fxor (int a, int b)
{
    v[a] ^= v[b];
    ans.push_back(make_pair(a, b));
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> S;
    for(int i = 1; i <= n; i++)
        cin >> v[i];
    if(S == 1)
    {
        for(int i = 1; i <= n; i++)
            initial[i] = v[i];
        for(int k = n; k >= 1; k--)
        {
            for(int i = 1; i < n && i <= k; i++)
                fxor(i, i + 1);
            int pos = -1;
            for(int i = 1; i <= k; i++)
                if(pos == -1 || initial[i] > initial[pos])
                    pos = i;
            for(int i = pos - 2; i >= 1; i--)
                fxor(i, i + 1);
            for(int i = pos + 1; i <= k; i++)
                fxor(i, i - 1);
            int aux = initial[pos];
            for(int i = pos; i < k; i++)
                initial[i] = initial[i + 1];
            initial[k] = aux;
        }
    }
    else
    {
        for(int bit = 19, k = n; bit >= 0 && k >= 1; bit--)
        {
            bool ok = false;
            for(int i = 1; i <= k; i++)
                if(((v[i] >> bit) & 1) == 1)
                    ok = true;
            if(ok == true)
            {
                for(int i = 2; i <= k; i++)
                    if(((v[i] >> bit) & 1) == 0)
                        fxor(i, i - 1);
                for(int i = 1; i <= k - 1; i++)
                    if(((v[i] >> bit) & 1) == 1)
                        fxor(i, i + 1);
                k--;
            }
        }
    }
    cout << ans.size() << "\n";
    for(pair <int, int> &p : ans)
        cout << p.first << " " << p.second << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...