#include <bits/stdc++.h>
using namespace std;
int N, M;
int pos[200005];
int S[200005], A[200005];
int X[600005], Y[600005];
int P[600005], Q[600005];
int SA[600005], SB[600005];
int res;
int pp;
bool qq;
void balance(int now) {
while(pp <= now) swap(S[X[pp]], S[Y[pp]]), pp++;
while(pp > now + 1) swap(S[X[pp - 1]], S[Y[pp - 1]]), pp--;
}
void SWAP(int i, int j) {
int ii = pos[A[i]];
int jj = pos[A[j]];
pos[A[i]] = jj;
pos[A[j]] = ii;
swap(A[i], A[j]);
}
bool trysort(int limit) {
int cnt = 0;
// cout << limit << "\n";
for(int i = 0; i < N; i++) {
A[i] = S[i];
// cout << A[i] << " ";
pos[A[i]] = i;
}
// cout << "\n";
for(int i = 0; i < N; i++) {
if(A[i] != i) {
SA[cnt] = i, SB[cnt] = A[i];
cnt++;
if(cnt > limit) return 0;
SWAP(i, pos[i]);
// if(qq)for(int j = 0; j < N; j++) cout << A[j] << " ";
// if(qq)cout << "\n";
}
}
for(int i = cnt; i < limit; i++) {
SA[i] = 0, SB[i] = 0;
}
// cout << cnt << "\n\n";
return cnt <= limit;
}
void findans() {
for(int i = res - 1; i >= 0; i--) {
P[i] = pos[SA[i]], Q[i] = pos[SB[i]];
SWAP(P[i], Q[i]);
SWAP(X[i + 1], Y[i + 1]);
}
}
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++) S[i] = s[i];
for(int i = 1; i <= M; i++) X[i] = x[i - 1], Y[i] = y[i - 1];
int l = 0, r = N;
while(l <= r) {
int mid = (l + r) >> 1;
balance(mid);
if(trysort(mid)) {
res = mid;
r = mid - 1;
}else l = mid + 1;
}
balance(res);
qq = 1;
trysort(res);
findans();
for(int i = 0; i < res; i++) {
p[i] = P[i], q[i] = Q[i];
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
4 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
640 KB |
Output is correct |
22 |
Correct |
5 ms |
640 KB |
Output is correct |
23 |
Correct |
5 ms |
640 KB |
Output is correct |
24 |
Correct |
5 ms |
640 KB |
Output is correct |
25 |
Correct |
5 ms |
640 KB |
Output is correct |
26 |
Correct |
5 ms |
640 KB |
Output is correct |
27 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
512 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
512 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
5 ms |
512 KB |
Output is correct |
13 |
Correct |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
137 ms |
17912 KB |
Output is correct |
16 |
Correct |
145 ms |
18296 KB |
Output is correct |
17 |
Correct |
161 ms |
19320 KB |
Output is correct |
18 |
Correct |
71 ms |
12664 KB |
Output is correct |
19 |
Correct |
119 ms |
16120 KB |
Output is correct |
20 |
Correct |
136 ms |
17016 KB |
Output is correct |
21 |
Correct |
137 ms |
17144 KB |
Output is correct |
22 |
Correct |
131 ms |
19576 KB |
Output is correct |
23 |
Correct |
157 ms |
19964 KB |
Output is correct |
24 |
Correct |
161 ms |
19704 KB |
Output is correct |
25 |
Correct |
161 ms |
19448 KB |
Output is correct |
26 |
Correct |
128 ms |
16632 KB |
Output is correct |
27 |
Correct |
123 ms |
15864 KB |
Output is correct |
28 |
Correct |
161 ms |
19432 KB |
Output is correct |
29 |
Correct |
157 ms |
18684 KB |
Output is correct |
30 |
Correct |
100 ms |
14968 KB |
Output is correct |
31 |
Correct |
160 ms |
19064 KB |
Output is correct |
32 |
Correct |
155 ms |
19320 KB |
Output is correct |
33 |
Correct |
167 ms |
19704 KB |
Output is correct |
34 |
Correct |
161 ms |
19704 KB |
Output is correct |
35 |
Correct |
125 ms |
15864 KB |
Output is correct |
36 |
Correct |
65 ms |
13688 KB |
Output is correct |
37 |
Correct |
169 ms |
20216 KB |
Output is correct |
38 |
Correct |
162 ms |
19448 KB |
Output is correct |
39 |
Correct |
153 ms |
19576 KB |
Output is correct |