#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;
int timer = 0;
vector<ll> inv(2 * n + 1);
map<int, Tree<int>> sets;
function<void(int)> dfs = [&](int idx) {
int x; cin >> x;
if (x == 0) {
int idx_l = ++timer;
// self(idx_l, self);
dfs(idx_l);
int idx_r = ++timer;
// self(idx_r, self);
dfs(idx_r);
if (sz(sets[idx_l]) > sz(sets[idx_r])) {
sets[idx_l].swap(sets[idx_r]);
}
ll opt_1 = 0, opt_2 = 0;
for (int v : sets[idx_l]) {
int loc = sets[idx_r].order_of_key(v);
opt_1 += loc;
opt_2 += sz(sets[idx_r]) - loc;
}
for (int v : sets[idx_l]) {
sets[idx_r].insert(v);
}
inv[idx] = inv[idx_l] + inv[idx_r] + min(opt_1, opt_2);
sets[idx].swap(sets[idx_r]);
sets.erase(idx_l);
sets.erase(idx_r);
} else {
sets[idx].insert(x);
}
};
dfs(0);
cout << inv[0] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1116 KB |
Output is correct |
2 |
Correct |
6 ms |
604 KB |
Output is correct |
3 |
Correct |
4 ms |
1364 KB |
Output is correct |
4 |
Correct |
5 ms |
1476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
3420 KB |
Output is correct |
2 |
Correct |
18 ms |
1372 KB |
Output is correct |
3 |
Correct |
59 ms |
2756 KB |
Output is correct |
4 |
Correct |
16 ms |
2676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
4432 KB |
Output is correct |
2 |
Correct |
68 ms |
8020 KB |
Output is correct |
3 |
Correct |
76 ms |
10836 KB |
Output is correct |
4 |
Correct |
72 ms |
11088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
22220 KB |
Output is correct |
2 |
Correct |
110 ms |
15700 KB |
Output is correct |
3 |
Correct |
170 ms |
9428 KB |
Output is correct |
4 |
Correct |
117 ms |
8020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
152 ms |
9040 KB |
Output is correct |
2 |
Correct |
166 ms |
11600 KB |
Output is correct |
3 |
Correct |
146 ms |
22084 KB |
Output is correct |
4 |
Correct |
158 ms |
21588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
281 ms |
21384 KB |
Output is correct |
2 |
Correct |
248 ms |
15188 KB |
Output is correct |
3 |
Correct |
238 ms |
18260 KB |
Output is correct |
4 |
Correct |
253 ms |
16640 KB |
Output is correct |
5 |
Correct |
356 ms |
14316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
257 ms |
17636 KB |
Output is correct |
2 |
Correct |
288 ms |
33108 KB |
Output is correct |
3 |
Correct |
336 ms |
23684 KB |
Output is correct |
4 |
Correct |
257 ms |
35236 KB |
Output is correct |
5 |
Correct |
452 ms |
16980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
296 ms |
20860 KB |
Output is correct |
2 |
Correct |
288 ms |
17744 KB |
Output is correct |
3 |
Correct |
354 ms |
14160 KB |
Output is correct |
4 |
Correct |
319 ms |
20368 KB |
Output is correct |
5 |
Correct |
313 ms |
31584 KB |
Output is correct |
6 |
Correct |
381 ms |
17580 KB |
Output is correct |