#include "sorting.h"
#include<bits/stdc++.h>
#define ll int
#define pb push_back
#define ld long double
#define mp make_pair
#define F first
#define S second
#define pii pair<ll,ll>
using namespace :: std;
const ll maxn=6e5+500;
const ll inf=1e9+900;
ll per[maxn];
ll s[maxn];
ll x[maxn];
ll y[maxn];
ll koj[maxn];
void swa(ll a,ll b){
koj[per[a]]=b;
koj[per[b]]=a;
swap(per[a],per[b]);
}
ll n,m;
vector<pii> vec;
void dfs(ll a){
if(per[a]==a)return;
vec.pb(mp(a,per[a]));
swap(per[a],per[per[a]]);
dfs(a);
}
void copytoper(){
for(ll i=0;i<n;i++){
per[i]=s[i];
}
}
bool mishe(ll mid){
copytoper();
for(ll i=0;i<mid;i++){
swap(per[x[i]],per[y[i]]);
}
vec.clear();
for(ll i=0;i<n;i++){
dfs(i);
}
return (vec.size()<=mid);
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n=N,m=M;
for(ll i=0;i<n;i++){
s[i]=S[i];
}
for(ll i=0;i<m;i++){
x[i]=X[i];
y[i]=Y[i];
}
ll b=-1;
ll e=M;
while(e-b>1){
ll mid=(e+b)/2;
if(mishe(mid)){
e=mid;
}else{
b=mid;
}
}
// ans=e;
copytoper();
for(ll i=0;i<e;i++){
swap(per[x[i]],per[y[i]]);
}
vec.clear();
for(ll i=0;i<n;i++){
dfs(i);
}
while(vec.size()<e){
vec.pb(mp(0,0));
}
if(vec.size()>e){
return 0;
}
for(ll i=0;i<n;i++){
per[i]=i;
koj[i]=i;
}
for(ll i=e-1;i>=0;i--){
swa(x[i],y[i]);
}
vector<pii> ans;
for(ll i=0;i<e;i++){
swa(x[i],y[i]);
pii a=vec[i];
ll b1=koj[a.F];
ll b2=koj[a.S];
ans.pb(mp(b1,b2));
}
for(ll i=0;i<e;i++){
P[i]=ans[i].F;
Q[i]=ans[i].S;
}
copytoper();
for(ll i=0;i<e;i++){
swap(per[x[i]],per[y[i]]);
swap(per[Q[i]],per[P[i]]);
}
for(ll i=0;i<n;i++){
if(per[i]!=i){
return 0;
}
}
return e;
}
Compilation message
sorting.cpp: In function 'bool mishe(int)':
sorting.cpp:51:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
return (vec.size()<=mid);
~~~~~~~~~~^~~~~
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:81:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(vec.size()<e){
~~~~~~~~~~^~
sorting.cpp:84:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(vec.size()>e){
~~~~~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
3 ms |
632 KB |
Output is correct |
22 |
Correct |
3 ms |
632 KB |
Output is correct |
23 |
Correct |
3 ms |
632 KB |
Output is correct |
24 |
Correct |
3 ms |
632 KB |
Output is correct |
25 |
Correct |
3 ms |
632 KB |
Output is correct |
26 |
Correct |
3 ms |
632 KB |
Output is correct |
27 |
Correct |
3 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
632 KB |
Output is correct |
3 |
Correct |
3 ms |
632 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
632 KB |
Output is correct |
6 |
Correct |
3 ms |
632 KB |
Output is correct |
7 |
Correct |
3 ms |
632 KB |
Output is correct |
8 |
Correct |
3 ms |
632 KB |
Output is correct |
9 |
Correct |
3 ms |
632 KB |
Output is correct |
10 |
Correct |
3 ms |
636 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
636 KB |
Output is correct |
13 |
Correct |
4 ms |
632 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
2 |
Correct |
3 ms |
632 KB |
Output is correct |
3 |
Correct |
3 ms |
632 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
632 KB |
Output is correct |
6 |
Correct |
3 ms |
632 KB |
Output is correct |
7 |
Correct |
3 ms |
632 KB |
Output is correct |
8 |
Correct |
3 ms |
632 KB |
Output is correct |
9 |
Correct |
3 ms |
632 KB |
Output is correct |
10 |
Correct |
3 ms |
636 KB |
Output is correct |
11 |
Correct |
3 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
636 KB |
Output is correct |
13 |
Correct |
4 ms |
632 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
229 ms |
23416 KB |
Output is correct |
16 |
Correct |
255 ms |
31568 KB |
Output is correct |
17 |
Correct |
295 ms |
34548 KB |
Output is correct |
18 |
Correct |
91 ms |
26380 KB |
Output is correct |
19 |
Correct |
181 ms |
33236 KB |
Output is correct |
20 |
Correct |
192 ms |
29544 KB |
Output is correct |
21 |
Correct |
188 ms |
31976 KB |
Output is correct |
22 |
Correct |
227 ms |
24184 KB |
Output is correct |
23 |
Correct |
281 ms |
35404 KB |
Output is correct |
24 |
Correct |
305 ms |
34688 KB |
Output is correct |
25 |
Correct |
307 ms |
34564 KB |
Output is correct |
26 |
Correct |
199 ms |
25052 KB |
Output is correct |
27 |
Correct |
156 ms |
23892 KB |
Output is correct |
28 |
Correct |
293 ms |
33232 KB |
Output is correct |
29 |
Correct |
239 ms |
25684 KB |
Output is correct |
30 |
Correct |
113 ms |
23144 KB |
Output is correct |
31 |
Correct |
251 ms |
26296 KB |
Output is correct |
32 |
Correct |
256 ms |
34188 KB |
Output is correct |
33 |
Correct |
295 ms |
34932 KB |
Output is correct |
34 |
Correct |
251 ms |
26616 KB |
Output is correct |
35 |
Correct |
180 ms |
30568 KB |
Output is correct |
36 |
Correct |
66 ms |
21368 KB |
Output is correct |
37 |
Correct |
319 ms |
36928 KB |
Output is correct |
38 |
Correct |
296 ms |
35204 KB |
Output is correct |
39 |
Correct |
352 ms |
35744 KB |
Output is correct |