| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 117855 | zubec | Sorting (IOI15_sorting) | C++14 | 73 ms | 24100 KiB | 
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 <bits/stdc++.h>
using namespace std;
const int N = 200100;
int n, a[N], pos[N];
pair<int, int> pr[N];
pair<int, int> anss[N];
int b[N];
int f(int m){
    for (int i = 0; i < n; i++)
        b[i] = a[i];
    for (int i = 1; i <= m; i++){
        swap(b[pr[i].first], b[pr[i].second]);
    }
    for (int i = 0; i < n; i++){
        pos[b[i]] = i;
    }
    int ans = 0;
    for (int i = 0; i < n; i++){
        if (b[i] != i){
            anss[ans] = {i, b[i]};
            ++ans;
            int ps = b[i];
            swap(b[i], b[pos[i]]);
            swap(pos[ps], pos[i]);
        }
    }
    if (ans > m)
        return -1;
    while(ans < m){
        anss[ans] = {0, 0};
        ++ans;
    }
    return ans;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    n = N;
    for (int i = 0; i < n; i++)
        a[i] = S[i];
    for (int i = 0; i < M; i++){
        pr[i+1] = {X[i], Y[i]};
    }
    int l = 0, r = M;
    while(l < r){
        int mid = (l+r)>>1;
        if (f(mid) != -1)
            r = mid; else
            l = mid+1;
    }
    f(l);
    for (int i = 0; i < n; i++)
        pos[S[i]] = i;
    for (int i = 0; i < l; i++){
        swap(pos[S[X[i]]], pos[S[Y[i]]]);
        swap(S[X[i]], S[Y[i]]);
        int a = anss[i].first;
        int b = anss[i].second;
        P[i] = pos[a];
        Q[i] = pos[b];
        swap(S[pos[a]], S[pos[b]]);
        swap(pos[a], pos[b]);
    }
    return l;
}
/**
5
3 0 4 2 1
5
1 1
4 0
2 3
1 4
0 4
*/
Compilation message (stderr)
| # | 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... | ||||
