//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r){
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 200005
#define mod 1000000007
#define pii pair<int, int>
#define ff first
#define ss second
#include "sorting.h"
int findSwapPairs(int n, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int low = -1, up = M;
vector<int> pos(n), rev(n);
auto getp = [&] (int k) {
vector<int> ret(n);
vector<int> a(n);
for (int i = 0;i < n;i++) a[i] = i;
//[0, mid)
for (int i = k-1;i >= 0;i--) {
swap(a[X[i]], a[Y[i]]);
}
for (int i = 0;i < n;i++) {
pos[a[i]] = i;
rev[i] = a[i];
ret[a[i]] = S[i];
}
return ret;
};
while (low < up - 1) {
int mid = (low + up) / 2;
vector<int> v = getp(mid);
vector<bool> vis(n, 0);
int num = n;
for (int i = 0;i < n;i++) {
if (vis[i]) continue;
int cur = i;
do {
vis[cur] = 1;
cur = v[cur];
} while (!vis[cur]);
num--;
}
if (num <= mid) up = mid;
else low = mid;
}
vector<int> v = getp(up);
vector<bool> vis(n, 0);
vector<pii> sw;
for (int i = 0;i < n;i++) {
if (vis[i]) continue;
int cur = i;
do {
vis[cur] = 1;
if (!vis[v[cur]]) sw.push_back({cur, v[cur]});
cur = v[cur];
} while (!vis[cur]);
}
reverse(sw.begin(), sw.end());
for (int i = 0;i < up;i++) {
int vx = rev[X[i]], vy = rev[Y[i]];
swap(rev[X[i]], rev[Y[i]]);
swap(pos[vx], pos[vy]);
if (i < sw.size()) {
P[i] = pos[sw[i].ff];
Q[i] = pos[sw[i].ss];
} else {
P[i] = Q[i] = 0;
}
}
return up;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:77:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | if (i < sw.size()) {
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
336 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
147 ms |
11044 KB |
Output is correct |
16 |
Correct |
153 ms |
19044 KB |
Output is correct |
17 |
Correct |
173 ms |
20244 KB |
Output is correct |
18 |
Correct |
94 ms |
16276 KB |
Output is correct |
19 |
Correct |
149 ms |
18968 KB |
Output is correct |
20 |
Correct |
156 ms |
19668 KB |
Output is correct |
21 |
Correct |
152 ms |
19584 KB |
Output is correct |
22 |
Correct |
143 ms |
15292 KB |
Output is correct |
23 |
Correct |
159 ms |
20816 KB |
Output is correct |
24 |
Correct |
174 ms |
20556 KB |
Output is correct |
25 |
Correct |
176 ms |
20308 KB |
Output is correct |
26 |
Correct |
166 ms |
19456 KB |
Output is correct |
27 |
Correct |
140 ms |
18952 KB |
Output is correct |
28 |
Correct |
178 ms |
20320 KB |
Output is correct |
29 |
Correct |
168 ms |
19876 KB |
Output is correct |
30 |
Correct |
119 ms |
18012 KB |
Output is correct |
31 |
Correct |
170 ms |
20308 KB |
Output is correct |
32 |
Correct |
164 ms |
20012 KB |
Output is correct |
33 |
Correct |
179 ms |
20420 KB |
Output is correct |
34 |
Correct |
170 ms |
20428 KB |
Output is correct |
35 |
Correct |
147 ms |
18776 KB |
Output is correct |
36 |
Correct |
89 ms |
17540 KB |
Output is correct |
37 |
Correct |
189 ms |
21112 KB |
Output is correct |
38 |
Correct |
169 ms |
20272 KB |
Output is correct |
39 |
Correct |
185 ms |
20376 KB |
Output is correct |