forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path(HACKERRANK) - Maximum Element - DS.cpp
64 lines (49 loc) · 1.24 KB
/
(HACKERRANK) - Maximum Element - DS.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stack>
#include <sstream>
#include <string>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n, query_type, query_value;
string query_line;
stack<int> s, max_val;
cin>>n;
cin.ignore();
for(int i=0; i<n; i++)
{
getline(cin, query_line);
istringstream ss(query_line);
ss>>query_type>>query_value;
switch(query_type)
{
case 1:
{
s.push(query_value);
if(max_val.empty() || max_val.top() <= query_value)
{
max_val.push(query_value);
}
break;
}
case 2:
{
if(s.top() == max_val.top())
{
max_val.pop();
}
s.pop();
break;
}
default:
{
cout<<max_val.top()<<endl;
}
}
}
return 0;
}