답안 #774808

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
774808 2023-07-06T03:40:53 Z vjudge1 Xor Sort (eJOI20_xorsort) C++17
0 / 100
1 ms 212 KB
#include <bits/stdc++.h>

using namespace std;

int n, s;
vector<pair<int, int>> v;
vector<int> valueToIndex, indexToValue;
vector<pair<int, int>> operations;

void performOperation(const int& i, const int& j)
{
    operations.emplace_back(i, j);
}

void performSwap(const int& i, const int& j)
{
    assert(max(i - j, j - i) == 1);
    performOperation(i, j);
    performOperation(j, i);
    performOperation(i, j);
}

/*
 * a b
 * a^b b
 * a^b a
 *
 */

int main()
{
    // freopen("inp.txt", "r", stdin);

    ios::sync_with_stdio(false);

    cin >> n >> s;
    v.resize(n);
    for (int i = 0; i < n; i++)
    {
        cin >> v[i].first;
        v[i].second = i;
    }

    sort(v.begin(), v.end());

    valueToIndex.resize(n);
    indexToValue.resize(n);
    for (int i = 0; i < n; i++)
    {
        indexToValue[i] = v[i].second;
        valueToIndex[indexToValue[i]] = i;
    }

    for (int i = 0; i < n; i++)
    {
        int x = valueToIndex[i];
        for (int i1 = x; i1 > i; i1--)
        {
            int index = valueToIndex[i];
            int otherIndex = index - 1;
            int prevValueAtIndex = i;
            int prevValueAtOtherIndex = indexToValue[otherIndex];

            valueToIndex[prevValueAtIndex] = otherIndex;
            indexToValue[otherIndex] = prevValueAtIndex;

            valueToIndex[prevValueAtOtherIndex] = index;
            indexToValue[index] = prevValueAtOtherIndex;

            performSwap(index, otherIndex);
        }
    }

    cout << operations.size() << '\n';
    for (auto &operation : operations)
        cout << operation.first + 1 << ' ' << operation.second + 1 << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Not sorted
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB Not sorted
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 212 KB Not sorted
2 Halted 0 ms 0 KB -