Advertisement
dipBRUR

Untitled

Jun 4th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1.  
  2.  
  3. def leftChild(i):
  4.     return i << 1
  5.  
  6. def rightChild(i):
  7.     return i << 1 | 1
  8.  
  9. def heapifyMax(a, length, i):
  10.     left = leftChild(i)
  11.     right = rightChild(i)
  12.    
  13.     largest = i
  14.    
  15.     if left < length and a[left] > a[largest]:
  16.         largest = left
  17.    
  18.     if right < length and a[right] > a[largest]:
  19.         largest = right
  20.        
  21.     if largest is not i:
  22.         a[i], a[largest] = a[largest], a[i]
  23.        
  24.         heapifyMax(a, length, largest)
  25.        
  26.  
  27. def heapifyMin(a, length, i):
  28.     left = leftChild(i)
  29.     right = rightChild(i)
  30.    
  31.     largest = i
  32.    
  33.     if left < length and a[left] < a[largest]:
  34.         largest = left
  35.    
  36.     if right < length and a[right] < a[largest]:
  37.         largest = right
  38.    
  39.     if largest is not i:
  40.         a[i], a[largest] = a[largest], a[i]
  41.        
  42.         heapifyMin(a, length, largest)
  43.  
  44.  
  45.  
  46. def buildMaxHeap(a, length):
  47.     start = int((length-1) >> 1)
  48.    
  49.     for i in range(start, 0, -1):
  50.         heapifyMax(a, length, i)
  51.  
  52.  
  53. def buildMinHeap(a, length):
  54.     start = int((length-1) >> 1)
  55.    
  56.     for i in range(start, 0, -1):
  57.         heapifyMin(a, length, i)
  58.  
  59. maxHeap = [0]
  60. minHeap = [0]
  61.  
  62. totalElement = int(input())
  63.  
  64. fnum = -1
  65. snum = -1
  66.  
  67. for f in range(1, 3):
  68.     if (f == 1):
  69.         num = int(input())
  70.        
  71.         fnum = max(fnum, num)
  72.        
  73.         print("{}.0".format(fnum))
  74.    
  75.     elif (f == 2):
  76.         num = int(input())
  77.        
  78.         snum = max(snum, num)
  79.        
  80.         print(round((fnum+snum)/2, 1))
  81.  
  82. minHeap.append(max(fnum, snum))
  83. maxHeap.append(min(fnum, snum))
  84.  
  85. for f in range(3, totalElement+1, 1):
  86.     num = int(input())
  87.    
  88.     if maxHeap[1] > num:
  89.         maxHeap.append(num)
  90.        
  91.         buildMaxHeap(maxHeap, len(maxHeap))
  92.    
  93.     else:
  94.         minHeap.append(num)
  95.        
  96.         buildMinHeap(minHeap, len(minHeap))
  97.    
  98.     maxHeapLen = len(maxHeap)
  99.     minHeapLen = len(minHeap)
  100.    
  101.     if minHeapLen > maxHeapLen:
  102.         m = minHeap[1]
  103.        
  104.         minHeap.pop(1)
  105.        
  106.         maxHeap.append(m)
  107.         buildMaxHeap(maxHeap, len(maxHeap))
  108.    
  109.     elif maxHeapLen > minHeapLen:
  110.         m = maxHeap[1]
  111.        
  112.         maxHeap.pop(1)
  113.        
  114.         minHeap.append(m)
  115.         buildMinHeap(minHeap, len(minHeap))
  116.    
  117.    
  118.     maxHeapLen = len(maxHeap)
  119.     minHeapLen = len(minHeap)
  120.    
  121.     if maxHeapLen > minHeapLen:
  122.         median = maxHeap[1] * 2 / 2
  123.    
  124.     elif minHeapLen > maxHeapLen:
  125.         median = minHeap[1] * 2 / 2
  126.    
  127.     elif minHeapLen is maxHeapLen:
  128.         median = (minHeap[1] + maxHeap[1])/2
  129.    
  130.     print(round(median, 1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement