#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;
int n,m;
pair<int,int> brr[600001];
int ori[200001],arr[200001],pos[200001];
vector<pair<int,int> > val;
bool works(int k){
val.clear();
for (int i=0;i<n;i++) arr[i]=ori[i];
for (int i=0;i<k;i++) swap(arr[brr[i].first],arr[brr[i].second]);
for (int i=0;i<n;i++){
pos[arr[i]]=i;
}
for (int i=0;i<n;i++){
if (arr[i]!=i){
int one=i;
int two=pos[i];
swap(arr[one],arr[two]);
val.push_back(make_pair(arr[one],arr[two]));
pos[i]=i;
pos[arr[two]]=two;
}
}
if (val.size()>k) return false;
while (val.size()<k) val.push_back(make_pair(0,0));
return true;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n=N;
m=M;
for (int i=0;i<n;i++) ori[i]=S[i];
for (int i=0;i<m;i++) brr[i]=make_pair(X[i],Y[i]);
int mini=-1,maxi=m;
while (mini+1<maxi){
int mid=(mini+maxi)/2;
if (works(mid)) maxi=mid;
else mini=mid;
}
works(maxi);
for (int i=0;i<n;i++) arr[i]=ori[i];
for (int i=0;i<n;i++) pos[arr[i]]=i;
for (int i=0;i<maxi;i++){
int a=X[i];
int b=Y[i];
swap(arr[a],arr[b]);
swap(pos[arr[a]],pos[arr[b]]);
int one=val[i].first;
int two=val[i].second;
a=pos[one];
b=pos[two];
P[i]=a;
Q[i]=b;
swap(pos[arr[a]],pos[arr[b]]);
swap(arr[a],arr[b]);
}
return maxi;
}
Compilation message
sorting.cpp: In function 'bool works(int)':
sorting.cpp:25:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (val.size()>k) return false;
~~~~~~~~~~^~
sorting.cpp:26:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (val.size()<k) val.push_back(make_pair(0,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 |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 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 |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 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 |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
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 |
408 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
5 |
Correct |
2 ms |
376 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 |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 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 |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 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 |
408 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
376 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 |
888 KB |
Output is correct |
22 |
Correct |
3 ms |
760 KB |
Output is correct |
23 |
Correct |
3 ms |
888 KB |
Output is correct |
24 |
Correct |
3 ms |
756 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 |
888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
632 KB |
Output is correct |
3 |
Correct |
3 ms |
632 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 |
4 ms |
632 KB |
Output is correct |
8 |
Correct |
3 ms |
632 KB |
Output is correct |
9 |
Correct |
4 ms |
632 KB |
Output is correct |
10 |
Correct |
3 ms |
632 KB |
Output is correct |
11 |
Correct |
3 ms |
632 KB |
Output is correct |
12 |
Correct |
3 ms |
632 KB |
Output is correct |
13 |
Correct |
3 ms |
632 KB |
Output is correct |
14 |
Correct |
0 ms |
636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
632 KB |
Output is correct |
3 |
Correct |
3 ms |
632 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 |
4 ms |
632 KB |
Output is correct |
8 |
Correct |
3 ms |
632 KB |
Output is correct |
9 |
Correct |
4 ms |
632 KB |
Output is correct |
10 |
Correct |
3 ms |
632 KB |
Output is correct |
11 |
Correct |
3 ms |
632 KB |
Output is correct |
12 |
Correct |
3 ms |
632 KB |
Output is correct |
13 |
Correct |
3 ms |
632 KB |
Output is correct |
14 |
Correct |
0 ms |
636 KB |
Output is correct |
15 |
Correct |
238 ms |
25060 KB |
Output is correct |
16 |
Correct |
260 ms |
25568 KB |
Output is correct |
17 |
Correct |
319 ms |
27024 KB |
Output is correct |
18 |
Correct |
92 ms |
24168 KB |
Output is correct |
19 |
Correct |
183 ms |
25448 KB |
Output is correct |
20 |
Correct |
224 ms |
26272 KB |
Output is correct |
21 |
Correct |
202 ms |
26420 KB |
Output is correct |
22 |
Correct |
270 ms |
22368 KB |
Output is correct |
23 |
Correct |
273 ms |
28108 KB |
Output is correct |
24 |
Correct |
315 ms |
27588 KB |
Output is correct |
25 |
Correct |
305 ms |
27296 KB |
Output is correct |
26 |
Correct |
205 ms |
26224 KB |
Output is correct |
27 |
Correct |
190 ms |
25576 KB |
Output is correct |
28 |
Correct |
309 ms |
27360 KB |
Output is correct |
29 |
Correct |
282 ms |
26724 KB |
Output is correct |
30 |
Correct |
140 ms |
25576 KB |
Output is correct |
31 |
Correct |
281 ms |
27360 KB |
Output is correct |
32 |
Correct |
275 ms |
26976 KB |
Output is correct |
33 |
Correct |
337 ms |
27532 KB |
Output is correct |
34 |
Correct |
293 ms |
27488 KB |
Output is correct |
35 |
Correct |
190 ms |
25184 KB |
Output is correct |
36 |
Correct |
75 ms |
25576 KB |
Output is correct |
37 |
Correct |
337 ms |
28352 KB |
Output is correct |
38 |
Correct |
304 ms |
27216 KB |
Output is correct |
39 |
Correct |
305 ms |
27272 KB |
Output is correct |