#include <bits/stdc++.h>
using namespace std;
template<class C>constexpr int len(const C&c){return int(c.size());}
const int MAXN = 200005;
int N, inp[MAXN], sz[MAXN], L[MAXN], R[MAXN], bit[MAXN]; long long ans;
void update(int pos, int val){
for(int i = pos; i <= N; i += i & -i) bit[i] += val;
}
int query(int pos){
int tot = 0;
for(int i = pos; i > 0; i &= i - 1) tot += bit[i];
return tot;
}
int read_tree(int id){
cin >> inp[id];
if(inp[id] != 0){
L[id] = R[id] = id;
sz[id] = 1;
return id;
}
int left = read_tree(id + 1);
int right = read_tree(left + 1);
sz[id] = sz[left] + sz[right];
L[id] = left; return R[id] = right;
}
vector<int> solve(int id){
int A = inp[id];
if(A != 0){
update(A, 1);
return {A};
}
vector<int> left, right;
if(sz[L[id]] <= sz[R[id]]){
left = solve(id + 1);
for(auto i : left) update(i, -1);
right = solve(L[id] + 1);
} else {
left = solve(L[id] + 1);
for(auto i : left) update(i, -1);
right = solve(id + 1);
}
int cur, case1 = 0, case2 = 0;
for(auto i : left){
cur = query(i - 1);
case1 += cur; case2 += len(right) - cur;
}
ans += min(case1, case2);
for(auto i : left) update(i, 1), right.push_back(i);
return right;
}
int main(){
cin.sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N; read_tree(0); solve(0); cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
416 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
126 ms |
1152 KB |
Output is correct |
2 |
Correct |
5 ms |
640 KB |
Output is correct |
3 |
Correct |
134 ms |
1372 KB |
Output is correct |
4 |
Correct |
189 ms |
1400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1073 ms |
3580 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
2888 KB |
Output is correct |
2 |
Execution timed out |
1087 ms |
6840 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1091 ms |
20600 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
24 ms |
7040 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
24 ms |
6904 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
25 ms |
7808 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
25 ms |
7544 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |