#include <bits/stdc++.h>
using namespace std;
int n;
struct BIT {
unordered_map<int,int> tree;
vector<int> elems;
BIT() {}
void add(int v) {
elems.push_back(v);
for (; v < n; v|=v+1)
tree[v]++;
}
int sum(int v) {
int ans = 0;
for (; v >= 0; v = (v&(v+1))-1)
if (tree.count(v))
ans += tree[v];
return ans;
}
int merge(BIT &other) {
int ans = 0;
for (int x : other.elems)
ans += sum(x);
for (int x : other.elems)
add(x);
return ans;
}
};
int merge(BIT &a, BIT &b) {
if (a.elems.size() < b.elems.size())
swap(a,b);
int y = a.elems.size() * b.elems.size();
int x = a.merge(b);
return min(x, y-x);
}
BIT solve(int &ans) {
int x; cin>>x;
if (x) {
BIT ds;
ds.add(x-1);
return ds;
} else {
BIT a = solve(ans), b = solve(ans);
ans += merge(a,b);
return a.elems.size()>b.elems.size() ? a : b;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
int ans = 0;
solve(ans);
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
380 KB |
Output is correct |
3 |
Correct |
3 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
476 KB |
Output is correct |
2 |
Correct |
8 ms |
476 KB |
Output is correct |
3 |
Correct |
7 ms |
508 KB |
Output is correct |
4 |
Correct |
22 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
481 ms |
1744 KB |
Output is correct |
2 |
Correct |
44 ms |
888 KB |
Output is correct |
3 |
Correct |
504 ms |
1900 KB |
Output is correct |
4 |
Correct |
499 ms |
1932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1083 ms |
4984 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
854 ms |
6704 KB |
Output is correct |
2 |
Execution timed out |
1080 ms |
9720 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1086 ms |
37208 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1064 ms |
2388 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1072 ms |
11820 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1077 ms |
8448 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1074 ms |
6868 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |