# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
797242 | Ronin13 | Sorting (IOI15_sorting) | C++17 | 140 ms | 21764 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>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
int findSwapPairs(int n, int s[], int m, int x[], int y[], int P[], int Q[]) {
int l = -1, r = m;
while(l + 1 < r){
int mid = (l + r) / 2;
int b[n];
for(int i = 0; i < n; i++){
b[i] = s[i];
}
for(int i = 0; i < mid; i++){
swap(b[x[i]], b[y[i]]);
}
int pos[n];
int p[n];
for(int i = 0; i < n; i++){
pos[s[i]] = i;
p[b[i]] = i;
}
int a[n];
for(int i = 0; i < n; i++){
a[i] = s[i];;
}
int ind = 0;
for(int i = 0; i < mid; i++){
swap(pos[a[x[i]]], pos[a[y[i]]]);
swap(a[x[i]], a[y[i]]);
while(ind < n && b[ind] == ind) ind++;
if(ind == n) continue;
int x = b[ind];
swap(b[ind], b[p[ind]]);
swap(p[x], p[ind]);
swap(pos[x], pos[ind]);
swap(a[pos[x]], a[pos[ind]]);
}
bool good = true;
for(int i = 0; i < n; i++){
if(a[i] != i) good = false;
}
good ? r = mid : l = mid;
}
int b[n];
for(int i = 0; i < n; i++){
b[i] = s[i];
}
int p[n];
for(int i = 0; i < r; i++){
swap(b[x[i]], b[y[i]]);
}
int pos[n];
for(int i = 0; i < n; i++){
pos[s[i]] = i;
p[b[i]] = i;
}
int a[n];
for(int i = 0; i < n; i++){
a[i] = s[i];;
}
int ind = 0;
for(int i = 0; i < r; i++){
swap(pos[a[x[i]]], pos[a[y[i]]]);
swap(a[x[i]], a[y[i]]);
while(ind < n && b[ind] == ind) ind++;
if(ind == n) {
P[i] = Q[i] = 0;
continue;
}
int x = b[ind];
swap(b[ind], b[p[ind]]);
swap(p[x], p[ind]);
P[i] = pos[x], Q[i] = pos[ind];
swap(pos[x], pos[ind]);
swap(a[pos[x]], a[pos[ind]]);
}
bool good = true;
for(int i = 0; i < n; i++){
if(a[i] != i) good = false;
}
return r;
}
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... |