# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
245727 | luisoncpp | Arranging Shoes (IOI19_shoes) | C++17 | 455 ms | 144572 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 <algorithm>
#include <iostream>
#include <vector>
#include <cstdint>
#include <cstdlib>
#include <cassert>
#include <queue>
#include <unordered_map>
using ll = int64_t;
template<typename T>
using Vec = std::vector<T>;
void GetFinalDestination(Vec<int>& S) {
std::unordered_map<int, std::queue<int>> UnmatchedValue;
int index = 0;
for (int& num : S) {
int bucket_id;
if (!UnmatchedValue[-num].empty()) {
auto prev_num = num;
bucket_id = UnmatchedValue[-num].front();
UnmatchedValue[-prev_num].pop();
} else {
UnmatchedValue[num].push(index);
bucket_id = index;
++index;
}
num = bucket_id * 2 + (num > 0);
}
}
ll SortAndCountInversions(Vec<int>& S, const int start, const int size) {
if (size < 2) {
return 0;
}
ll count = 0;
int izq = start;
int der = start + size/2;
const int mid = der;
const int end = start + size;
count += SortAndCountInversions(S, izq, size / 2);
count += SortAndCountInversions(S, der, size - (size / 2));
Vec<int> result;
while (result.size() < size) {
bool insert_left = true;
if (izq >= mid || (der < end && S[der] < S[izq])) {
insert_left = false;
}
if (insert_left) {
result.push_back(S[izq]);
++izq;
} else {
result.push_back(S[der]);
count += mid - izq;
++der;
}
}
for (ll i = 0; i < size; ++i) {
S[start + i] = result[i];
}
return count;
}
ll count_swaps(Vec<int> S) {
GetFinalDestination(S);
return SortAndCountInversions(S, 0, S.size());
}
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... |