# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
207250 | oscarsierra12 | Sorting (IOI15_sorting) | C++14 | 1008 ms | 19952 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 ff first
#define ss second
#define pb push_back
using namespace std ;
typedef long long ll ;
typedef pair<int,int> ii ;
const int n = 200010 ;
int prv[n], nxt[n] ;
int ind[n], ele[n] ;
int rind[n] ;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
for ( int i = 0 ; i < N ; ++i )
rind[S[i]] = i ;
for ( int i = 0 ; i <= M ; ++i ) {
for ( int j = 0 ; j < N ; ++j ) prv[j] = nxt[j] = j ;
for ( int j = 0 ; j < i ; ++j ) {
int A = prv[X[j]] ;
int B = prv[Y[j]] ;
nxt[A] = Y[j] ;
prv[Y[j]] = A ;
nxt[B] = X[j] ;
prv[X[j]] = B ;
}
for ( int j = 0 ; j < N ; ++j ) {
ele[j] = S[prv[j]] ;
ind[S[prv[j]]] = j ;
}
vector <ii> swaps ;
for ( int j = 0 ; j < N ; ++j ) {
if ( ele[j] == j ) continue ;
swaps.pb ({rind[ele[j]], rind[ele[ind[j]]]}) ;
ind[ele[j]] = ind[j] ;
swap (ele[j], ele[ind[j]]) ;
}
if ( swaps.size() % 2 != i % 2 ) swaps.pb ({0,0}) ;
for ( int j = 0 ; j < N ; ++j ) prv[j] = nxt[j] = j ;
int id = 0 ;
for ( int j = 0 ; j < i ; ++j ) {
int A = prv[X[j]] ;
int B = prv[Y[j]] ;
nxt[A] = Y[j] ;
prv[Y[j]] = A ;
nxt[B] = X[j] ;
prv[X[j]] = B ;
if ( id < swaps.size() ) {
P[j] = nxt[swaps[id].ff] ;
Q[j] = nxt[swaps[id].ss] ;
}
else {
P[j] = X[j] ;
Q[j] = Y[j] ;
}
A = prv[P[j]] ;
B = prv[Q[j]] ;
nxt[A] = Q[j] ;
prv[Q[j]] = A ;
nxt[B] = P[j] ;
prv[P[j]] = B ;
id++ ;
}
if ( swaps.size() <= i ) return i ;
}
}
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... |