This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "sorting.h"
#include <algorithm>
#include <cassert>
#include <iostream>
using namespace std;
#define val first
#define idx second
typedef pair<int,int> pii;
const int MN = 200005;
pii A[MN];
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    int K = 0;
    auto record_swaps = [&](pii& a, pii& b) {
        if (a.val > b.val) {
            P[K] = a.idx;
            Q[K] = b.idx;
            swap(a.idx, b.idx);
            ++K;
        }
        return a.val < b.val;
    };
    for (int i=0; i<N; ++i) {
        A[i] = pii(S[i], i);
    }
    if (X[0] == 0 && Y[0] == 0) {
        sort(A, A+N, record_swaps);
    } else if (X[0] == 0 && Y[0] == 1) {
        sort(A+2, A+N, record_swaps);
        if ((K & 1) == 0) swap(A[0], A[1]);
        while (A[0].val + A[1].val > 1) {
            if (A[0] < A[1]) {
                P[K] = 1;
                swap(A[1], A[2]);
            } else {
                P[K] = 0;
                swap(A[0], A[2]);
            }
            Q[K] = 2;
            ++K;
            swap(A[0], A[1]);
            for (int i=2; i+1<N; ++i) {
                if (!record_swaps(A[i], A[i+1])) {
                    swap(A[i], A[i+1]);
                    swap(A[0], A[1]);
                } else break;
            }
        }
    } else assert(0);
    assert(K <= M);
    return K;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |