# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
298515 | TMJN | Sorting (IOI15_sorting) | C++17 | 2 ms | 512 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;
int s[200000];
int k[200000];
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
bool f=true;
for(int i=0;i<N;i++){
if(S[i]!=i)f=false;
}
if(f)return 0;
int d=0;
for(int i=0;i<N;i++){
if(S[i]!=i)d++;
}
int l,r;
l=-1;
r=M;
while(l+1!=r){
int m=(l+r)/2;
for(int i=0;i<N;i++){
s[i]=S[i];
}
for(int i=0;i<=m;i++){
swap(s[X[i]],s[Y[i]]);
}
for(int i=0;i<N;i++){
k[s[i]]=i;
}
int c=0;
for(int i=0;i<N;i++){
if(s[i]!=i){
c++;
swap(s[i],s[k[i]]);
swap(k[i],k[s[k[i]]]);
}
}
if(c<=m+1){
r=m;
}
else{
l=m;
}
}
for(int i=0;i<N;i++){
s[i]=S[i];
}
for(int i=0;i<=r;i++){
swap(s[X[i]],s[Y[i]]);
}
for(int i=0;i<N;i++){
k[s[i]]=i;
}
vector<pair<int,int>>vp;
for(int i=0;i<N;i++){
if(s[i]!=i){
vp.push_back({s[i],s[k[i]]});
swap(s[i],s[k[i]]);
swap(k[s[i]],k[s[k[i]]]);
}
}
if(vp.size()!=r+1){
vp.push_back({0,0});
}
for(int i=0;i<N;i++){
s[i]=S[i];
}
for(int i=0;i<N;i++){
k[s[i]]=i;
}
for(int i=0;i<=r;i++){
swap(s[X[i]],s[Y[i]]);
swap(k[s[X[i]]],k[s[Y[i]]]);
P[i]=k[vp[i].first];
Q[i]=k[vp[i].second];
swap(s[P[i]],s[Q[i]]);
swap(k[s[P[i]]],k[s[Q[i]]]);
}
if(vp.back()==pair<int,int>{0,0}){
return r;
}
else{
return r+1;
}
}
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... |