#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
int findSwapPairs(int n, int s[], int m, int X[], int Y[], int P[], int Q[]) {
auto sorted = [&] (vector<int>&v) -> bool {
for(int i = 0 ; i < n ; i++)
if(v[i] != i)
return 0;
return 1;
};
for(int T = 1 ; T <= m ; T++) {
//cout << "T : " << T << '\n';
vector<int>c(n);
for(int i = 0 ; i < n ; i++) c[i]= i;
for(int i = 0 ; i < T ; i++)
swap(c[X[i]], c[Y[i]]);
vector<int>go(n, -1);
for(int i = 0 ; i < n ; i++) {
//assert(go[c[i]] == -1);
go[c[i]] = i;
}
vector<int>v(n);
auto S = [&] (int W) {
for(int i = 0 ; i < n ; i++)
if(v[i] == W)
return i;
//assert(0);
};
for(int i = 0 ; i < n ; i++) v[i] = s[i];
int cnt = 0;
for(int i = 0 ; i < T ; i++) {
if(sorted(v))
return cnt;
cnt++;
swap(v[X[i]], v[Y[i]]);
swap(go[X[i]], go[Y[i]]);
int j;
for(j = 0 ; j < n ; j++) {
if(go[j] != v[j]) {
int p = S(go[j]);
P[cnt-1] = p;
Q[cnt-1] = j;
swap(v[p], v[j]);
break;
}
}
/*cout << " " << i << '\n';
cout << " ";
for(auto z : v)
cout << z << ' ';
cout << '\n';*/
if(j == n)
P[cnt-1] = 0, Q[cnt-1] = 0;
// do the swappings
// look for first element not correct in go
}
if(sorted(v))
return cnt;
}
assert(0);
return 0;
}
Compilation message
sorting.cpp: In lambda function:
sorting.cpp:29:9: warning: control reaches end of non-void function [-Wreturn-type]
29 | };
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
232 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 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 |
232 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 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 |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
1 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 |
232 KB |
Output is correct |
3 |
Correct |
1 ms |
300 KB |
Output is correct |
4 |
Correct |
1 ms |
296 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 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 |
300 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
308 KB |
Output is correct |
18 |
Correct |
1 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 |
30 ms |
436 KB |
Output is correct |
22 |
Correct |
35 ms |
516 KB |
Output is correct |
23 |
Correct |
30 ms |
468 KB |
Output is correct |
24 |
Correct |
23 ms |
428 KB |
Output is correct |
25 |
Correct |
6 ms |
468 KB |
Output is correct |
26 |
Correct |
16 ms |
468 KB |
Output is correct |
27 |
Correct |
43 ms |
536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1089 ms |
428 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1089 ms |
428 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |