后2个超时
查看原帖
后2个超时
1385996
ZSYhaouuan楼主2024/8/3 11:15

有什么更快找栈最大值的方法?

#include<bits/stdc++.h>
using namespace std;
long long n;
stack<long long> a;
long long b[200000+5];
int main(){
	int cnt=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		int how;
		cin>>how;
		if(how==0){
			long long x;
			cin>>x;
			a.push(x);
		}else if(how==1){
			a.pop();
		}else if(how==2){
			long long maxn=0;
			while(!a.empty()){
				maxn=max(a.top(),maxn);
				b[++cnt]=a.top();
				a.pop();
			}
			while(cnt!=0){
				a.push(b[cnt]);
				cnt--;
			}
			cout<<maxn<<'\n';
		}
	}
	return 0;
}
2024/8/3 11:15
加载中...