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 "shoes.h"
#include <iostream>
#include <iterator>
#include <map>
using namespace std;
using ll = signed long long int;
ll merge_sort(vector<int>& a) {
if(a.size() == 1) return 0;
vector<int> l(a.size()/2), r(a.size() - a.size()/2);
copy(a.begin(), a.begin() + a.size()/2, l.begin());
copy(a.begin() + a.size()/2, a.end(), r.begin());
ll ans = merge_sort(l) + merge_sort(r);
int i = 0, j = 0;
while(i < (int)l.size() && j < (int)r.size()) {
if(l[i] <= r[j]) a[i+j] = l[i], i++;
else ans += l.size() - i, a[i+j] = r[j], j++;
}
while(i < (int)l.size()) a[i+j] = l[i], i++;
while(j < (int)r.size()) a[i+j] = r[j], j++;
return ans;
}
long long count_swaps(std::vector<int> a) {
int n = a.size(), x = 0;
vector<int> idx(n);
multimap<int, int> pairs;
for(int i = 0; i < n; ++i) {
if(pairs.count(a[i])) {
auto it = pairs.lower_bound(a[i]);
idx[i] = it->second + (a[i] > 0);
pairs.erase(it);
} else {
idx[i] = x + (a[i] > 0);
pairs.emplace(-a[i], x);
x += 2;
}
}
return merge_sort(idx);
}
# | 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... |