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;
- }
-
相关阅读:
EPICS简单实例1 -- 向IOCSH注册自己的函数
JS-对象
【09】Spring源码-分析篇-DI源码分析
Scala数组常用函数(1)
嵌入式分享合集105
实验六 移位寄存器的设计【Verilog】
如何在 Unbuntu 下安装配置 Apache Zookeeper
【招聘岗位】基础设施软件开发人员
ASP.NET Core 6框架揭秘实例演示[04]:自定义依赖注入框架
【EasyRL学习笔记】第五章 Proximal Policy Optimization 近端策略优化算法
-
原文地址:https://blog.csdn.net/weixin_52030057/article/details/133128243
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU