动态中位数(对顶堆)
作者:
jzz2.0
,
2024-01-31 12:21:46
,
所有人可见
,
阅读 131
struct DynamicMedian {
priority_queue<int> down;
priority_queue<int, vector<int>, greater<int>> up;
DynamicMedian() {}
void insert(int x) {
if (down.empty() || x <= down.top()) {
down.push(x);
} else {
up.push(x);
}
if (down.size() > 1 + up.size()) {
up.push(down.top());
down.pop();
}
if (up.size() > down.size()) {
down.push(up.top());
up.pop();
}
};
double Ans() {
if (up.size() + down.size() & 1) {
return down.top();
} else {
return (down.top() + up.top()) / 2.0;
}
};
};
## 很吊