# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
636041 | shafinalam | Sorting (IOI15_sorting) | C++14 | 463 ms | 27504 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 n , m , a [200005] , aa [200005] , x [600005] , y [600005] , dis [200005] , wrdis [200005] , wra [200005] , P [600005] , Q [600005];
bool ck ( int xx )
{
for ( int i = 1 ; i <= n ; i ++ )
{
a [i] = aa [i];
wra [ a [i] ] = i;
}
int mm = xx;
for ( int i = 1 ; i <= n ; i ++ )
{
dis [i] = i;
wrdis [ dis [i] ] = i;
}
for ( int j = mm - 1 ; j >= 0 ; j -- )
{
swap ( dis [ x [j] ] , dis [ y [j] ] );
swap ( wrdis [ dis [ x [j] ] ] , wrdis [ dis [ y [j] ] ] );
}
deque < int > s;
for ( int i = 1 ; i <= n ; i ++ )
{
if ( dis [i] != a [i] ) s . push_back ( dis [i] );
}
for ( int i = 1 ; i <= mm ; i ++ )
{
swap ( a [ x [ i - 1 ] ] , a [ y [ i - 1 ] ] );
swap ( wra [ a [ x [ i - 1 ] ] ] , wra [ a [ y [ i - 1 ] ] ] );
swap ( dis [ x [ i - 1 ] ] , dis [ y [ i - 1 ] ] );
swap ( wrdis [ dis [ x [ i - 1 ] ] ] , wrdis [ dis [ y [ i - 1 ] ] ] );
while ( s . size () )
{
int x = * s . begin () , z = wra [x];
s . pop_front ();
if ( dis [ wrdis [x] ] == a [ wrdis [x] ] ) continue;
swap ( a [ wrdis [x] ] , a [z] );
swap ( wra [ a [ wrdis [x] ] ] , wra [ a [z] ] );
break;
}
}
int f = 0;
for ( int i = 1 ; i <= n ; i ++ )
{
if ( a [i] != i ) f = 1;
}
if ( f == 0 ) return 1;
return 0;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n = N;
for ( int i = 1 ; i <= n ; i ++ )
{
a [i] = S [ i - 1 ];
a [i] ++;
aa [i] = a [i];
}
m = M;
for ( int i = 0 ; i < m ; i ++ )
{
x [i] = X [i] , y [i] = Y [i];
x [i] ++ , y [i] ++;
}
int l = 0 , rr = m + 1 , mid;
while ( l < rr )
{
mid = ( l + rr ) / 2;
if ( ck ( mid ) ) rr = mid;
else l = mid + 1;
}
m = l;
for ( int i = 1 ; i <= n ; i ++ )
{
a [i] = aa [i];
wra [ a [i] ] = i;
}
for ( int i = 1 ; i <= n ; i ++ )
{
dis [i] = i;
wrdis [i] = i;
}
for ( int j = m - 1 ; j >= 0 ; j -- )
{
swap ( dis [ x [j] ] , dis [ y [j] ] );
swap ( wrdis [ dis [ x [j] ] ] , wrdis [ dis [ y [j] ] ] );
}
deque < int > s;
for ( int i = 1 ; i <= n ; i ++ )
{
if ( dis [i] != a [i] ) s . push_back ( dis [i] );
}
int r = 0;
for ( int i = 1 ; i <= m ; i ++ )
{
swap ( a [ x [ i - 1 ] ] , a [ y [ i - 1 ] ] );
swap ( wra [ a [ x [ i - 1 ] ] ] , wra [ a [ y [ i - 1 ] ] ] );
swap ( dis [ x [ i - 1 ] ] , dis [ y [ i - 1 ] ] );
swap ( wrdis [ dis [ x [ i - 1 ] ] ] , wrdis [ dis [ y [ i - 1 ] ] ] );
P [ i - 1 ] = -1 , Q [ i - 1 ] = -1;
while ( s . size () )
{
int x = * s . begin () , z = wra [x];
s . pop_front ();
if ( dis [ wrdis [x] ] == a [ wrdis [x] ] ) continue;
swap ( a [ wrdis [x] ] , a [z] );
swap ( wra [ a [ wrdis [x] ] ] , wra [ a [z] ] );
P [ i - 1 ] = wrdis [x] - 1 , Q [ i - 1 ] = z - 1;
r ++;
break;
}
if ( P [ i - 1 ] == -1 )
{
P [ i - 1 ] = 0;
Q [ i - 1 ] = 0;
r ++;
}
}
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... |