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

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

https://www.luogu.org/problem/show?pid=1168

维护两个堆,一个大根堆L维护小于等于mid的数,一个小根堆R维护大于等于mid的数,mid是上次输出的中位数

然后进行调整即可

#include
#include
#include
#include
#define ms(i,j) memset(i,j, sizeof i);using namespace std;int n;priority_queue
L;//bigpriority_queue
, greater
> R;//smallint main(){ scanf("%d",&n); int x1,x2; scanf("%d", &x1); R.push(x1); int mid = x1; printf("%d\n", mid); for (int i=1;i<=(n+1)/2-1;i++) { scanf("%d%d", &x1, &x2); if (x1<=mid) L.push(x1); else R.push(x1); if (x2<=mid) L.push(x2); else R.push(x2); while (L.size() >= R.size()) {R.push(L.top()); L.pop();} while (L.size() < R.size()-1) {L.push(R.top()); R.pop();} printf("%d\n", R.top()); mid = R.top(); } return 0;}

转载于:https://www.cnblogs.com/flyinthesky1/p/6384241.html

你可能感兴趣的文章
黑马程序员--抽象类与接口
查看>>
IaaS,PaaS,SaaS 的区别
查看>>
Python复习基础篇
查看>>
关于Cocos2d-x中背景音乐和音效的添加
查看>>
checkbox和文字对齐
查看>>
%s的用法
查看>>
java中==和equals
查看>>
CCActionPageTurn3D
查看>>
python random
查看>>
esp32-智能语音-cli(调试交互命令)
查看>>
netty与MQ使用心得
查看>>
关于dl dt dd 文字过长换行在移动端显示对齐的探讨总结
查看>>
swoolefy PHP的异步、并行、高性能网络通信引擎内置了Http/WebSocket服务器端/客户端...
查看>>
Python学习笔记
查看>>
unshift()与shift()
查看>>
使用 NPOI 、aspose实现execl模板公式计算
查看>>
行为型模式:中介者模式
查看>>
How to Notify Command to evaluate in mvvmlight
查看>>
33. Search in Rotated Sorted Array
查看>>
461. Hamming Distance
查看>>