博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
P1168 中位数
阅读量:6973 次
发布时间:2019-06-27

本文共 2181 字,大约阅读时间需要 7 分钟。

一个大根堆,一个小根堆。胡乱搞搞就可以了,根据中位数的计算方法。

#include
#include
#include
struct Min_heap{ int tail; int data[100010]; int top() { return data[1]; } void swap(int &a,int &b) { int c=a; a=b; b=c; return ; } void insert(int value) { data[++tail]=value; int pos=tail; while(pos&&data[pos>>1]>data[pos]) { swap(data[pos>>1],data[pos]); pos>>=1; } return ; } void del() { swap(data[1],data[tail]); tail--; int pos=1,pass; while(pos<=tail) { pass=pos; if(data[pass]>data[pos<<1]&&(pos<<1)<=tail) pass=pos<<1; if(data[pass]>data[pos<<1|1]&&(pos<<1|1)<=tail) pass=pos<<1|1; if(pass==pos) break; swap(data[pass],data[pos]); pos=pass; } return ; }};struct Max_heap{ int tail; int data[100010]; int top() { return data[1]; } void swap(int &a,int &b) { int c=a; a=b; b=c; return ; } void insert(int value) { data[++tail]=value; int pos=tail; while(pos&&data[pos>>1]
>1) { swap(data[pos>>1],data[pos]); pos>>=1; } return ; } void del() { swap(data[1],data[tail]); tail--; int pos=1,pass; while(pos<=tail) { pass=pos; if(data[pass]
<<1]&&(pos<<1)<=tail) pass=pos<<1; if(data[pass]
<<1|1]&&(pos<<1|1)<=tail) pass=pos<<1|1; if(pass==pos) break; swap(data[pass],data[pos]); pos=pass; } return ; }};Min_heap h1;Max_heap h2;int main(){ int n; scanf("%d",&n); int a; for(int i=1;i<=n;i++) { scanf("%d",&a); /*h1.insert(a); if(i%2) { h2.insert(h1.top()); h1.del(); printf("%d ",h2.top()); }*/ /*if(h1.top()>a) h2.insert(a); else*/ h1.insert(a); if(h1.tail>h2.tail-1) while(h1.tail>h2.tail-1) { h2.insert(h1.top()); h1.del(); } if(h1.tail

转载于:https://www.cnblogs.com/Lance1ot/p/8762065.html

你可能感兴趣的文章
T51658 【wsy】签到题
查看>>
mysql 控制台上传数据库
查看>>
洛谷P1196 银河英雄传说
查看>>
aop为系统添加操作日志,注入或配置声明的方式来实现
查看>>
好用的日期控件jeDate
查看>>
Ajax学习之------>Ajax和Json实现无限下拉框联动(上)
查看>>
古今之成大事业、大学问者,必经过三种之境界
查看>>
我的Android进阶之旅------>Android中编解码学习笔记
查看>>
我的Android进阶之旅------>android如何将List请求参数列表转换为json格式
查看>>
转载:负载均衡器技术Nginx和F5的优缺点对比
查看>>
【资源共享】5G AP分析
查看>>
APP测试与Web测试的区别
查看>>
模式识别,计算机视觉领域,期刊
查看>>
AngularJs的UI组件ui-Bootstrap分享(三)——Accordion
查看>>
中缀、前缀和后缀表达式
查看>>
Redis 自定义对象 cannot be cast to java.lang.String
查看>>
[题解]第十一届北航程序设计竞赛预赛——H.高中数学题
查看>>
内置对象Array及Array常见操作
查看>>
oracle 表字段新增、修改、删除、重命名以及表重命名
查看>>
Python连接MySQL之Python库pymysql
查看>>