#include<bits/stdc++.h>
#define STIZE(x) fprintf(stderr, "STIZE%d\n", x);
#define PRINT(x) fprintf(stderr, "%s = %d\n", #x, x);
#define NL(x) printf("%c", " \n"[(x)]);
#define lld long long
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define mid (l+r)/2
#define endl '\n'
#define all(a) begin(a),end(a)
#define sz(a) int((a).size())
#define LINF 1000000000000000LL
#define INF 1000000000
#define EPS 1e-9
using namespace std;
#define MAXN 200010
int n, s[MAXN], m, x[3*MAXN], y[3*MAXN], p[3*MAXN], q[3*MAXN], idxx, idx[MAXN];
bool pos[MAXN];
void DFS(int node) {
pos[node] = 1;
if(!pos[s[node]]) DFS(s[node]);
}
void DFS1(int node) {
pos[node] = 1;
if(!pos[s[node]]) {
p[idxx] = node;
q[idxx] = s[node];
idxx++;
DFS1(s[node]);
}
}
bool check(int key) {
for(int i = 0; i < key; i++) swap(s[x[i]], s[y[i]]);
for(int i = 0; i < n; i++) pos[i] = 0;
int cnt = 0;
for(int i = 0; i < n; i++) {
if(!pos[i]) DFS(i), cnt++;
}
return n-cnt <= key;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n = N, m = M; int rez = M, l = 1, r = M;
bool sorted = 1;
for(int i = 0; i < N; i++) if(S[i] != i) sorted = 0;
for(int i = 0; i < M; i++) x[i] = X[i], y[i] = Y[i];
if(sorted) return 0;
for(int i = 0; i < N; i++) s[i] = S[i], idx[s[i]] = i;
while(l <= r) {
if(check(mid)) rez = mid, r = mid-1;
else l = mid+1;
for(int i = 0; i < n; i++) s[i] = S[i];
}
for(int i = 0; i < rez; i++) swap(s[X[i]], s[Y[i]]);
for(int j = 0; j < N; j++) pos[j] = 0;
for(int j = 0; j < N; j++) {
if(!pos[j]) DFS1(j);
}
for(int j = 0; j < idxx; j++) P[j] = p[j], Q[j] = q[j];
for(int j = idxx; j < rez; j++) P[j] = 0, Q[j] = 0;
for(int i = 0; i < rez; i++) {
swap(S[X[i]], S[Y[i]]);
swap(idx[S[X[i]]], idx[S[Y[i]]]);
P[i] = idx[s[P[i]]], Q[i] = idx[s[Q[i]]];
swap(S[P[i]], S[Q[i]]);
swap(idx[S[P[i]]], idx[S[Q[i]]]);
}
return rez;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
432 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
432 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
3 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
2 ms |
640 KB |
Output is correct |
25 |
Correct |
2 ms |
640 KB |
Output is correct |
26 |
Correct |
3 ms |
640 KB |
Output is correct |
27 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
512 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
512 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
15 |
Correct |
163 ms |
15216 KB |
Output is correct |
16 |
Correct |
188 ms |
22668 KB |
Output is correct |
17 |
Correct |
208 ms |
23032 KB |
Output is correct |
18 |
Correct |
39 ms |
15992 KB |
Output is correct |
19 |
Correct |
143 ms |
20944 KB |
Output is correct |
20 |
Correct |
154 ms |
21368 KB |
Output is correct |
21 |
Correct |
146 ms |
21496 KB |
Output is correct |
22 |
Correct |
151 ms |
20088 KB |
Output is correct |
23 |
Correct |
181 ms |
23288 KB |
Output is correct |
24 |
Correct |
194 ms |
23160 KB |
Output is correct |
25 |
Correct |
207 ms |
23388 KB |
Output is correct |
26 |
Correct |
153 ms |
21240 KB |
Output is correct |
27 |
Correct |
134 ms |
20728 KB |
Output is correct |
28 |
Correct |
196 ms |
22776 KB |
Output is correct |
29 |
Correct |
201 ms |
22904 KB |
Output is correct |
30 |
Correct |
96 ms |
19832 KB |
Output is correct |
31 |
Correct |
187 ms |
23084 KB |
Output is correct |
32 |
Correct |
174 ms |
22528 KB |
Output is correct |
33 |
Correct |
207 ms |
23260 KB |
Output is correct |
34 |
Correct |
187 ms |
23112 KB |
Output is correct |
35 |
Correct |
144 ms |
20808 KB |
Output is correct |
36 |
Correct |
97 ms |
18432 KB |
Output is correct |
37 |
Correct |
208 ms |
24184 KB |
Output is correct |
38 |
Correct |
211 ms |
23148 KB |
Output is correct |
39 |
Correct |
201 ms |
23544 KB |
Output is correct |