#include "sorting.h"
#include <bits/stdc++.h>
#define MAXN 200015
using namespace std;
int bk[MAXN],pos_bk[MAXN],pre[MAXN],pos_pre[MAXN];
int solve(int N,int n,int S[],int P[],int Q[],int X[],int Y[]){
int now=0,cnt_same=0;
for (int i=0;i<N;i++){
bk[i]=i; pos_bk[i]=i;
pre[i]=S[i]; pos_pre[S[i]]=i;
if (pre[i]==i) cnt_same++;
}
for (int i=n-1;i>=0;i--){
swap(bk[X[i]],bk[Y[i]]);
pos_bk[bk[X[i]]]=X[i];
pos_bk[bk[Y[i]]]=Y[i];
}
for (int i=0;i<n;i++){
if (cnt_same==N) return n;
if (pre[X[i]]==X[i]) cnt_same--;
if (pre[Y[i]]==Y[i]) cnt_same--;
swap(pre[X[i]],pre[Y[i]]);
if (pre[X[i]]==X[i]) cnt_same++;
if (pre[Y[i]]==Y[i]) cnt_same++;
pos_pre[pre[X[i]]]=X[i];
pos_pre[pre[Y[i]]]=Y[i];
swap(bk[X[i]],bk[Y[i]]);
pos_bk[bk[X[i]]]=X[i];
pos_bk[bk[Y[i]]]=Y[i];
now=i;
while (now<N&&pos_pre[now]==pos_bk[now])
now++;
if (now>=N){
P[i]=0; Q[i]=0;
continue;
}
P[i]=pos_pre[now]; Q[i]=pos_bk[now];
if (pre[P[i]]==P[i]) cnt_same--;
if (pre[Q[i]]==Q[i]) cnt_same--;
swap(pre[pos_pre[now]],pre[pos_bk[now]]);
if (pre[P[i]]==P[i]) cnt_same++;
if (pre[Q[i]]==Q[i]) cnt_same++;
pos_pre[pre[pos_pre[now]]]=pos_pre[now];
pos_pre[pre[pos_bk[now]]]=pos_bk[now];
}
if (cnt_same==N) return n;
else return 1e9;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int l=0,r=M;
while (l<r){
int mid=(l+r)>>1;
if (solve(N,mid,S,P,Q,X,Y)<1e8) r=mid;
else l=mid+1;
}
return solve(N,l,S,P,Q,X,Y);
/*
int mn=0;
for (int i=0;i<M;i++){
int need=solve(N,i,S,P,Q,X,Y);
if (need<1e8){
if (!mn) mn=i;
cout<<i<<" -> OK\n";
}
else cout<<i<<" -> NO\n";
}
return solve(N,mn,S,P,Q,X,Y);
*/
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
324 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 |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
324 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 |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
312 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 |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 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 |
1 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 |
1 ms |
324 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 |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
312 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 |
320 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 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 |
308 KB |
Output is correct |
21 |
Correct |
2 ms |
620 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
2 ms |
580 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
448 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
456 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
5 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
4 ms |
468 KB |
Output is correct |
13 |
Correct |
3 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
448 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
456 KB |
Output is correct |
6 |
Correct |
3 ms |
468 KB |
Output is correct |
7 |
Correct |
5 ms |
468 KB |
Output is correct |
8 |
Correct |
3 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
468 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
4 ms |
468 KB |
Output is correct |
13 |
Correct |
3 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
284 ms |
12088 KB |
Output is correct |
16 |
Correct |
360 ms |
20428 KB |
Output is correct |
17 |
Correct |
463 ms |
21524 KB |
Output is correct |
18 |
Correct |
65 ms |
16200 KB |
Output is correct |
19 |
Execution timed out |
1026 ms |
19640 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |