-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcookie_selection.py
65 lines (48 loc) · 1.63 KB
/
cookie_selection.py
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
"""
We need to keep a list of sorted elements.
When the input is #, we get the median element from the list
if L is a sorted list of size n,
self.lower_half is a max heap that contains the first n/2 elements
self.upper_half is a min heap that contains the last n/2 elements
The median element will always be self.upper_half[0]
"""
import heapq
class MedianHeap:
def __init__(self):
self.upper_half = []
self.lower_half = []
def pop(self):
result = heapq.heappop(self.upper_half)
self._recalculate_heaps()
return result
def push(self, value):
median = self._get_median()
if value > median:
heapq.heappush(self.upper_half, value)
else:
heapq.heappush(self.lower_half, -value)
self._recalculate_heaps()
def _get_median(self):
if len(self.upper_half) == 0 and len(self.lower_half) == 0:
return -1
return self.upper_half[0]
def _recalculate_heaps(self):
while len(self.lower_half) - len(self.upper_half) < -1:
result = heapq.heappop(self.upper_half)
heapq.heappush(self.lower_half, -result)
while len(self.lower_half) - len(self.upper_half) > 0:
result = heapq.heappop(self.lower_half)
heapq.heappush(self.upper_half, -result)
median_heap = MedianHeap()
if __name__ == '__main__':
try:
while True:
line = input()
if not line:
break
if line == "#":
print(median_heap.pop())
else:
median_heap.push(int(line))
except EOFError:
pass