#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;
ordered_set s[400005];
long long int dfs(int x) {
int w;
cin >> w;
if (w != 0) {
s[x].insert(w);
return 0;
}
long long int res = 0, d = 0, h = 1;
{
int w = ++c;
d += dfs(w);
h *= s[w].size();
int flag = 0;
if (s[w].size() > s[x].size()) {
s[x].swap(s[w]);
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);
}
s[w].clear();
}
{
int w = ++c;
d += dfs(w);
h *= s[w].size();
int flag = 0;
if (s[w].size() > s[x].size()) {
s[x].swap(s[w]);
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);
}
s[w].clear();
}
return min(res, h - res) + d;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
cout << dfs(1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
31576 KB |
Output is correct |
2 |
Correct |
20 ms |
31580 KB |
Output is correct |
3 |
Correct |
22 ms |
31580 KB |
Output is correct |
4 |
Correct |
21 ms |
31580 KB |
Output is correct |
5 |
Correct |
21 ms |
31580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
31764 KB |
Output is correct |
2 |
Correct |
21 ms |
31580 KB |
Output is correct |
3 |
Correct |
23 ms |
31580 KB |
Output is correct |
4 |
Correct |
21 ms |
31580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
31820 KB |
Output is correct |
2 |
Correct |
23 ms |
31576 KB |
Output is correct |
3 |
Correct |
22 ms |
31580 KB |
Output is correct |
4 |
Correct |
21 ms |
31832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
32432 KB |
Output is correct |
2 |
Correct |
24 ms |
31868 KB |
Output is correct |
3 |
Correct |
23 ms |
32604 KB |
Output is correct |
4 |
Correct |
23 ms |
32604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
34392 KB |
Output is correct |
2 |
Correct |
33 ms |
32420 KB |
Output is correct |
3 |
Correct |
62 ms |
33644 KB |
Output is correct |
4 |
Correct |
30 ms |
33868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
34896 KB |
Output is correct |
2 |
Correct |
59 ms |
38736 KB |
Output is correct |
3 |
Correct |
67 ms |
41888 KB |
Output is correct |
4 |
Correct |
63 ms |
41608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
51536 KB |
Output is correct |
2 |
Correct |
79 ms |
46412 KB |
Output is correct |
3 |
Correct |
101 ms |
40020 KB |
Output is correct |
4 |
Correct |
75 ms |
37460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
37968 KB |
Output is correct |
2 |
Correct |
121 ms |
41304 KB |
Output is correct |
3 |
Correct |
101 ms |
52052 KB |
Output is correct |
4 |
Correct |
103 ms |
51784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
240 ms |
50660 KB |
Output is correct |
2 |
Correct |
166 ms |
44196 KB |
Output is correct |
3 |
Correct |
166 ms |
46952 KB |
Output is correct |
4 |
Correct |
232 ms |
44624 KB |
Output is correct |
5 |
Correct |
304 ms |
43136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
178 ms |
45324 KB |
Output is correct |
2 |
Correct |
182 ms |
63060 KB |
Output is correct |
3 |
Correct |
220 ms |
52988 KB |
Output is correct |
4 |
Correct |
156 ms |
65536 KB |
Output is correct |
5 |
Correct |
406 ms |
46760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
234 ms |
48208 KB |
Output is correct |
2 |
Correct |
194 ms |
46164 KB |
Output is correct |
3 |
Correct |
304 ms |
42360 KB |
Output is correct |
4 |
Correct |
227 ms |
48208 KB |
Output is correct |
5 |
Correct |
182 ms |
61416 KB |
Output is correct |
6 |
Correct |
290 ms |
45648 KB |
Output is correct |