Posted on

Description

Submission

#include <cmath>
#include <cstdio>
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;

struct StackNode
{
    int value;
    StackNode* prevMax;
    StackNode(int value, StackNode* prevMax)
        :
        value(value),
        prevMax(prevMax)
    {}
};

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    int N; cin >> N;

    stack<StackNode*> stk;

    StackNode* max = NULL;
    while(N--) {
        int operand; cin >> operand;
        if(operand == 1) {
            int x; cin >> x;
            StackNode* node = new StackNode(x, max);
            if(node->prevMax == NULL) node->prevMax = node;
            if(max == NULL || (max != NULL && x >= max->value)) max = node;
            stk.push(node);
        }
        if(operand == 2) {
            max = stk.top()->prevMax;
            stk.pop();
            if(stk.empty()) max = NULL;
        }
        if(operand == 3) {
            cout << max->value << endl;
        }
    }  

    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *