#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<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]];
if(gen)ans.emplace_back(u,v);
swap(seq[u],seq[v]);
swap(pos[seq[u]],pos[seq[v]]);
cnt++;
}
}
return cnt<=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);
while(ans.size()<l)ans.emplace_back(0,0);
for(int i=0;i<l;i++)tie(P[i],Q[i])=ans[i];
return l;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:53:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
53 | while(ans.size()<l)ans.emplace_back(0,0);
| ~~~~~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
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 |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 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 |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 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 |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 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 |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
460 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 |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
460 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 |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
136 ms |
13696 KB |
Output is correct |
16 |
Correct |
140 ms |
14052 KB |
Output is correct |
17 |
Correct |
176 ms |
14588 KB |
Output is correct |
18 |
Correct |
71 ms |
11948 KB |
Output is correct |
19 |
Correct |
145 ms |
13452 KB |
Output is correct |
20 |
Correct |
148 ms |
13788 KB |
Output is correct |
21 |
Correct |
179 ms |
13804 KB |
Output is correct |
22 |
Correct |
128 ms |
14776 KB |
Output is correct |
23 |
Correct |
154 ms |
14976 KB |
Output is correct |
24 |
Correct |
200 ms |
14812 KB |
Output is correct |
25 |
Correct |
201 ms |
14620 KB |
Output is correct |
26 |
Correct |
159 ms |
13864 KB |
Output is correct |
27 |
Correct |
138 ms |
13424 KB |
Output is correct |
28 |
Correct |
178 ms |
14780 KB |
Output is correct |
29 |
Correct |
166 ms |
14732 KB |
Output is correct |
30 |
Correct |
117 ms |
13888 KB |
Output is correct |
31 |
Correct |
173 ms |
15012 KB |
Output is correct |
32 |
Correct |
145 ms |
14620 KB |
Output is correct |
33 |
Correct |
181 ms |
14736 KB |
Output is correct |
34 |
Correct |
158 ms |
14704 KB |
Output is correct |
35 |
Correct |
137 ms |
13364 KB |
Output is correct |
36 |
Correct |
74 ms |
13076 KB |
Output is correct |
37 |
Correct |
189 ms |
15056 KB |
Output is correct |
38 |
Correct |
218 ms |
14596 KB |
Output is correct |
39 |
Correct |
171 ms |
14696 KB |
Output is correct |