1.使用两个multiset维护中值,up和down分别存储一半的数据。
2.multiset允许元素重复,set元素不重复。
两者默认升序排列,降序使用multiset
>set1; 3.常用函数:
insert(int x)
find(int x) 返回迭代器
earse(iterator it)
lower_bound(int x) 返回第一个>=x的迭代器
upper_bound
size
- #include
-
- using namespace std;
- stack<int> st;
- multiset<int> up,down;
- void ba(){
- while(up.size()>down.size()){
- down.insert(*up.begin());
- up.erase(up.begin());
- }
- while(down.size()>up.size()+1){
- auto it=down.end();it--;
- up.insert(*it);
- down.erase(it);
- }
- }
- int main(){
- int n;
- cin>>n;
- for(int i=0;i
- string op;int num;
- cin>>op;
- if(op=="Push"){
- cin>>num;
- st.push(num);
- //判断
- if(up.empty()||*up.begin()>num) down.insert(num);
- else up.insert(num);
- ba();
- }
- else if(op=="Pop"){
- if(st.empty())cout<<"Invalid\n";
- else{
- int x=st.top();
- auto it=down.end();it--;//up可能为空
- if(*it < x ){
- up.erase(up.find(x));
- }
- else down.erase(down.find(x));
- ba();
- cout<
top()< - st.pop();
- }
- }
- else{
- if(st.empty())cout<<"Invalid\n";
- else{
- auto it=down.end();it--;
- cout<<*it<
- }
- }
- }
- return 0;
- }
-
相关阅读:
C程序的编译过程及生成文件详解
程序的编译和链接
基于多尺度注意力网络单图像超分(MAN)
如何使用 Docker Buildx Bake 创建复杂的镜像构建管道
随机6位数唯一标识
如何使用C++ 在Word文档中创建列表
ESP8266-Arduino编程实例-MQ3酒精传感器驱动
企业分账如何帮助用户解决成本优化和预算分配的问题
GBASE 8s中onshutdown 脚本的用法
【 C++ 】二叉搜索树
-
原文地址:https://blog.csdn.net/weixin_52030057/article/details/133128243