Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class MaximumElement
- {
- static void Main()
- {
- int n = int.Parse(Console.ReadLine());
- Stack<int> stack = new Stack<int>();
- Stack<int> stackMax = new Stack<int>();
- int max = 1;
- for (int i = 0; i < n; i++)
- {
- string[] query = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- string queryType = query[0];
- switch (queryType)
- {
- case "1":
- int elementX = int.Parse(query[1]);
- stack.Push(elementX);
- if (max < elementX)
- {
- max = elementX;
- stackMax.Push(max);
- }
- break;
- case "2":
- if (stack.Count > 0)
- {
- int deleted = stack.Pop();
- if (deleted == stackMax.Peek())
- {
- stackMax.Pop();
- max = 1;
- }
- }
- break;
- case "3":
- if (stack.Count > 0)
- {
- Console.WriteLine(stackMax.Peek());
- }
- break;
- default:
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment