#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
bool can(int K, int N, int S[], int M, int X[], int Y[], int P[], int Q[]){
assert(K <= M);
int s[N], si[N]; for(int i = 0; i < N; i++) s[i] = S[i];
for(int i = 0; i < K; i++) swap(s[X[i]], s[Y[i]]);
vector<pair<int, int>> V;
bool u[N] = {};
for(int i = 0; i < N; i++){
if(!u[i] && s[i] != i){
vector<int> v {i}; int x = s[i];
while(x != i) v.push_back(x), x = s[x];
for(auto i : v) u[i] = true;
for(int j = 1; j < (int) v.size(); j++) V.push_back({v[j - 1], v[j]});
}
}
if((int) V.size() > K) return false;
for(int i = 0; i < N; i++) s[i] = S[i], si[s[i]] = i;
for(int i = 0; i < K; i++){
swap(s[X[i]], s[Y[i]]); swap(si[s[X[i]]], si[s[Y[i]]]);
if(i < (int) V.size()){
int a = V[i].first, b = V[i].second;
P[i] = si[a], Q[i] = si[b]; swap(s[si[a]], s[si[b]]); swap(si[s[si[a]]], si[s[si[b]]]);
}else{
P[i] = Q[i] = 0;
}
}
return true;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int L = 0, R = M;
while(L != R){
int mid = (L + R) / 2;
if(can(mid, N, S, M, X, Y, P, Q)){
R = mid;
}else{
L = mid + 1;
}
}
can(L, N, S, M, X, Y, P, Q);
return L;
}
Compilation message
sorting.cpp: In function 'bool can(int, int, int*, int, int*, int*, int*, int*)':
sorting.cpp:16:13: warning: declaration of 'i' shadows a previous local [-Wshadow]
16 | for(auto i : v) u[i] = true;
| ^
sorting.cpp:12:10: note: shadowed declaration is here
12 | for(int i = 0; i < N; i++){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
0 ms |
304 KB |
Output is correct |
5 |
Correct |
0 ms |
300 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
0 ms |
304 KB |
Output is correct |
5 |
Correct |
0 ms |
300 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
304 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
312 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
0 ms |
304 KB |
Output is correct |
5 |
Correct |
0 ms |
300 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
304 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
312 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
440 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
500 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
436 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
440 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
500 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
444 KB |
Output is correct |
15 |
Correct |
179 ms |
20584 KB |
Output is correct |
16 |
Correct |
187 ms |
20900 KB |
Output is correct |
17 |
Correct |
202 ms |
22016 KB |
Output is correct |
18 |
Correct |
68 ms |
20444 KB |
Output is correct |
19 |
Correct |
168 ms |
21708 KB |
Output is correct |
20 |
Correct |
165 ms |
22140 KB |
Output is correct |
21 |
Correct |
161 ms |
22068 KB |
Output is correct |
22 |
Correct |
177 ms |
17184 KB |
Output is correct |
23 |
Correct |
226 ms |
23348 KB |
Output is correct |
24 |
Correct |
200 ms |
22468 KB |
Output is correct |
25 |
Correct |
197 ms |
22456 KB |
Output is correct |
26 |
Correct |
159 ms |
20524 KB |
Output is correct |
27 |
Correct |
139 ms |
20048 KB |
Output is correct |
28 |
Correct |
205 ms |
22352 KB |
Output is correct |
29 |
Correct |
211 ms |
21400 KB |
Output is correct |
30 |
Correct |
125 ms |
19488 KB |
Output is correct |
31 |
Correct |
221 ms |
21828 KB |
Output is correct |
32 |
Correct |
193 ms |
22456 KB |
Output is correct |
33 |
Correct |
205 ms |
22508 KB |
Output is correct |
34 |
Correct |
210 ms |
21684 KB |
Output is correct |
35 |
Correct |
160 ms |
21292 KB |
Output is correct |
36 |
Correct |
46 ms |
18380 KB |
Output is correct |
37 |
Correct |
207 ms |
23120 KB |
Output is correct |
38 |
Correct |
195 ms |
22248 KB |
Output is correct |
39 |
Correct |
200 ms |
22392 KB |
Output is correct |