-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMinMaxQ.cpp
54 lines (45 loc) · 1.15 KB
/
MinMaxQ.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
/*
* Author : Nikhil Garg
* Date : 2010-12-31
* A templatized MinMax Queue implementation.
* Provides O(1) insert, delete, fimnMin and findMax opertions.
* Assumption : all elements pushed in queue are different. This assumption can be
* relaxed after some extra book keeping code.
*
* I'm pre allocating space for queues. This can also be changed by replacing vectors with STL queues.
*
*/
#include<vector>
using namespace std;
template <class T>
class MinMaxQueue
{
vector<T> Q, Qmax, Qmin;
int qf, qmaxf, qminf, qb, qmaxb, qminb;
public :
MinMaxQueue(int N)
{
qf = qb = 0; Q.resize(N);
qmaxf = qmaxb = 0; Qmax.resize(N);
qminf = qminb = 0; Qmin.resize(N);
}
void push(T v)
{
while( qmaxf < qmaxb && Qmax[qmaxb -1] < v) qmaxb--;
while( qminf < qminb && Qmin[qminb -1] > v) qminb--;
Q[qb++] = v;
Qmax[qmaxb++] = v;
Qmin[qminb++] = v;
}
T pop()
{
T v = Q[qf++];
if (v == Qmax[qmaxf]) qmaxf ++;
if (v == Qmin[qminf]) qminf ++;
return v;
}
T front() { return Q[qf]; }
T getMax() { return Qmax[qmaxf]; }
T getMin() { return Qmin[qminf]; }
int size() { return qb - qf; }
};