Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def leftChild(i):
- return i << 1
- def rightChild(i):
- return i << 1 | 1
- def heapifyMax(a, length, i):
- left = leftChild(i)
- right = rightChild(i)
- largest = i
- if left < length and a[left] > a[largest]:
- largest = left
- if right < length and a[right] > a[largest]:
- largest = right
- if largest is not i:
- a[i], a[largest] = a[largest], a[i]
- heapifyMax(a, length, largest)
- def heapifyMin(a, length, i):
- left = leftChild(i)
- right = rightChild(i)
- largest = i
- if left < length and a[left] < a[largest]:
- largest = left
- if right < length and a[right] < a[largest]:
- largest = right
- if largest is not i:
- a[i], a[largest] = a[largest], a[i]
- heapifyMin(a, length, largest)
- def buildMaxHeap(a, length):
- start = int((length-1) >> 1)
- for i in range(start, 0, -1):
- heapifyMax(a, length, i)
- def buildMinHeap(a, length):
- start = int((length-1) >> 1)
- for i in range(start, 0, -1):
- heapifyMin(a, length, i)
- maxHeap = [0]
- minHeap = [0]
- totalElement = int(input())
- fnum = -1
- snum = -1
- for f in range(1, 3):
- if (f == 1):
- num = int(input())
- fnum = max(fnum, num)
- print("{}.0".format(fnum))
- elif (f == 2):
- num = int(input())
- snum = max(snum, num)
- print(round((fnum+snum)/2, 1))
- minHeap.append(max(fnum, snum))
- maxHeap.append(min(fnum, snum))
- for f in range(3, totalElement+1, 1):
- num = int(input())
- if maxHeap[1] > num:
- maxHeap.append(num)
- buildMaxHeap(maxHeap, len(maxHeap))
- else:
- minHeap.append(num)
- buildMinHeap(minHeap, len(minHeap))
- maxHeapLen = len(maxHeap)
- minHeapLen = len(minHeap)
- if minHeapLen > maxHeapLen:
- m = minHeap[1]
- minHeap.pop(1)
- maxHeap.append(m)
- buildMaxHeap(maxHeap, len(maxHeap))
- elif maxHeapLen > minHeapLen:
- m = maxHeap[1]
- maxHeap.pop(1)
- minHeap.append(m)
- buildMinHeap(minHeap, len(minHeap))
- maxHeapLen = len(maxHeap)
- minHeapLen = len(minHeap)
- if maxHeapLen > minHeapLen:
- median = maxHeap[1] * 2 / 2
- elif minHeapLen > maxHeapLen:
- median = minHeap[1] * 2 / 2
- elif minHeapLen is maxHeapLen:
- median = (minHeap[1] + maxHeap[1])/2
- print(round(median, 1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement