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;
using ll = long long;
using pii = array<int, 2>;
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using Tree =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int main() {
cin.tie(0) -> sync_with_stdio(0);
int n; cin >> n;
const auto dfs = [&](auto &&self) -> pair<ll, Tree<int>> {
int x; cin >> x;
if (x == 0) {
auto [inv_l, set_l] = self(self);
auto [inv_r, set_r] = self(self);
if (sz(set_l) > sz(set_r)) {
set_l.swap(set_r);
}
ll opt_1 = 0, opt_2 = 0;
for (int v : set_l) {
int idx = set_r.order_of_key(v);
opt_1 += idx;
opt_2 += sz(set_r) - idx;
}
for (int v : set_l) {
set_r.insert(v);
}
return {inv_l + inv_r + min(opt_1, opt_2), set_r};
} else {
Tree<int> res;
res.insert(x);
return {0, res};
}
};
cout << dfs(dfs).first << "\n";
}
# | 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... |
# | 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... |