# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
785473 | Sohsoh84 | Sorting (IOI15_sorting) | C++17 | 205 ms | 21500 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << ": " << x << endl;
#define sep ' '
const int MAXN = 1e6 + 10;
int X[MAXN], Y[MAXN], n, m, ans_A[MAXN], ans_B[MAXN], T[MAXN], P[MAXN], S[MAXN], ind[MAXN];
inline void check(set<int>& st, int x) {
if (st.find(x) != st.end()) st.erase(x);
if (P[x] != x) st.insert(x);
}
inline bool solve(int m) {
// P: jaye mellat too jaygasht nahayii baad swap ha
for (int i = 0; i < n; i++) T[i] = S[i];
for (int i = 0; i <= m; i++) swap(T[X[i]], T[Y[i]]);
for (int i = 0; i < n; i++) P[T[i]] = i;
for (int i = 0; i < n; i++) {
T[i] = S[i];
ind[S[i]] = i;
}
vector<int> vec;
for (int i = 0; i < n; i++)
if (P[i] != i)
vec.push_back(i);
for (int i = 0; i <= m; i++) {
swap(T[X[i]], T[Y[i]]);
ind[T[X[i]]] = X[i];
ind[T[Y[i]]] = Y[i];
while (!vec.empty() && P[vec.back()] == vec.back()) vec.pop_back();
if (vec.empty()) ans_A[i] = ans_B[i] = 0;
else {
int e = vec.back();
ans_A[i] = ind[e];
ans_B[i] = ind[P[e]];
int a = e, b = P[e];
swap(P[e], P[P[e]]);
vec.push_back(a);
vec.push_back(b);
}
swap(T[ans_A[i]], T[ans_B[i]]);
ind[T[ans_A[i]]] = ans_A[i];
ind[T[ans_B[i]]] = ans_B[i];
}
while (!vec.empty() && P[vec.back()] == vec.back()) vec.pop_back();
return vec.empty();
}
int findSwapPairs(int N_, int S_[], int M_, int X_[], int Y_[], int P[], int Q[]) {
n = N_, m = M_;
for (int i = 0; i < n; i++)
S[i] = S_[i];
for (int i = 0; i < m; i++)
X[i] = X_[i], Y[i] = Y_[i];
bool flag = true;
for (int i = 0; i < n; i++)
if (S[i] != i)
flag = false;
if (flag) return 0;
int l = 0, r = m - 1;
while (l < r) {
int mid = (l + r) >> 1;
if (solve(mid)) r = mid;
else l = mid + 1;
}
solve(l);
for (int i = 0; i <= l; i++)
P[i] = ans_A[i], Q[i] = ans_B[i];
return l + 1;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |