-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunningMedian.cpp
More file actions
306 lines (266 loc) · 6.87 KB
/
runningMedian.cpp
File metadata and controls
306 lines (266 loc) · 6.87 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define MAX_HEAP_SIZE 100000
#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
using namespace std;
//// Utility functions
// This function exchanges a and b.
inline void exch(int &a, int &b)
{
int temp =a ;
a = b;
b = temp;
}
// Greater and Smaller are used as comparators
//can't be named greater because when getting the address of the function-&greater, it referes to a reserved function greater.
bool Greater(int a , int b){
return a>b;
}
bool Smaller(int a, int b){
return a < b;
}
float average(int a, int b){
return float(a+b)/2.0;
}
// Signum function
// = 0 if a == b - heaps are balanced
// = -1 if a < b - left contains less elements than right
// = 1 if a > b - left contains more elements than right
inline int signum(int a , int b){
if (a == b)
return 0;
return a < b ? -1 : 1;
}
class Heap
{
public:
// Initializes heap array and comparator required
// in heapification
Heap(int * b, bool (*c)(int, int)): A(b), comp(c)
{
heapSize = -1;
}
virtual ~Heap()
{
if(A)
{
delete[]A;
}
}
//
virtual bool insert(int e) = 0;
virtual int gettop() = 0;
virtual int extracttop() = 0;
virtual int getcount() = 0;
protected:
int left(int i )
{
return 2*i + 1;
}
int right(int i){
return 2*(i+1);
}
int parent(int i){
if( i <= 0){
return -1;
}
return (i-1)/2;
}
//array of heap
int * A;
//comparator
bool (*comp)(int, int);
//heapSize
int heapSize;
//return the top element of the heap
int top(){
int max = -1;
if(heapSize>=0){
max = A[0];
}
return max;
}
//Returns the number of elements in the heap
int count(){
return heapSize +1;
}
void heapify(int i)
{
int p = parent(i);
if(p >= 0 && comp(A[i], A[p])){
exch(A[i], A[p]);
heapify(p);
}
}
void heapifydown(int i){
int index = i;
int lchild = left(index);
if(lchild < count()){
int minimum = lchild;
int rchild = right(index);
if(rchild < count()){
if(comp(A[rchild],A[lchild])){
minimum = rchild;
}
}
if(comp( A[minimum], A[index])){
exch(A[index], A[minimum]);
}
heapifydown(minimum);
}
}
// Deletes root of heap
int deleteTop(){
int del = -1;
if(heapSize > -1){
del = A[0];
exch(A[0], A[heapSize]);
heapSize--;
heapifydown(0);
}
return del;
}
bool insertHelper(int key){
bool ret = false;
if(heapSize < MAX_HEAP_SIZE){
ret = true;
heapSize++;
A[heapSize] = key;
heapify(heapSize);
}
return ret;
}
};
class MaxHeap: public Heap
{
private:
public:
MaxHeap(): Heap(new int[MAX_HEAP_SIZE], &Greater){}
~MaxHeap(){}
// Wrapper to return root of Max Heap
int gettop()
{
return top();
}
// Wrapper to delete and return root of Max Heap
int extracttop()
{
return deleteTop();
}
// Wrapper to return # elements of Max Heap
int getcount()
{
return count();
}
// Wrapper to insert into Max Heap
bool insert(int key)
{
return insertHelper(key);
}
};
class MinHeap: public Heap
{
private:
public:
MinHeap(): Heap(new int[MAX_HEAP_SIZE], &Smaller){}
~MinHeap(){}
// Wrapper to return root of Min Heap
int gettop()
{
return top();
}
// Wrapper to delete and return root of Min Heap
int extracttop(){
return deleteTop();
}
// Wrapper to return # elements of Min Heap
int getcount(){
return count();
}
// Wrapper to insert into Min Heap
bool insert(int key)
{
return insertHelper(key);
}
};
float getMedian(int e, float &m, Heap &l, Heap &r){
int sig = signum(l.getcount(), r.getcount());//verify if the heaps are balanced.
switch(sig)
{
case 1: //The heap in the left has more elements than the heap in the right.
if(e < m){
r.insert(l.extracttop()); //we remove the top element from the left and move it to the right
l.insert(e); // insert element to the left
}
else
{
r.insert(e); //the element can be inserted into the right heap since it has few elements and it's greater than the median.
}
m = average(l.gettop(), r.gettop()); //both heaps contain the same number of elements now, so we take the average.
break;
case 0: //both heaps contain the same number of elements.
if(e < m)
{
l.insert(e); //insert new elements and he leaft heap contains 1 more elements than the right heap
m = l.gettop();//the median now becomes the top of the left heap since it contains 1 more elements than the right heap, and its top elements is the greater among the elements in the left heap.
}
else
{
r.insert(e);
m = r.gettop();
}
break;
case -1: //the right heap contains more elements than the left heap
if(e<m){
l.insert(e); //insert into the left heap since it has less elements
}
else
{
l.insert(r.extracttop()); // move the top element to the left heap since it contains 1 more element than the left heap.
r.insert(e); //insert elements into the right heap, both heaps now constain the same number of elements
}
m = average(l.gettop(), r.gettop());
break;
}
return m;
}
int main(){
Heap * right = new MinHeap();
Heap * left = new MaxHeap();
float median = -1.0;
int sizev =0;
int n;
cin >> n;
vector<int> a(n);
for(int a_i = 0;a_i < n;a_i++){
cin >> a[a_i];
}
sizev = a.size();
for(int b = 0; b < sizev; b++){
median = getMedian(a[b], median, *left, *right);
printf("%.1lf\n",median);
}
delete left;//deallocate memory
delete right;//deallocate memory
return 0;
}