#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;
#define sz(v) ((int)(v).size())
typedef long long lint;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int p=-1,q=M,h;
vector<int>C(N),R(N),go(N);
while(p!=q-1)
{
h=(p+q)/2;
for(int i=0;i<N;i++)C[i]=S[i];
for(int i=0;i<h;i++)swap(C[X[i]],C[Y[i]]);
for(int i=0;i<N;i++)go[C[i]]=i;
for(int i=0;i<N;i++)
{
C[i]=S[i];
R[S[i]]=i;
}
for(int i=0;i<h;i++)
{
swap(R[C[X[i]]],R[C[Y[i]]]);
swap(C[X[i]],C[Y[i]]);
int ch=-1;
for(int j=0;j<N;j++)
{
if(go[j]!=j)
{
ch=j;
break;
}
}
if(ch>=0)
{
for(int j=0;j<N;j++)
{
if(go[j]==ch)
{
swap(C[R[j]],C[R[ch]]);
swap(R[j],R[ch]);
swap(go[j],go[ch]);
break;
}
}
}
}
int s=0;
for(int i=0;i<N;i++)if(go[i]==i)s++;
if(s==N)q=h;
else p=h;
}
for(int i=0;i<N;i++)C[i]=S[i];
for(int i=0;i<q;i++)swap(C[X[i]],C[Y[i]]);
for(int i=0;i<N;i++)go[C[i]]=i;
for(int i=0;i<N;i++)
{
C[i]=S[i];
R[S[i]]=i;
}
for(int i=0;i<q;i++)
{
R[C[X[i]]]=Y[i];
R[C[Y[i]]]=X[i];
swap(C[X[i]],C[Y[i]]);
int ch=-1;
for(int j=0;j<N;j++)
{
if(go[j]!=j)
{
ch=j;
break;
}
}
if(ch>=0)
{
for(int j=0;j<N;j++)
{
if(go[j]==ch)
{
swap(C[R[j]],C[R[ch]]);
P[i]=R[j];
Q[i]=R[ch];
swap(R[j],R[ch]);
swap(go[j],go[ch]);
break;
}
}
}
else P[i]=Q[i]=0;
}
return q;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
260 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
260 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
296 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 |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
304 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
340 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 |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
260 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
296 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 |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
304 KB |
Output is correct |
14 |
Correct |
1 ms |
300 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
8 ms |
528 KB |
Output is correct |
22 |
Correct |
8 ms |
468 KB |
Output is correct |
23 |
Correct |
8 ms |
468 KB |
Output is correct |
24 |
Correct |
8 ms |
468 KB |
Output is correct |
25 |
Correct |
6 ms |
468 KB |
Output is correct |
26 |
Correct |
7 ms |
444 KB |
Output is correct |
27 |
Correct |
8 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
340 KB |
Output is correct |
2 |
Correct |
45 ms |
436 KB |
Output is correct |
3 |
Correct |
53 ms |
416 KB |
Output is correct |
4 |
Correct |
9 ms |
340 KB |
Output is correct |
5 |
Correct |
11 ms |
444 KB |
Output is correct |
6 |
Correct |
15 ms |
432 KB |
Output is correct |
7 |
Correct |
30 ms |
440 KB |
Output is correct |
8 |
Correct |
42 ms |
424 KB |
Output is correct |
9 |
Correct |
39 ms |
424 KB |
Output is correct |
10 |
Correct |
45 ms |
340 KB |
Output is correct |
11 |
Correct |
38 ms |
424 KB |
Output is correct |
12 |
Correct |
28 ms |
444 KB |
Output is correct |
13 |
Correct |
45 ms |
420 KB |
Output is correct |
14 |
Correct |
7 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
340 KB |
Output is correct |
2 |
Correct |
45 ms |
436 KB |
Output is correct |
3 |
Correct |
53 ms |
416 KB |
Output is correct |
4 |
Correct |
9 ms |
340 KB |
Output is correct |
5 |
Correct |
11 ms |
444 KB |
Output is correct |
6 |
Correct |
15 ms |
432 KB |
Output is correct |
7 |
Correct |
30 ms |
440 KB |
Output is correct |
8 |
Correct |
42 ms |
424 KB |
Output is correct |
9 |
Correct |
39 ms |
424 KB |
Output is correct |
10 |
Correct |
45 ms |
340 KB |
Output is correct |
11 |
Correct |
38 ms |
424 KB |
Output is correct |
12 |
Correct |
28 ms |
444 KB |
Output is correct |
13 |
Correct |
45 ms |
420 KB |
Output is correct |
14 |
Correct |
7 ms |
340 KB |
Output is correct |
15 |
Execution timed out |
1052 ms |
14828 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |