| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 781538 | vjudge1 | Sorting (IOI15_sorting) | C++17 | 40 ms | 22588 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 sp " "
#define endl "\n"
#define pii pair<int, int>
#define st first
#define nd second
#define pb push_back
#define K 200005
#define LOGN 20
int n, s[K], m, x[K], y[K], p[K], q[K];
vector<pii> swaps;
int check(int k){
vector<int> res(n), ind(n);
int todo = 0;
for (int i = 0; i < n; i++)
{
res[i] = s[i];
ind[s[i]] = i;
}
swaps.clear();
for (int i = 0; i < k; i++){
swap(res[x[i]], res[y[i]]);
ind[res[x[i]]] = x[i], ind[res[y[i]]] = y[i];
}
for (int i = 0; i < n; i++){
if (res[i] != i) {
int a = i, b = ind[i];
swaps.pb({res[i], i});
swap(res[a], res[b]);
ind[res[a]] = a, ind[res[b]] = b;
}
}
if (swaps.size() <= k) return 1;
return 0;
}
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];
int ans = 0;
for (int i = LOGN; i >= 0; i--){
int tmp = ans + (1<<i);
if (tmp <= m && check(tmp) == 0) ans = tmp;
}
if (check(ans) == 0) ans++;
int tmp = check(ans);
vector<int> res(N), ind(N);
int todo = 0;
for (int i = 0; i < n; i++)
{
res[i] = s[i];
ind[s[i]] = i;
}
/*
for (auto i : swaps){
cout<<i.st<<sp<<i.nd<<endl;
}*/
reverse(swaps.begin(), swaps.end());
for (int i = 0; i < ans; i++){
/*
for (auto i : res) cout<<i<<sp;
cout<<endl;*/
swap(res[X[i]], res[Y[i]]);
ind[res[X[i]]] = X[i], ind[res[Y[i]]] = Y[i];
if (swaps.empty()){
P[i] = 0, Q[i] = 0;
}
else{
int a = swaps.back().st, b = swaps.back().nd;
swaps.pop_back();
P[i] = ind[a], Q[i] = ind[b];
swap(res[P[i]], res[Q[i]]);
ind[res[P[i]]] = P[i], ind[res[Q[i]]] = Q[i];
}
}
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... | ||||
