forked from Ashishgup1/Competitive-Coding
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxqueue.cpp
More file actions
93 lines (92 loc) · 1.88 KB
/
Maxqueue.cpp
File metadata and controls
93 lines (92 loc) · 1.88 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
struct maxqueue
{
stack<pii>s1;
stack<pii>s2;
maxqueue() {}
void push (int x)
{
if(s1.empty())
s1.push({x,x});
else
{
s1.push({x,max(x,(int)s1.top().ss)});
}
}
void pop_front()
{
if(s2.empty())
{
while(!s1.empty())
{
int p = s1.top().ff;
s1.pop();
int next = s2.empty() ? p : max(s2.top().ss,p) ;
s2.push({p,next});
}
}
int ans = s2.top().ff;
s2.pop();
}
int front()
{
if(s2.empty())
{
while(!s1.empty())
{
int p = s1.top().ff;
s1.pop();
int next = s2.empty() ? p : max(s2.top().ss,p) ;
s2.push({p,next});
}
}
int ans = s2.top().ff;
s2.pop();
return ans;
}
void pop_rear()
{
if(s1.empty())
{
while(!s2.empty())
{
int p = s2.top().ff;
int val = s1.empty() ? p : max(p,s1.top().ff);
s1.push({p,val});
s2.pop();
}
}
int val = s1.top().ff;
s1.pop();
}
int rear()
{
if(s1.empty())
{
while(!s2.empty())
{
int p = s2.top().ff;
int val = s1.empty() ? p : max(p,s1.top().ff);
s1.push({p,val});
s2.pop();
}
}
int val = s1.top().ff;
return val;
}
int maximum()
{
int val = -1;
for(auto s : {s1,s2})
{
if(s.size())
{
val = max(s.top().ss,val);
}
}
return val;
}
int size()
{
return s1.size()+s2.size();
}
};