#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;
int n , m , a [2003] , aa [2003] , x [20004] , y [20004] , dis [2003][20004];
bool ck ( int xx )
{
for ( int i = 1 ; i <= n ; i ++ ) a [i] = aa [i];
int mm = xx;
for ( int i = 1 ; i <= n ; i ++ ) dis [i][mm] = i;
for ( int j = mm - 1 ; j >= 0 ; j -- )
{
for ( int i = 1 ; i <= n ; i ++ ) dis [i][j] = dis [i][ j + 1 ];
swap ( dis [ x [j] ][j] , dis [ y [j] ][j] );
}
for ( int i = 1 ; i <= mm ; i ++ )
{
int f = 0;
for ( int i = 1 ; i <= n ; i ++ )
{
if ( a [i] != i ) f = 1;
}
if ( f == 0 ) return 1;
swap ( a [ x [ i - 1 ] ] , a [ y [ i - 1 ] ] );
for ( int j = 1 ; j <= n ; j ++ )
{
if ( dis [j][i] != a [j] )
{
int wr;
for ( int z = 1 ; z <= n ; z ++ )
{
if ( a [z] == dis [j][i] )
{
wr = z;
break;
}
}
swap ( a [j] , a [wr] );
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 , r = m + 1 , mid;
while ( l < r )
{
mid = ( l + r ) / 2;
if ( ck ( mid ) ) r = mid;
else l = mid + 1;
}
m = l;
for ( int i = 1 ; i <= n ; i ++ ) a [i] = aa [i];
for ( int i = 1 ; i <= n ; i ++ ) dis [i][m] = i;
for ( int j = m - 1 ; j >= 0 ; j -- )
{
for ( int i = 1 ; i <= n ; i ++ ) dis [i][j] = dis [i][ j + 1 ];
swap ( dis [ x [j] ][j] , dis [ y [j] ][j] );
}
int rr = 0;
for ( int i = 1 ; i <= m ; i ++ )
{
int f = 0;
for ( int j = 1 ; j <= n ; j ++ )
{
if ( a [j] != j ) f = 1;
}
if ( f == 0 ) return rr;
swap ( a [ x [ i - 1 ] ] , a [ y [ i - 1 ] ] );
P [ i - 1 ] = -1 , Q [ i - 1 ] = -1;
for ( int j = 1 ; j <= n ; j ++ )
{
if ( dis [j][i] != a [j] )
{
int wr;
for ( int z = 1 ; z <= n ; z ++ )
{
if ( a [z] == dis [j][i] )
{
wr = z;
break;
}
}
P [ i - 1 ] = j - 1 , Q [ i - 1 ] = wr - 1;
swap ( a [j] , a [wr] );
rr ++;
break;
}
}
if ( P [ i - 1 ] == -1 )
{
P [ i - 1 ] = 0;
Q [ i - 1 ] = 0;
rr ++;
}
}
return rr;
}
Compilation message
sorting.cpp: In function 'bool ck(int)':
sorting.cpp:20:19: warning: declaration of 'i' shadows a previous local [-Wshadow]
20 | for ( int i = 1 ; i <= n ; i ++ )
| ^
sorting.cpp:17:15: note: shadowed declaration is here
17 | for ( int i = 1 ; i <= mm ; i ++ )
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
2 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
1236 KB |
Output is correct |
4 |
Correct |
2 ms |
1364 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
1236 KB |
Output is correct |
11 |
Correct |
1 ms |
1364 KB |
Output is correct |
12 |
Correct |
2 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
1236 KB |
Output is correct |
16 |
Correct |
2 ms |
1364 KB |
Output is correct |
17 |
Correct |
1 ms |
1236 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
27 ms |
16108 KB |
Output is correct |
22 |
Correct |
26 ms |
15828 KB |
Output is correct |
23 |
Correct |
28 ms |
17044 KB |
Output is correct |
24 |
Correct |
25 ms |
16196 KB |
Output is correct |
25 |
Correct |
30 ms |
16964 KB |
Output is correct |
26 |
Correct |
28 ms |
15828 KB |
Output is correct |
27 |
Correct |
31 ms |
15700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
451 ms |
26868 KB |
Output is correct |
2 |
Correct |
593 ms |
30856 KB |
Output is correct |
3 |
Correct |
493 ms |
28292 KB |
Output is correct |
4 |
Correct |
107 ms |
30940 KB |
Output is correct |
5 |
Correct |
174 ms |
32028 KB |
Output is correct |
6 |
Correct |
209 ms |
30428 KB |
Output is correct |
7 |
Correct |
400 ms |
32180 KB |
Output is correct |
8 |
Correct |
582 ms |
29548 KB |
Output is correct |
9 |
Correct |
583 ms |
30344 KB |
Output is correct |
10 |
Correct |
581 ms |
30504 KB |
Output is correct |
11 |
Correct |
622 ms |
32140 KB |
Output is correct |
12 |
Correct |
352 ms |
31928 KB |
Output is correct |
13 |
Correct |
627 ms |
31144 KB |
Output is correct |
14 |
Correct |
111 ms |
29644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
451 ms |
26868 KB |
Output is correct |
2 |
Correct |
593 ms |
30856 KB |
Output is correct |
3 |
Correct |
493 ms |
28292 KB |
Output is correct |
4 |
Correct |
107 ms |
30940 KB |
Output is correct |
5 |
Correct |
174 ms |
32028 KB |
Output is correct |
6 |
Correct |
209 ms |
30428 KB |
Output is correct |
7 |
Correct |
400 ms |
32180 KB |
Output is correct |
8 |
Correct |
582 ms |
29548 KB |
Output is correct |
9 |
Correct |
583 ms |
30344 KB |
Output is correct |
10 |
Correct |
581 ms |
30504 KB |
Output is correct |
11 |
Correct |
622 ms |
32140 KB |
Output is correct |
12 |
Correct |
352 ms |
31928 KB |
Output is correct |
13 |
Correct |
627 ms |
31144 KB |
Output is correct |
14 |
Correct |
111 ms |
29644 KB |
Output is correct |
15 |
Runtime error |
47 ms |
17900 KB |
Execution killed with signal 11 |
16 |
Halted |
0 ms |
0 KB |
- |