이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |