随机提取中位数
来源:互联网 发布:网络硬盘录像机怎么用 编辑:程序博客网 时间:2024/06/17 07:30
题目:Numbers are randomly generated and stored into an (expanding) array. How would you keep track of the median?
思路分析:使用两个堆,一个最大堆(存储小于中位数的一半的元素),一个最小堆(存储大于中位数的一半的元素),只要维护好这两个堆,根据堆顶元素就能得到中位数。我们的程序保证左右两个堆的元素个数差不能超过1,设最大堆堆顶元素为x1,最小堆堆顶元素为x2,则若插入元素x>x2,则将该元素插入最小堆中,若最小堆元素个数-最大堆元素个数>1,将最小堆堆顶元素弹出到最大堆中去;若x1<=x<=x2,插入任意一个堆中;若x<x1,同x>x2的情况。
#include<iostream>#include<queue>using namespace std;//最后的> >中间要有一个空格,避免被编译器理解为移位操作//大顶堆 priority_queue<int,vector<int>,less<int> > maxheap;//小顶堆 priority_queue<int,vector<int>, greater<int> > minheap;void insert(int x){//insert into any heapif(maxheap.size()==0&&minheap.size()==0)maxheap.push(x);//rebalance it else if(maxheap.size()==0&&minheap.size()==1){if(x>minheap.top()){maxheap.push(minheap.top());minheap.pop();minheap.push(x);}else{maxheap.push(x);}}//rebalance itelse if(maxheap.size()==1&&minheap.size()==0){if(x<maxheap.top()){minheap.push(maxheap.top());maxheap.pop();maxheap.push(x);}else{minheap.push(x);}}//insertelse{if(x>minheap.top()){minheap.push(x);if(minheap.size()-maxheap.size()>1){int tmp=minheap.top();minheap.pop();maxheap.push(tmp);}}else if(x<maxheap.top()){maxheap.push(x);if(maxheap.size()-minheap.size()>1){int tmp=maxheap.top();maxheap.pop();maxheap.push(tmp);}}else{//插入任意一个 if(maxheap.size()==minheap.size()){maxheap.push(x);}else if(maxheap.size()-minheap.size()==1){minheap.push(x);}else{maxheap.push(x);}}}}int getMid(){if(minheap.size()-maxheap.size()==1) return minheap.top();else if(maxheap.size()-minheap.size()==1)return maxheap.top();elsereturn min(maxheap.top(),minheap.top());//这一句很重要,可能出现最大堆是:5 5 最小堆是:6 6 这种情况,我们必须取两者中的最小值。}
- 随机提取中位数
- oracle提取随机记录
- 随机生成数字,并记录中位数
- SQL实现随机提取数据
- 随机提取10条记录
- 随机提取N条记录
- 随机提取N条记录
- 随机提取N条记录
- 巧用RAND()提取随机行
- 随机选择(求中位数,第i 小的元素)
- 中位数
- 中位数
- 中位数
- 中位数
- 中位数
- 中位数
- 中位数
- 中位数
- 左移<<和右移
- 使用openlayers开源组件建立地图应用
- SQLITE3 使用总结
- 软链接和硬链接
- Android 动态加载APK--代码安装、获取资源及Intent调用已安装apk
- 随机提取中位数
- HDU 2037 今年暑假不ac
- Android WebView的loadData方法注意事项
- NOIP2011 观光公交
- poj 1456 并查集
- 1011. Sticks(木棒) -POJ
- QT界面美化
- 类成员方法的内联写法
- struts2启动服务器时自定义初始化