#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
int n, s[200001], m, x[600001], y[600001];
bool f(ll xf){
int s1[200001];
for(int i=0; i<n; i++){
s1[i]=s[i];
}
int c=0;
for(int i=0; i<xf; i++){
swap(s1[x[i]], s1[y[i]]);
}
for(int i=0; i<n; i++){
while(s1[i]!=i){
c++;
swap(s1[i], s1[s1[i]]);
}
}
return (c<=xf);
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n=N;
m=M;
bool b=1;
for(int i=0; i<n; i++){
s[i]=S[i];
if(S[i]!=i)b=0;
}
if(b)return 0;
for(int i=0; i<n; i++){
x[i]=X[i];
y[i]=Y[i];
}
int l=0, r=n+1, mid=0;
while(l+1<r){
mid=(l+r)/2;
if(f(mid)){
r=mid;
}
else l=mid;
}
int c=0;
l++;
// cout << l << "\n";
int s1[n], p[n];
for(int i=0; i<n; i++){
s1[i]=i;
p[i]=i;
}
for(int i=0; i<l; i++){
swap(S[x[i]], S[y[i]]);
swap(s1[x[i]], s1[y[i]]);
swap(p[s1[x[i]]], p[s1[y[i]]]);
}
for(int i=0; i<n; i++){
while(S[i]!=i){
swap(s1[p[x[c]]], s1[p[y[c]]]);
swap(p[s1[p[x[c]]]], p[s1[p[y[c]]]]);
P[c]=s1[i];
Q[c]=s1[S[i]];
//cout << P[c] << " " << Q[c] << "\n";
// for(int t=0; t<n; t++){
// cout << s1[t] << " ";
// }
// cout << "\n";
c++;
swap(S[i], S[S[i]]);
}
}
for(int i=c; i<l; i++){
P[i]=0;
Q[i]=0;
}
return l;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1100 KB |
Output is correct |
12 |
Correct |
2 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
1 ms |
1100 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
2 ms |
1100 KB |
Output is correct |
22 |
Correct |
3 ms |
1228 KB |
Output is correct |
23 |
Correct |
2 ms |
1228 KB |
Output is correct |
24 |
Correct |
2 ms |
1228 KB |
Output is correct |
25 |
Correct |
2 ms |
1228 KB |
Output is correct |
26 |
Correct |
2 ms |
1228 KB |
Output is correct |
27 |
Correct |
2 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1100 KB |
Output is correct |
2 |
Correct |
2 ms |
1228 KB |
Output is correct |
3 |
Correct |
2 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
1228 KB |
Output is correct |
6 |
Correct |
2 ms |
1228 KB |
Output is correct |
7 |
Correct |
2 ms |
1228 KB |
Output is correct |
8 |
Correct |
2 ms |
1232 KB |
Output is correct |
9 |
Correct |
2 ms |
1228 KB |
Output is correct |
10 |
Correct |
3 ms |
1228 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1228 KB |
Output is correct |
13 |
Correct |
2 ms |
1208 KB |
Output is correct |
14 |
Correct |
2 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1100 KB |
Output is correct |
2 |
Correct |
2 ms |
1228 KB |
Output is correct |
3 |
Correct |
2 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
1228 KB |
Output is correct |
6 |
Correct |
2 ms |
1228 KB |
Output is correct |
7 |
Correct |
2 ms |
1228 KB |
Output is correct |
8 |
Correct |
2 ms |
1232 KB |
Output is correct |
9 |
Correct |
2 ms |
1228 KB |
Output is correct |
10 |
Correct |
3 ms |
1228 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
1228 KB |
Output is correct |
13 |
Correct |
2 ms |
1208 KB |
Output is correct |
14 |
Correct |
2 ms |
1228 KB |
Output is correct |
15 |
Correct |
153 ms |
19148 KB |
Output is correct |
16 |
Correct |
141 ms |
19524 KB |
Output is correct |
17 |
Correct |
168 ms |
21116 KB |
Output is correct |
18 |
Correct |
44 ms |
14028 KB |
Output is correct |
19 |
Correct |
110 ms |
19836 KB |
Output is correct |
20 |
Correct |
121 ms |
20620 KB |
Output is correct |
21 |
Correct |
122 ms |
20548 KB |
Output is correct |
22 |
Correct |
118 ms |
15936 KB |
Output is correct |
23 |
Correct |
148 ms |
21444 KB |
Output is correct |
24 |
Correct |
160 ms |
21444 KB |
Output is correct |
25 |
Correct |
164 ms |
21512 KB |
Output is correct |
26 |
Correct |
121 ms |
20428 KB |
Output is correct |
27 |
Correct |
105 ms |
19704 KB |
Output is correct |
28 |
Correct |
154 ms |
21088 KB |
Output is correct |
29 |
Correct |
155 ms |
21060 KB |
Output is correct |
30 |
Correct |
81 ms |
19172 KB |
Output is correct |
31 |
Correct |
157 ms |
21552 KB |
Output is correct |
32 |
Correct |
147 ms |
20608 KB |
Output is correct |
33 |
Correct |
163 ms |
21572 KB |
Output is correct |
34 |
Correct |
150 ms |
21296 KB |
Output is correct |
35 |
Correct |
113 ms |
19632 KB |
Output is correct |
36 |
Correct |
47 ms |
18180 KB |
Output is correct |
37 |
Correct |
172 ms |
22516 KB |
Output is correct |
38 |
Correct |
162 ms |
21440 KB |
Output is correct |
39 |
Correct |
168 ms |
21960 KB |
Output is correct |