# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
331635 | monkey8 | Sorting (IOI15_sorting) | C++14 | 5 ms | 1388 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 <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <cstdio>
#include <utility>
#include <queue>
#include <math.h>
#include <set>
#include <bitset>
#include <cmath>
#include <bitset>
#include <iterator>
#include <limits>
#include <cstring>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 200010;
int visited[MAXN];
int vals[MAXN];
int idxs[MAXN];
void dfs(int u) {
visited[u] = 1;
if(!visited[vals[u]]) dfs(vals[u]);
}
int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
int low = 1;
int high = n;
while(low < high) {
int avg = (low + high) / 2;
for(int i = 0; i < n; i++)
vals[i] = s[i];
for(int i = 0; i < avg; i++) {
int temp = vals[x[i]];
vals[x[i]] = vals[y[i]];
vals[y[i]] = temp;
}
int num = 0;
for(int i = 0; i < n; i++) {
if(!visited[i]) {
dfs(i);
num++;
}
}
memset(visited, 0, sizeof(visited));
if(n - num <= avg) high = avg;
else low = avg + 1;
}
for(int i = 0; i < n; i++) {
vals[i] = s[i];
idxs[s[i]] = i;
}
for(int i = 0; i < low; i++) {
int temp = vals[x[i]];
vals[x[i]] = vals[y[i]];
vals[y[i]] = temp;
}
vector<pii> swaps;
for(int i = 0; i < n; i++) {
if(i == vals[i]) continue;
int j = i;
visited[j] = 1;
while(!visited[vals[j]]) {
swaps.push_back(pii(j, vals[j]));
visited[vals[j]] = 1;
j = vals[j];
}
}
for(int i = 0; i < n; i++) {
vals[i] = s[i];
idxs[s[i]] = i;
}
for(int i = 0; i < low; i++) {
int temp = vals[x[i]];
idxs[vals[x[i]]] = y[i];
idxs[vals[y[i]]] = x[i];
vals[x[i]] = vals[y[i]];
vals[y[i]] = temp;
if(i < (int)swaps.size()) {
p[i] = idxs[swaps[i].first];
q[i] = idxs[swaps[i].second];
temp = idxs[swaps[i].first];
idxs[swaps[i].first] = idxs[swaps[i].second];
idxs[swaps[i].second] = temp;
temp = vals[p[i]];
vals[p[i]] = vals[q[i]];
vals[q[i]] = temp;
}
else {
p[i] = 0;
q[i] = 0;
}
}
return low;
}
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... |