#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;
int n,m;
vector<int> s,x,y;
vector<pair<int,int>> ans;
bool chk(int val,bool gen=false){
vector<pair<int,int>> res;
vector<int> id(n),pos(n),pos2(n),tar(n);
iota(id.begin(),id.end(),0);
iota(tar.begin(),tar.end(),0);
iota(pos2.begin(),pos2.end(),0);
for(int i=0;i<val;i++)swap(tar[x[i]],tar[y[i]]);
for(int i=0;i<n;i++)pos[s[i]]=i;
auto seq=s;
auto doswap=[&](int u,int v){
swap(seq[u],seq[v]);
swap(pos[seq[u]],pos[seq[v]]);
swap(id[u],id[v]);
swap(pos2[id[u]],pos2[id[v]]);
};
int cnt=0;
for(int i=0;i<n;i++){
if(id[pos[i]]!=tar[i]){
doswap(x[cnt],y[cnt]);
int u=pos[i],v=pos2[tar[i]];
res.emplace_back(u,v);
swap(seq[u],seq[v]);
swap(pos[seq[u]],pos[seq[v]]);
cnt++;
}
}
if(gen){
ans=res;
while(ans.size()<val)ans.emplace_back(0,0);
}
return res.size()<=val;
}
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.emplace_back(S[i]);
x.emplace_back(X[i]);
y.emplace_back(Y[i]);
}
int l=0,r=n-1;
while(l<r){
int mid=(l+r)/2;
auto seq=s;
if(chk(mid))r=mid;
else l=mid+1;
}
chk(l,true);
for(int i=0;i<l;i++)tie(P[i],Q[i])=ans[i];
return l;
}
Compilation message
sorting.cpp: In function 'bool chk(int, bool)':
sorting.cpp:38:19: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
38 | while(ans.size()<val)ans.emplace_back(0,0);
| ~~~~~~~~~~^~~~
sorting.cpp:40:19: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
40 | return res.size()<=val;
| ~~~~~~~~~~^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
296 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 |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
304 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 |
308 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
304 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 |
308 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
568 KB |
Output is correct |
23 |
Correct |
2 ms |
544 KB |
Output is correct |
24 |
Correct |
1 ms |
552 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
564 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
432 KB |
Output is correct |
7 |
Correct |
1 ms |
440 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
496 KB |
Output is correct |
10 |
Correct |
1 ms |
568 KB |
Output is correct |
11 |
Correct |
1 ms |
504 KB |
Output is correct |
12 |
Correct |
1 ms |
500 KB |
Output is correct |
13 |
Correct |
1 ms |
500 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
564 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
432 KB |
Output is correct |
7 |
Correct |
1 ms |
440 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
496 KB |
Output is correct |
10 |
Correct |
1 ms |
568 KB |
Output is correct |
11 |
Correct |
1 ms |
504 KB |
Output is correct |
12 |
Correct |
1 ms |
500 KB |
Output is correct |
13 |
Correct |
1 ms |
500 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
182 ms |
25428 KB |
Output is correct |
16 |
Correct |
162 ms |
26012 KB |
Output is correct |
17 |
Correct |
206 ms |
27392 KB |
Output is correct |
18 |
Correct |
75 ms |
21364 KB |
Output is correct |
19 |
Correct |
162 ms |
22988 KB |
Output is correct |
20 |
Correct |
162 ms |
24760 KB |
Output is correct |
21 |
Correct |
172 ms |
24948 KB |
Output is correct |
22 |
Correct |
158 ms |
22832 KB |
Output is correct |
23 |
Correct |
186 ms |
28356 KB |
Output is correct |
24 |
Correct |
214 ms |
25360 KB |
Output is correct |
25 |
Correct |
202 ms |
27616 KB |
Output is correct |
26 |
Correct |
171 ms |
24740 KB |
Output is correct |
27 |
Correct |
152 ms |
23136 KB |
Output is correct |
28 |
Correct |
223 ms |
25192 KB |
Output is correct |
29 |
Correct |
180 ms |
24788 KB |
Output is correct |
30 |
Correct |
122 ms |
22856 KB |
Output is correct |
31 |
Correct |
197 ms |
25184 KB |
Output is correct |
32 |
Correct |
179 ms |
27340 KB |
Output is correct |
33 |
Correct |
202 ms |
27836 KB |
Output is correct |
34 |
Correct |
180 ms |
25220 KB |
Output is correct |
35 |
Correct |
151 ms |
23704 KB |
Output is correct |
36 |
Correct |
68 ms |
21448 KB |
Output is correct |
37 |
Correct |
213 ms |
26032 KB |
Output is correct |
38 |
Correct |
190 ms |
25028 KB |
Output is correct |
39 |
Correct |
197 ms |
27708 KB |
Output is correct |