#include <queue> class Solution { private: priority_queue<int, vector<int>, less<>> max_heap; // 最大堆 priority_queue<int, vector<int>, greater<>> min_heap; // 最小堆 public: void Insert(int num) { if (max_heap.empty() || num <= max_heap.top()) { max_heap.push(num); } else { min_heap.push(num); } // 保持两个堆的大小平衡 if (max_heap.size() > min_heap.size() + 1) { min_heap.push(max_heap.top()); max_heap.pop(); } else if (min_heap.size() > max_heap.size()) { max_heap.push(min_heap.top()); min_heap.pop(); } } double GetMedian() { if (max_heap.size() == min_heap.size()) { return (max_heap.top() + min_heap.top()) / 2.0; } else { return max_heap.top(); } } };