Submission #590134

#TimeUsernameProblemLanguageResultExecution timeMemory
590134alirezasamimi100Sorting (IOI15_sorting)C++17
100 / 100
175 ms20820 KiB
#include "sorting.h"
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
using namespace std;
using pii = pair<int, int>;

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
    int l=-1,r=M,k,T[N],f[N],R[N];
    vector<pii> mv;
    while(r-l>1){
        int m=(l+r)>>1;
        k=N;
        memset(f,0,sizeof f);
        for(int i=0; i<N; i++) T[i]=S[i];
        for(int i=0; i<m; i++){
            swap(T[X[i]],T[Y[i]]);
        }
        for(int i=0; i<N; i++){
            if(f[i]) continue;
            k--;
            int x=i;
            while(!f[x]){
                f[x]=1;
                x=T[x];
            }
        }
        if(k<=m) r=m;
        else l=m;
    }
    memset(f,0,sizeof f);
    for(int i=0; i<N; i++){
        T[i]=S[i];
        R[S[i]]=i;
    }
    for(int i=0; i<r; i++){
        swap(T[X[i]],T[Y[i]]);
    }
    for(int i=0; i<N; i++){
        if(f[i]) continue;
        int x=i;
        while(!f[x]){
            f[x]=1;
            x=T[x];
            if(x!=i) mv.pb({i,x});
        }
    }
    int p=0;
    for(int i=0; i<r; i++){
        swap(R[S[X[i]]],R[S[Y[i]]]);
        swap(S[X[i]],S[Y[i]]);
        if(p<(int)mv.size()){
            P[p]=R[mv[p].F];
            Q[p]=R[mv[p].S];
            p++;
        }else{
            P[p]=Q[p]=0;
            p++;
        }
    }
	return r;
}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...