#include "sorting.h"
#include <bits/stdc++.h>
#define rep(i,a,b) for (int i=a; i<int(b); i++)
using namespace std;
vector<pair<int, int> > moves;
int movestofix(int Ii[],int Ss[], int N) {
int I[N], S[N];
rep (i,0,N) { I[i]=Ii[i];S[i]=Ss[i];}
moves.clear();
//I[i] is not value at i, but index of value i in S
int counter=0;
rep(i,0,N) {
if (S[i]==i) continue;
counter++;
int a,b;
a=i;
b=I[i];
//a och b byter plats
int sa,sb;
sa=S[a];
sb=S[b];
moves.push_back(make_pair(a,b));
S[a]=sb;
S[b]=sa;
I[sa]=b;
I[sb]=a;
}
moves.push_back(make_pair(0,0));
return counter;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int I[N];
rep (i,0,N) {
I[S[i]]=i;
}
int i=0;
int hi,lo;
hi=N;
lo=-1;
while (hi>lo+1) {
int mid=(hi+lo)/2;
int copyI[N],copyS[N];
rep (x,0,N) {
copyI[x]=I[x];
copyS[x]=S[x];
}
rep (x,0,mid) {
int a,b;
a=X[x];
b=Y[x];
//a och b byter plats
int sa,sb;
sa=copyS[a];
sb=copyS[b];
copyS[a]=sb;
copyS[b]=sa;
copyI[sa]=b;
copyI[sb]=a;
}
if (movestofix(copyI,copyS,N)>mid) lo=mid;
else hi=mid;
}
i=hi;
int mid = hi;
int copyI[N],copyS[N];
rep (x,0,N) {
copyI[x]=I[x];
copyS[x]=S[x];
}
rep (x,0,mid) {
int a,b;
a=X[x];
b=Y[x];
//a och b byter plats
int sa,sb;
sa=copyS[a];
sb=copyS[b];
copyS[a]=sb;
copyS[b]=sa;
copyI[sa]=b;
copyI[sb]=a;
}
if (movestofix(copyI,copyS,N)>mid) lo=mid;
else hi=mid;
rep (j,0,N) {S[j]=j;I[j]=j;}
for (int j=i-1;j>=0; j--) {
P[j]=S[moves[j].first];
Q[j]=S[moves[j].second];
//swap(switches[X[j]],switches[Y[j]]);
int a,b;
a=I[X[j]];
b=I[Y[j]];
//a och b byter plats
int sa,sb;
sa=S[a];
sb=S[b];
S[a]=sb;
S[b]=sa;
I[sa]=b;
I[sb]=a;
}
return i;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:34:39: warning: unused parameter 'M' [-Wunused-parameter]
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
256 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
256 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
3 ms |
356 KB |
Output is correct |
9 |
Correct |
3 ms |
300 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
256 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
256 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
256 KB |
Output is correct |
8 |
Correct |
3 ms |
356 KB |
Output is correct |
9 |
Correct |
3 ms |
300 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
256 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
3 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
256 KB |
Output is correct |
20 |
Correct |
2 ms |
256 KB |
Output is correct |
21 |
Correct |
3 ms |
384 KB |
Output is correct |
22 |
Correct |
4 ms |
512 KB |
Output is correct |
23 |
Correct |
3 ms |
512 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
3 ms |
512 KB |
Output is correct |
27 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
4 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 |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
4 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 |
5 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
242 ms |
13888 KB |
Output is correct |
16 |
Correct |
272 ms |
14184 KB |
Output is correct |
17 |
Correct |
368 ms |
14824 KB |
Output is correct |
18 |
Correct |
68 ms |
10096 KB |
Output is correct |
19 |
Correct |
261 ms |
12252 KB |
Output is correct |
20 |
Correct |
278 ms |
13032 KB |
Output is correct |
21 |
Correct |
292 ms |
13132 KB |
Output is correct |
22 |
Correct |
256 ms |
15112 KB |
Output is correct |
23 |
Correct |
338 ms |
15320 KB |
Output is correct |
24 |
Correct |
440 ms |
15208 KB |
Output is correct |
25 |
Correct |
384 ms |
15080 KB |
Output is correct |
26 |
Correct |
237 ms |
13132 KB |
Output is correct |
27 |
Correct |
212 ms |
12396 KB |
Output is correct |
28 |
Correct |
364 ms |
14936 KB |
Output is correct |
29 |
Correct |
352 ms |
14496 KB |
Output is correct |
30 |
Correct |
173 ms |
11120 KB |
Output is correct |
31 |
Correct |
436 ms |
14836 KB |
Output is correct |
32 |
Correct |
293 ms |
15080 KB |
Output is correct |
33 |
Correct |
468 ms |
15080 KB |
Output is correct |
34 |
Correct |
334 ms |
15080 KB |
Output is correct |
35 |
Correct |
252 ms |
12248 KB |
Output is correct |
36 |
Correct |
53 ms |
9720 KB |
Output is correct |
37 |
Correct |
412 ms |
15592 KB |
Output is correct |
38 |
Correct |
439 ms |
14916 KB |
Output is correct |
39 |
Correct |
454 ms |
15084 KB |
Output is correct |