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;
- }
-
相关阅读:
processflow流程图多人协作预热
深度学习六十年简史
Flutter高仿微信-第53篇-群聊-删除并退出
使用Python爬取信息403解决,并统计汇总绘制直方图,柱状图,折线图
解决hive表新增的字段查询为空null问题
解决问题:Unable to connect to Redis
成都瀚网科技有限公司:抖店的评论会消失吗?
Git提交代码仓库的两种方式
行业追踪,2023-09-12
浅谈软件测试面试一些常见的问题
-
原文地址:https://blog.csdn.net/weixin_52030057/article/details/133128243