# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
395435 | rocks03 | Sorting (IOI15_sorting) | C++14 | 1084 ms | 460 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.
//#pragma GCC target("avx2")
//#pragma GCC optimization("O3")
//#pragma GCC optimization("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int findSwapPairs(int N, int B[], int M, int X[], int Y[], int P[], int Q[]){
vector<int> a, pos;
set<int> st;
auto swappa = [&](int i, int j){
auto it = st.find(a[i]);
if(it != st.end()) st.erase(it);
it = st.find(a[j]);
if(it != st.end()) st.erase(it);
swap(pos[a[i]], pos[a[j]]);
swap(a[i], a[j]);
if(pos[a[i]] != i) st.insert(a[i]);
if(pos[a[j]] != j) st.insert(a[j]);
};
auto random_sol = [&](){
int ans = 0;
while(SZ(st) && ans <= M){
int i = X[ans], j = Y[ans];
swappa(i, j);
if(SZ(st) == 0){
P[ans] = 0;
Q[ans] = 0;
ans++; break;
}
int x = rng() % N;
auto it = st.lower_bound(x);
if(it == st.end()) it--;
x = *it;
i = pos[x], j = x;
P[ans] = i, Q[ans++] = j;
swappa(i, j);
}
return ans;
};
while(true){
pos = a = vector<int>(N);
rep(i, 0, N){
a[i] = B[i];
pos[a[i]] = i;
}
st.clear();
rep(i, 0, N){
if(pos[i] != i) st.insert(i);
}
int ans = random_sol();
if(ans > M) continue;
rep(i, 0, N) a[i] = B[i];
rep(i, 0, ans){
swap(a[X[i]], a[Y[i]]);
swap(a[P[i]], a[Q[i]]);
}
rep(i, 0, ans){
if(a[i] != i){
ans = -1; break;
}
}
if(ans != -1) return ans;
}
}
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... |