用堆来求中位数

 维护一个大根堆和一个小根堆。使得大根堆堆顶(最大的元素)比小根堆堆顶(最小的元素)小,且两个堆的元素个数的差小于等于1。这样元素多的那个堆的堆顶就是已读入数的中位数。如果读入偶数个数,则中位数为两个堆堆顶的平均数。

洛谷P1168 

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<queue>
 4 #include<cmath>
 5 using namespace std;
 6 
 7 priority_queue<int> big;
 8 priority_queue<int,vector<int>,greater<int> > small;
 9 int a[100010];
10 
11 int main(){
12     int n;
13     cin >> n;
14     for(int i = 1; i <= n; i++) 
15         cin >> a[i];
16     big.push(a[1]);
17     cout << a[1] << endl;
18     for(int i = 2; i <= n; i++){
19         if(a[i] > big.top()) small.push(a[i]);
20         else big.push(a[i]);
21         while(abs(int(big.size() - small.size())) > 1){
22             if(big.size() > small.size())
23                 small.push(big.top()) , big.pop();
24             else
25                 big.push(small.top()) , small.pop();
26         }
27         if(i % 2)
28             cout << (big.size() > small.size() ? big.top() : small.top()) << endl;
29     }
30     
31     return 0;
32 }

猜你喜欢

转载自www.cnblogs.com/FoxC/p/10581950.html