4 solutions

  • 0
    @ 2022-10-25 21:29:43

    双栈,另外一个栈贮存最大值

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    stack<ll> s;
    stack<ll> m;\\储存最大值
    int main(){
        int k,x;
        ll mm;
        m.push(0);\\使m.top()一开始就存在
        while(scanf("%d",&k)!=EOF){
            for(int i=0;i<k;i++){
                cin>>x;
                if(x==1){
                    ll a;
                    cin>>a;
                    s.push(a);
                    mm=max(a,m.top());
                    m.push(mm);
                }
                else if(x==2){
                    s.pop();
                    m.pop();
                }
                else if(x==3){
                    if(m.top()!=0){
                        cout<<m.top()<<endl;
                    }else{
                        cout<<-1<<endl;
                    }
                }
            }
        }
    }
    

    Information

    ID
    1528
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    5
    Tags
    # Submissions
    73
    Accepted
    23
    Uploaded By