# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
590907 | AlperenT | Sorting (IOI15_sorting) | C++17 | 161 ms | 11636 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 <bits/stdc++.h>
#include "sorting.h"
using namespace std;
int swapcnt(vector<int> v){
int cnt = 0, n = v.size();
vector<bool> vis(n, false);
for(int i = 0; i < n; i++){
if(!vis[i]){
int x = i;
while(!vis[x]){
cnt++;
vis[x] = true;
x = v[x];
}
cnt--;
}
}
return cnt;
}
int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]){
vector<int> v, v2;
v.assign(n, 0);
copy(S, S + n, v.begin());
int l = -1, r = M;
while(r - l > 1){
int m = l + (r - l) / 2;
v2 = v;
for(int i = 0; i < m; i++) swap(v2[X[i]], v2[Y[i]]);
if(swapcnt(v2) <= m) r = m;
else l = m;
}
v2 = v;
for(int i = 0; i < r; i++) swap(v2[X[i]], v2[Y[i]]);
vector<pair<int, int>> swaps;
vector<bool> vis(n, false);
for(int i = 0; i < n; i++){
if(!vis[i]){
vis[i] = true;
int x = i;
while(!vis[v2[x]]){
vis[x] = true;
swaps.push_back({v2[x], v2[v2[x]]});
x = v2[x];
}
}
}
while(swaps.size() < r) swaps.push_back({0, 0});
vector<int> where(n);
for(int i = 0; i < n; i++) where[v[i]] = i;
for(int i = 0; i < r; i++){
swap(where[v[X[i]]], where[v[Y[i]]]);
swap(v[X[i]], v[Y[i]]);
P[i] = where[swaps[i].first];
Q[i] = where[swaps[i].second];
swap(where[v[P[i]]], where[v[Q[i]]]);
swap(v[P[i]], v[Q[i]]);
}
assert(is_sorted(v.begin(), v.end()));
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... |