#include <iostream>
#include <vector>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int c = 1, a[400005];
vector<int> g[400005];
ordered_set s[400005];
void trya() {
int h = c, w;
cin >> w;
if (w == 0) {
c++;
int k = c, l;
trya();
c++;
l = c;
trya();
g[h].push_back(k);
g[h].push_back(l);
}
else a[h] = w;
}
long long int dfs(int x) {
if (g[x].size() == 0) {
s[x].insert(a[x]);
return 0;
}
long long int res = 0, c = 0, h = 1;
for (int w : g[x]) {
c += dfs(w);
h *= s[w].size();
int flag = 0;
if (s[w].size() > s[x].size()) {
swap(s[w], s[x]);
flag = 1;
}
for (int v : s[w]) {
if (flag == 1) res += s[x].order_of_key(v);
else res += s[x].size() - s[x].order_of_key(v);
}
for (int v : s[w]) {
s[x].insert(v);
}
}
return min(res, h - res) + c;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
trya();
cout << dfs(1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
41040 KB |
Output is correct |
2 |
Correct |
26 ms |
41052 KB |
Output is correct |
3 |
Correct |
23 ms |
40988 KB |
Output is correct |
4 |
Correct |
24 ms |
41052 KB |
Output is correct |
5 |
Correct |
24 ms |
41052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
41052 KB |
Output is correct |
2 |
Correct |
24 ms |
41052 KB |
Output is correct |
3 |
Correct |
24 ms |
41076 KB |
Output is correct |
4 |
Correct |
31 ms |
41040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
41308 KB |
Output is correct |
2 |
Correct |
25 ms |
41308 KB |
Output is correct |
3 |
Correct |
25 ms |
41308 KB |
Output is correct |
4 |
Correct |
32 ms |
41308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
300 ms |
42436 KB |
Output is correct |
2 |
Correct |
34 ms |
42324 KB |
Output is correct |
3 |
Correct |
292 ms |
42308 KB |
Output is correct |
4 |
Correct |
387 ms |
42644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1020 ms |
44628 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
204 ms |
60752 KB |
Output is correct |
2 |
Execution timed out |
1070 ms |
47840 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1068 ms |
61936 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1060 ms |
48160 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
115 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1041 ms |
52764 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1016 ms |
52044 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |