-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_heap_submission.py
79 lines (62 loc) · 2.11 KB
/
build_heap_submission.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
# python3
class HeapBuilder:
def __init__(self):
self._swaps = []
self._data = []
self._size = -1
def ReadData(self):
n = int(input())
self._data = [int(s) for s in input().split()]
assert n == len(self._data)
self._size = n
def WriteResponse(self):
print(len(self._swaps))
for swap in self._swaps:
print(swap[0], swap[1])
def GenerateSwapsNaive(self):
# The following naive implementation just sorts
# the given sequence using selection sort algorithm
# and saves the resulting sequence of swaps.
# This turns the given array into a heap,
# but in the worst case gives a quadratic number of swaps.
#
# TODO: replace by a more efficient implementation
for i in range(len(self._data)):
for j in range(i + 1, len(self._data)):
if self._data[i] > self._data[j]:
self._swaps.append((i, j))
self._data[i], self._data[j] = self._data[j], self._data[i]
def GenerateSwaps(self):
for i in range(self._size // 2, -1, -1):
self.SiftDown(i)
def SiftDown(self, i):
max_index = i
left_child = LeftChild(i)
if left_child < self._size and self._data[left_child] < self._data[max_index]:
max_index = left_child
right_child = RightChild(i)
if right_child < self._size and self._data[right_child] < self._data[max_index]:
max_index = right_child
if i != max_index:
self._swaps.append((i, max_index))
self._data[max_index], self._data[i] = self._data[i], self._data[max_index]
self.SiftDown(max_index)
def Solve(self):
self.ReadData()
self.GenerateSwaps()
self.WriteResponse()
def Parent(i):
if i == 0:
return None
return int(i - 1 // 2)
def LeftChild(i):
if i == 0:
return 1
return int(2*i + 1)
def RightChild(i):
if i == 0:
return 2
return int(2*i + 2)
if __name__ == '__main__':
heap_builder = HeapBuilder()
heap_builder.Solve()