# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
17291 | erdemkiraz | Sorting (IOI15_sorting) | C++98 | 632 ms | 17800 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 <bits/stdc++.h>
using namespace std;
#define type(x) __typeof((x).begin())
#define foreach(it, x) for(type(x) it = (x).begin(); it != (x).end(); it++)
typedef long long ll;
typedef pair < int, int > ii;
const int inf = 1e9 + 333;
const ll linf = 1e18 + inf;
#include "sorting.h"
const int N = 2e5 + 5;
int n, m;
int s[N], a[N], h[N], w[N], p[N];
ii XY[N];
void swp(int x, int y) {
int X = a[x];
int Y = a[y];
swap(w[X], w[Y]);
swap(a[x], a[y]);
}
bool f(int x, int P[], int Q[]) {
for(int i = 0; i < n; i++) {
p[i] = i;
}
for(int i = 0; i < x; i++) {
swap(p[XY[i].first], p[XY[i].second]);
}
for(int i = 0; i < n; i++) {
w[s[i]] = i;
a[i] = s[p[i]];
}
memset(h, 0, sizeof(h));
int res = n, sz = 0;
vector < int > v;
vector < ii > ans;
for(int i = 0; i < n; i++) {
if(!h[i]) {
res--;
int x = i;
v.clear();
do{
v.push_back(x);
h[x] = 1;
x = a[x];
}while(x != i);
for(int it = (int) v.size() - 1; it >= 1; it--) {
int x = v[0];
int y = v[it];
ans.push_back(ii(a[x], a[y]));
}
}
}
for(int i = 0; i < n; i++)
a[i] = s[i];
if(res <= x) {
for(int it = 0; it < ans.size(); it++) {
swp(XY[it].first, XY[it].second);
P[sz] = w[ans[it].first];
Q[sz] = w[ans[it].second];
sz++;
swp(w[ans[it].first], w[ans[it].second]);
}
while(sz < x) {
P[sz] = 0;
Q[sz] = 0;
sz++;
}
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++) {
XY[i] = ii(X[i], Y[i]);
}
int l = 0, r = n - 1;
while(l < r) {
int m = (l + r) >> 1;
if(f(m, P, Q))
r = m;
else
l = m + 1;
}
f(l, P, Q);
return l;
}
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... |