Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- class StackMaxEffective {
- public:
- void push(const int element) {
- elements_.push_back(element);
- max_elements_.empty() ? max_elements_.push_back(element) : max_elements_.push_back(std::max(element, max_elements_.back()));
- }
- void pop() {
- if (elements_.empty()) {
- std::cout << "error" << std::endl;
- }
- else {
- elements_.pop_back();
- max_elements_.pop_back();
- }
- }
- void get_max() {
- if (elements_.empty()) {
- std::cout << "None" << std::endl;
- }
- else {
- std::cout << max_elements_.back() << std::endl;
- }
- }
- void top() {
- if (elements_.empty()) {
- std::cout << "error" << std::endl;
- }
- else {
- std::cout << elements_.back() << std::endl;
- }
- }
- private:
- std::vector<int> elements_;
- std::vector<int> max_elements_;
- };
- int main() {
- std::ifstream fin("input.txt");
- std::string command;
- int count, num;
- fin >> count;
- StackMaxEffective stack;
- for (int i = 0; i < count; i++) {
- fin >> command;
- if (command == "pop") {
- stack.pop();
- }
- if (command == "push") {
- fin >> num;
- stack.push(num);
- }
- if (command == "get_max") {
- stack.get_max();
- }
- if (command == "top") {
- stack.top();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment