# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
314790 | kylych03 | Sorting (IOI15_sorting) | C++14 | 0 ms | 0 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>
#include"grader.cpp"
using namespace std;
int n,s[800005],x[800005],y[800005],tmp[800005],pos[800005];
vector<pair<int,int>>res;
bool check(int m){
int i;
for(i=0;i<n;i++)tmp[i]=s[i];
for(i=0;i<m;i++)swap(tmp[x[i]],tmp[y[i]]);
for(i=0;i<n;i++)pos[tmp[i]]=i;
vector<pair<int,int>>sw;
res.clear();
for(i=0;i<n;i++){
if(pos[i]==i)continue;
sw.push_back({tmp[i],tmp[pos[i]]});
swap(tmp[pos[i]],tmp[i]);
swap(pos[tmp[pos[i]]],pos[tmp[i]]);
}
if(sw.size()>m)return 0;
for(i=0;i<n;i++)tmp[i]=s[i],pos[tmp[i]]=i;
for(i=0;i<sw.size();i++){
swap(tmp[x[i]],tmp[y[i]]);
swap(pos[tmp[x[i]]],pos[tmp[y[i]]]);
res.push_back({pos[sw[i].first],pos[sw[i].second]});
swap(tmp[pos[sw[i].first]],tmp[pos[sw[i].second]]);
swap(pos[tmp[pos[sw[i].first]]],pos[tmp[pos[sw[i].second]]]);
}
for(;i<m;i++)res.push_back({0,0});
return 1;
}
int findSwapPairs(int N,int S[],int m,int X[],int Y[],int P[],int Q[]){
n=N;
int i;
for(i=0;i<n;i++)s[i]=S[i];
for(i=0;i<m;i++)x[i]=X[i];
for(i=0;i<m;i++)y[i]=Y[i];
check(m);
for(i=0;i<m;i++)P[i]=res[i].first,Q[i]=res[i].second;
return m;
}