Submission #519162

#TimeUsernameProblemLanguageResultExecution timeMemory
519162tabrSorting (IOI15_sorting)C++17
100 / 100
329 ms14496 KiB
#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
    int low = -1;
    int high = m + 1;
    while (high - low > 1) {
        int mid = (high + low) >> 1;
        vector<int> t(n);
        for (int i = 0; i < n; i++) {
            t[i] = s[i];
        }
        for (int i = 0; i < mid; i++) {
            swap(t[x[i]], t[y[i]]);
        }
        vector<pair<int, int>> a;
        vector<int> pos(n);
        for (int i = 0; i < n; i++) {
            pos[t[i]] = i;
        }
        for (int i = 0; i < n; i++) {
            if (t[i] == i) {
                continue;
            }
            int j = pos[i];
            a.emplace_back(t[i], t[j]);
            swap(t[i], t[j]);
            swap(pos[t[i]], pos[t[j]]);
        }
        if ((int) a.size() <= mid) {
            for (int i = 0; i < n; i++) {
                t[i] = s[i];
                pos[t[i]] = i;
            }
            for (int i = 0; i < mid; i++) {
                swap(t[x[i]], t[y[i]]);
                swap(pos[t[x[i]]], pos[t[y[i]]]);
                if (i >= (int) a.size()) {
                    p[i] = q[i] = 0;
                } else {
                    p[i] = pos[a[i].first];
                    q[i] = pos[a[i].second];
                    swap(t[p[i]], t[q[i]]);
                    swap(pos[t[p[i]]], pos[t[q[i]]]);
                }
            }
            high = mid;
        } else {
            low = mid;
        }
    }
    return high;
}

#ifdef tabr
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    return 0;
}
#endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...