一个关于栈和最小值的题目

发布时间 2023-12-07 01:38:58作者: towboat

 

网上冲浪无意看到的qaq

 

现在有一个栈,有出栈和入栈的基本操作,还有求当前栈中的最小值

 

 

不知道对不对的code

#include <queue>
#include <cstring>
#include <stack>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
priority_queue<int,vector<int> ,greater<int> > q;
stack<int> s; 
map<int,int>mp ;

signed main(){
	int op;
	while(cin>>op){
		
		if(op==0){  
			int x; cin>>x; s.push(x); mp[x]++; q.push(x); 
		}
		else if(op==1){
			int t = s.top() ; s.pop();
			mp[t] --;
		}
		else{
			while(q.size()){
				
				int t =q.top() ;
				if(mp[t]) { cout<<t<<endl ; break ;} else q.pop() ;
			}
			
		}
	}
}