#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
int idx[200005], idx2[200005], sta[200005], sta2[200005];
int val[200005];
int st[200005 * 3],ed[200005 * 3];
int vi[200005];
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int l = 0, r = M - 1;
while(l <= r)
{
for(int i = 0; i < N; i++)
val[S[i]] = i, idx[i] = idx2[i] = sta[i] = sta2[i] = i;
memset(vi, 0, sizeof(vi));
int mid = (l + r) / 2;
for(int m = 0; m <= mid; m++)
{
swap(idx[sta[X[m]]],idx[sta[Y[m]]]);
swap(sta[X[m]],sta[Y[m]]);
}
int cnt = 0;
for(int i = 0; i < N; i++)
{
if(!vi[i])
{
int nw = i;
while(!vi[nw])
{
vi[nw] = 1;
st[cnt] = sta[nw];
ed[cnt] = sta[idx[val[nw]]];
cnt++;
nw = idx[val[nw]];
}
cnt--;
}
}
if(l == r)
{
for(int i = 0; i < cnt; i++)
{
swap(idx2[sta2[X[i]]],idx2[sta2[Y[i]]]);
swap(sta2[X[i]],sta2[Y[i]]);
P[i] = idx2[st[i]];
Q[i] = idx2[ed[i]];
}
for(int i = cnt; i < mid + 1; i++)
P[i] = 0, Q[i] = 0;
return mid + 1;
}
if(cnt <= mid + 1)
r = mid;
else
l = mid + 1;
}
return -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
1108 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
1108 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1080 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1068 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
1108 KB |
Output is correct |
5 |
Correct |
2 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
3 |
Correct |
1 ms |
1108 KB |
Output is correct |
4 |
Correct |
1 ms |
1108 KB |
Output is correct |
5 |
Correct |
1 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
1108 KB |
Output is correct |
7 |
Correct |
1 ms |
1080 KB |
Output is correct |
8 |
Correct |
1 ms |
1108 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
2 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
1068 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
2 ms |
1108 KB |
Output is correct |
18 |
Correct |
1 ms |
1108 KB |
Output is correct |
19 |
Correct |
1 ms |
1076 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
2 ms |
1336 KB |
Output is correct |
22 |
Correct |
2 ms |
1364 KB |
Output is correct |
23 |
Correct |
2 ms |
1364 KB |
Output is correct |
24 |
Correct |
2 ms |
1340 KB |
Output is correct |
25 |
Correct |
3 ms |
1364 KB |
Output is correct |
26 |
Correct |
3 ms |
1364 KB |
Output is correct |
27 |
Correct |
2 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1220 KB |
Output is correct |
2 |
Correct |
2 ms |
1236 KB |
Output is correct |
3 |
Correct |
2 ms |
1236 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1236 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1220 KB |
Output is correct |
2 |
Correct |
2 ms |
1236 KB |
Output is correct |
3 |
Correct |
2 ms |
1236 KB |
Output is correct |
4 |
Incorrect |
1 ms |
1236 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |