forked from Dev-XYS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.cpp
69 lines (57 loc) · 818 Bytes
/
Queue.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
65
66
67
68
69
#include <cstdio>
#define QUEUE_SIZE 100000
using namespace std;
int Q[QUEUE_SIZE], hp = 0, tp = 0, M[QUEUE_SIZE], mhp = 0, mtp = 0;
void enqueue(int x)
{
Q[tp++] = x;
tp %= QUEUE_SIZE;
while (mhp != mtp && x > M[mhp])
{
mtp = (mtp + 1) % QUEUE_SIZE;
}
M[mtp++] = x;
mtp %= QUEUE_SIZE;
}
int dequeue()
{
int ret = Q[hp++];
hp %= QUEUE_SIZE;
if (ret == M[mhp])
{
mhp = (mhp + 1) % QUEUE_SIZE;
}
return ret;
}
bool empty()
{
return hp == tp;
}
int get_max()
{
return M[mhp];
}
int main()
{
while (true)
{
// 1 : enqueue; 2 : dequeue; 3 : get_max.
int o, x;
scanf("%d", &o);
switch (o)
{
case 1:
scanf("%d", &x);
enqueue(x);
break;
case 2:
x = dequeue();
printf("%d\n", x);
break;
case 3:
printf("%d\n", get_max());
break;
}
}
return 0;
}