#include <bits/stdc++.h>
#include "sorting.h"
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int NN = 2e5 + 10;
const int MM = 6e5 + 10;
int n, m, s[NN], x[MM], y[MM];
vector<pii> chk( int mid ) {
int pos[NN], te[NN];
vector<pii> ret;
for( int i = 0 ; i < n ; i++ ) te[i] = s[i];
for( int i = 0 ; i < mid ; i++ ) swap( te[x[i]], te[y[i]] );
for( int i = 0 ; i < n ; i++ ) pos[te[i]] = i;
for( int i = 0 ; i < n ; i++ ) if( te[i] != i ) {
ret.emplace_back( pii( te[i], i ) );
int now = te[i];
swap( te[i], te[pos[i]] );
swap( pos[now], pos[i] );
}
return ret;
}
int findSwapPairs( int N, int S[], int M, int X[], int Y[], int p[], int q[] ) {
int pos[NN];
n = N, m = M;
for( int i = 0 ; i < n ; i++ ) s[i] = S[i];
for( int i = 0 ; i < m ; i++ ) x[i] = X[i], y[i] = Y[i];
int l = 0, r = m;
while( l < r ) {
int mid = ( l + r ) >> 1;
if( chk( mid ).size() <= mid ) r = mid;
else l = mid + 1;
}
vector<pii> ans = chk( l );
for( int i = 0 ; i < n ; i++ ) pos[s[i]] = i;
for( int i = 0 ; i < r ; i++ ) {
swap( pos[s[x[i]]], pos[s[y[i]]] );
swap( s[x[i]], s[y[i]] );
if( i < ans.size() ) {
int a = ans[i].x, b = ans[i].y;
p[i] = pos[a], q[i] = pos[b];
swap( s[pos[a]], s[pos[b]] );
swap( pos[a], pos[b] );
}
}
for( int i = ans.size() ; i < r ; i++ ) p[i] = 0, q[i] = 0;
return l;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:37:31: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if( chk( mid ).size() <= mid ) r = mid;
~~~~~~~~~~~~~~~~~~^~~~~~
sorting.cpp:45:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if( i < ans.size() ) {
~~^~~~~~~~~~~~
sorting.cpp:52:26: warning: conversion to 'int' from 'std::vector<std::pair<int, int> >::size_type {aka long unsigned int}' may alter its value [-Wconversion]
for( int i = ans.size() ; i < r ; i++ ) p[i] = 0, q[i] = 0;
~~~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
0 ms |
508 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
0 ms |
508 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
3 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
380 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
3 ms |
760 KB |
Output is correct |
22 |
Correct |
3 ms |
760 KB |
Output is correct |
23 |
Correct |
4 ms |
760 KB |
Output is correct |
24 |
Correct |
3 ms |
760 KB |
Output is correct |
25 |
Correct |
3 ms |
760 KB |
Output is correct |
26 |
Correct |
3 ms |
760 KB |
Output is correct |
27 |
Correct |
3 ms |
764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
632 KB |
Output is correct |
3 |
Correct |
3 ms |
636 KB |
Output is correct |
4 |
Correct |
3 ms |
632 KB |
Output is correct |
5 |
Correct |
3 ms |
632 KB |
Output is correct |
6 |
Correct |
3 ms |
632 KB |
Output is correct |
7 |
Correct |
3 ms |
632 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
3 ms |
632 KB |
Output is correct |
10 |
Correct |
4 ms |
632 KB |
Output is correct |
11 |
Correct |
4 ms |
632 KB |
Output is correct |
12 |
Correct |
4 ms |
632 KB |
Output is correct |
13 |
Correct |
4 ms |
632 KB |
Output is correct |
14 |
Correct |
3 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
632 KB |
Output is correct |
3 |
Correct |
3 ms |
636 KB |
Output is correct |
4 |
Correct |
3 ms |
632 KB |
Output is correct |
5 |
Correct |
3 ms |
632 KB |
Output is correct |
6 |
Correct |
3 ms |
632 KB |
Output is correct |
7 |
Correct |
3 ms |
632 KB |
Output is correct |
8 |
Correct |
4 ms |
632 KB |
Output is correct |
9 |
Correct |
3 ms |
632 KB |
Output is correct |
10 |
Correct |
4 ms |
632 KB |
Output is correct |
11 |
Correct |
4 ms |
632 KB |
Output is correct |
12 |
Correct |
4 ms |
632 KB |
Output is correct |
13 |
Correct |
4 ms |
632 KB |
Output is correct |
14 |
Correct |
3 ms |
632 KB |
Output is correct |
15 |
Correct |
207 ms |
24572 KB |
Output is correct |
16 |
Correct |
217 ms |
25296 KB |
Output is correct |
17 |
Correct |
246 ms |
26380 KB |
Output is correct |
18 |
Correct |
87 ms |
23056 KB |
Output is correct |
19 |
Correct |
156 ms |
25708 KB |
Output is correct |
20 |
Correct |
160 ms |
26556 KB |
Output is correct |
21 |
Correct |
162 ms |
26544 KB |
Output is correct |
22 |
Correct |
199 ms |
21844 KB |
Output is correct |
23 |
Correct |
281 ms |
27436 KB |
Output is correct |
24 |
Correct |
295 ms |
26988 KB |
Output is correct |
25 |
Correct |
255 ms |
26676 KB |
Output is correct |
26 |
Correct |
170 ms |
25084 KB |
Output is correct |
27 |
Correct |
160 ms |
24368 KB |
Output is correct |
28 |
Correct |
384 ms |
26968 KB |
Output is correct |
29 |
Correct |
223 ms |
26300 KB |
Output is correct |
30 |
Correct |
127 ms |
23460 KB |
Output is correct |
31 |
Correct |
228 ms |
26844 KB |
Output is correct |
32 |
Correct |
222 ms |
26548 KB |
Output is correct |
33 |
Correct |
246 ms |
26884 KB |
Output is correct |
34 |
Correct |
232 ms |
26900 KB |
Output is correct |
35 |
Correct |
159 ms |
25500 KB |
Output is correct |
36 |
Correct |
68 ms |
22136 KB |
Output is correct |
37 |
Correct |
258 ms |
27752 KB |
Output is correct |
38 |
Correct |
249 ms |
26672 KB |
Output is correct |
39 |
Correct |
251 ms |
26756 KB |
Output is correct |