#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;
typedef long long ll;
int n, q;
int arr[200002];
int a[600002], b[600002];
int lastOccurence[200002];
int tarr[200002];
int idx[200002];
stack<int> stk;
bool able(int x, bool print, int P[], int Q[]){
for(int i=0; i<n; i++){
tarr[i] = arr[i];
idx[tarr[i]] = i;
lastOccurence[i] = 0;
}
for(int i=1; i<=x; i++){
lastOccurence[a[i]] = i;
lastOccurence[b[i]] = i;
}
while(!stk.empty()) stk.pop();
for(int i=0; i<n; i++){
if(lastOccurence[i] == 0) stk.push(i);
}
for(int i=1; i<=x; i++){
int u = a[i], v = b[i];
swap(tarr[u], tarr[v]);
idx[tarr[u]] = u;
idx[tarr[v]] = v;
if(lastOccurence[u] == i) stk.push(u);
if(lastOccurence[v] == i) stk.push(v);
while(!stk.empty() && tarr[stk.top()] == stk.top()) stk.pop();
if(stk.empty()){
if(print) P[i-1] = Q[i-1] = 0;
continue;
}
u = stk.top(); stk.pop();
v = idx[u];
if(print) P[i-1] = u, Q[i-1] = v;
swap(tarr[u], tarr[v]);
idx[tarr[u]] = u;
idx[tarr[v]] = v;
}
for(int i=0; i<n; i++){
if(tarr[i] != i) return false;
}
return true;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]){
n = N;
for(int i=0; i<n; i++){
arr[i] = S[i];
}
q = M;
for(int i=1; i<=q; i++){
a[i] = X[i-1], b[i] = Y[i-1];
}
int MIN = 0, MAX = q, ANS = q+1;
while(MIN <= MAX){
int MID = (MIN + MAX) / 2;
if(able(MID, 0, P, Q)){
ANS = MID;
MAX = MID-1;
}
else MIN = MID+1;
}
assert(ANS <= q);
able(ANS, 1, P, Q);
return ANS;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
376 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
376 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
376 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
0 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Runtime error |
6 ms |
1132 KB |
Execution killed with signal 6 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
748 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
748 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |