Submission #1211772

#TimeUsernameProblemLanguageResultExecution timeMemory
1211772badge881Xor Sort (eJOI20_xorsort)C++20
100 / 100
28 ms1472 KiB
#include "bits/stdc++.h"
using namespace std;

#define int long long
#define endl endl

const int mod = 1e9 + 7;
const int maxn = 1e6;

int n, s, arr[maxn];
vector < pair < int, int >> ans;
void s1()
{
    for (int i = n - 1; i >= 0; i--)
    {
        int p = -1;
        for (int j = 0; j <= i; j++)
        {
            if (p == -1 || arr[p] < arr[j]) p = j;
            if (j) ans.push_back({j - 1, j});
        }
        for (int j = p + 1; j <= i; j++) ans.push_back({j, j - 1});

        for (int j = p - 2; j >= 0; j--) ans.push_back({j, j + 1});

        for (int j = p; j + 1 < n; j++) swap(arr[j], arr[j + 1]);

    }
    for (int i = n - 2; i >= 0; --i) ans.push_back({i, i + 1});
}

void s2()
{
    for(int i = 19; i >= 0; i--)
    {
        for(int j = 0; j < n - 1; j++)
        {
            if(arr[j + 1] >= (1 << (i + 1))) break;
            if(!(arr[j] & (1 << i))) continue;
            if(!(arr[j + 1] & (1 << i))) ans.push_back({j + 1, j}), arr[j + 1] ^= arr[j];

            ans.push_back({j, j + 1});
            arr[j] ^= arr[j + 1];
        }
    }
}

signed main()
{
    scanf("%lld%lld", &n, &s);

    for(int i = 0; i < n; i++) scanf("%lld", &arr[i]);
    int arr2[n];


    if(s == 1) s1();
    else s2();

    cout << ans.size() << endl;
    for(auto [i, j] : ans) cout << i + 1 << " " << j + 1 << endl;

}

Compilation message (stderr)

xorsort.cpp: In function 'int main()':
xorsort.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |     scanf("%lld%lld", &n, &s);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~
xorsort.cpp:52:37: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |     for(int i = 0; i < n; i++) scanf("%lld", &arr[i]);
      |                                ~~~~~^~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...