#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
int findSwapPairs(int n, int R[], int m, int X[], int Y[], int P[], int Q[]) {
int S[200001];
int l = -1, r = min(m, n+1);
vector<int> targetArr(n);
vector<int> revInd(n);
unordered_set<int> notGood; // Indices of places where no match
int countMatches;
auto swapTwo = [&](int i, int a, int b) {
if(a == S[a]) countMatches--;
if(b == S[b]) countMatches--;
swap(S[a], S[b]);
P[i] = a; Q[i] = b;
if(a == S[a]) countMatches++;
if(b == S[b]) countMatches++;
};
while(l != r - 1) {
m = (l + r + 1) / 2;
for(int i = 0; i < n; i++) S[i] = R[i];
countMatches = 0;
for(int i = 0; i < n; i++) {
targetArr[i] = i;
if(S[i] == i) countMatches++;
}
for(int i = m-1; i >= 0; i--) {
swap(targetArr[X[i]], targetArr[Y[i]]);
}
for(int i = 0; i < n; i++) {
revInd[targetArr[i]] = i;
}
for(int i = 0; i < n; i++) {
if(S[i] != targetArr[i]) notGood.insert(i);
}
for(int i = 0; i < m; i++) {
if(countMatches == n) {
m = i;
break;
}
if(X[i] != Y[i]) {
int x = (int)notGood.count(X[i]);
int y = (int)notGood.count(Y[i]);
if(X[i] == S[X[i]]) countMatches--;
if(Y[i] == S[Y[i]]) countMatches--;
swap(revInd[targetArr[X[i]]], revInd[targetArr[Y[i]]]);
swap(targetArr[X[i]], targetArr[Y[i]]);
swap(S[X[i]], S[Y[i]]);
notGood.erase(X[i]);
notGood.erase(Y[i]);
if(x) notGood.insert(Y[i]);
if(y) notGood.insert(X[i]);
if(X[i] == S[X[i]]) countMatches++;
if(Y[i] == S[Y[i]]) countMatches++;
}
if(notGood.size() == 0) {
swapTwo(i, 0, 0);
continue;
}
int a = *(notGood.begin());
int b = revInd[S[a]];
swapTwo(i, a, b);
assert(S[b] == targetArr[b]);
if(S[a] == targetArr[a]) notGood.erase(a);
if(S[b] == targetArr[b]) notGood.erase(b);
}
if(countMatches == n) r = m;
else l = m;
unordered_set<int>().swap(notGood);
}
m = r;
for(int i = 0; i < n; i++) S[i] = R[i];
countMatches = 0;
for(int i = 0; i < n; i++) {
targetArr[i] = i;
if(S[i] == i) countMatches++;
}
for(int i = m-1; i >= 0; i--) {
swap(targetArr[X[i]], targetArr[Y[i]]);
}
for(int i = 0; i < n; i++) {
revInd[targetArr[i]] = i;
}
for(int i = 0; i < n; i++) {
if(S[i] != targetArr[i]) notGood.insert(i);
}
for(int i = 0; i < m; i++) {
if(countMatches == n) {
m = i;
break;
}
if(X[i] != Y[i]) {
int x = (int)notGood.count(X[i]);
int y = (int)notGood.count(Y[i]);
if(X[i] == S[X[i]]) countMatches--;
if(Y[i] == S[Y[i]]) countMatches--;
swap(revInd[targetArr[X[i]]], revInd[targetArr[Y[i]]]);
swap(targetArr[X[i]], targetArr[Y[i]]);
swap(S[X[i]], S[Y[i]]);
notGood.erase(X[i]);
notGood.erase(Y[i]);
if(x) notGood.insert(Y[i]);
if(y) notGood.insert(X[i]);
if(X[i] == S[X[i]]) countMatches++;
if(Y[i] == S[Y[i]]) countMatches++;
}
if(notGood.size() == 0) {
swapTwo(i, 0, 0);
continue;
}
int a = *(notGood.begin());
int b = revInd[S[a]];
swapTwo(i, a, b);
assert(S[b] == targetArr[b]);
if(S[a] == targetArr[a]) notGood.erase(a);
if(S[b] == targetArr[b]) notGood.erase(b);
}
return r;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 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 |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
1108 KB |
Output is correct |
12 |
Correct |
1 ms |
1108 KB |
Output is correct |
13 |
Correct |
1 ms |
980 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1108 KB |
Output is correct |
17 |
Correct |
1 ms |
1108 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
1000 KB |
Output is correct |
20 |
Correct |
1 ms |
980 KB |
Output is correct |
21 |
Correct |
2 ms |
1108 KB |
Output is correct |
22 |
Correct |
2 ms |
1108 KB |
Output is correct |
23 |
Correct |
2 ms |
1236 KB |
Output is correct |
24 |
Correct |
3 ms |
1236 KB |
Output is correct |
25 |
Correct |
1 ms |
1108 KB |
Output is correct |
26 |
Correct |
2 ms |
1108 KB |
Output is correct |
27 |
Correct |
2 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1236 KB |
Output is correct |
2 |
Correct |
7 ms |
1236 KB |
Output is correct |
3 |
Correct |
5 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1108 KB |
Output is correct |
5 |
Correct |
2 ms |
1108 KB |
Output is correct |
6 |
Correct |
3 ms |
1108 KB |
Output is correct |
7 |
Correct |
5 ms |
1236 KB |
Output is correct |
8 |
Correct |
7 ms |
1236 KB |
Output is correct |
9 |
Correct |
6 ms |
1260 KB |
Output is correct |
10 |
Correct |
6 ms |
1236 KB |
Output is correct |
11 |
Correct |
7 ms |
1236 KB |
Output is correct |
12 |
Correct |
5 ms |
1248 KB |
Output is correct |
13 |
Correct |
7 ms |
1236 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1236 KB |
Output is correct |
2 |
Correct |
7 ms |
1236 KB |
Output is correct |
3 |
Correct |
5 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1108 KB |
Output is correct |
5 |
Correct |
2 ms |
1108 KB |
Output is correct |
6 |
Correct |
3 ms |
1108 KB |
Output is correct |
7 |
Correct |
5 ms |
1236 KB |
Output is correct |
8 |
Correct |
7 ms |
1236 KB |
Output is correct |
9 |
Correct |
6 ms |
1260 KB |
Output is correct |
10 |
Correct |
6 ms |
1236 KB |
Output is correct |
11 |
Correct |
7 ms |
1236 KB |
Output is correct |
12 |
Correct |
5 ms |
1248 KB |
Output is correct |
13 |
Correct |
7 ms |
1236 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Execution timed out |
1086 ms |
18156 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |