-
Notifications
You must be signed in to change notification settings - Fork 0
/
t20.py
82 lines (63 loc) · 1.96 KB
/
t20.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import operator
class Heap:
def __init__(self, op=operator.lt):
self.arr = []
self.op = op
def insert(self, k):
arr = self.arr
pos = len(arr) # last element + 1
arr.append(k)
# sift-up
while pos > 0:
parent_pos = (pos - 1) // 2
if self.op(k, arr[parent_pos]): # swap
arr[parent_pos], arr[pos] = k, arr[parent_pos]
pos = parent_pos
else:
break
def extract(self):
arr = self.arr
op = self.op
if not arr: # empty heap
raise IndexError("Extracting from empty heap.")
if len(arr) == 1: # return single element
return arr.pop()
n = len(arr) - 1 # new length
ret = arr[0]
arr[0] = last = arr[-1] # move last to first, then sift-down
pos, child_pos = 0, 1
while child_pos < n:
# select left or right child
if op(arr[child_pos+1], arr[child_pos]):
child_pos += 1 # select right child
if op(arr[child_pos], arr[pos]): # swap
arr[child_pos], arr[pos] = arr[pos], arr[child_pos]
pos = child_pos
else:
break
child_pos = pos * 2 + 1
del arr[-1] # always delete the last one
return ret
def __len__(self):
return(len(self.arr))
def __iter__(self):
return self
def __next__(self):
try:
val = self.extract()
except IndexError:
raise StopIteration
return val
class MinHeap(Heap):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs, op=operator.lt)
class MaxHeap(Heap):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs, op=operator.gt)
# ===
n = int(input())
inumbers = map(int, input().split())
heap = MinHeap()
for num in inumbers:
heap.insert(num)
print(*heap)