R
R
redfoo2018-06-03 04:07:05
Python
redfoo, 2018-06-03 04:07:05

Priority queue, speed?

Hello, I wrote an algorithm that implements priority queues based on max-heap, with the function of inserting and extracting the maximum element, according to my measurements, the running time does not go beyond O (logn), but, apparently, I am mistaken in something, since stepik does not makes a decision with the Time limit exceeded error, tell me what the error is.
5b133f083ce36737094407.png

from sys import stdin
from _heapq import _heappop_max
from _heapq import _heapify_max

class PriorityQueue:
  def __init__(self):
    self.queue = []
  
  def Insert(self, p):
    self.queue.append(p)
    _heapify_max(self.queue)

  def GetMax(self):
    return _heappop_max(self.queue)

def main():
  queues = PriorityQueue()
  reader = (tuple(line.split()) for line in stdin)
  n = next(reader)
  data = list(reader)
  for key in data:
    if key[0] == 'Insert':
      queues.Insert(int(key[1]))
    elif key[0] == 'ExtractMax':
      print(queues.GetMax())

if __name__ == '__main__':
  main()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
longclaps, 2018-06-03
@longclaps

Excuse me, with all due respect, you are a govnokoder. Something needs to be done about this, to begin with, to recognize this fact. Let's take the longest line:

reader = (tuple(map(str, line.split())) for line in stdin)
how it differs from the answer - by calling map , which nonsensically maps str to str . Why did you do it? There in the task (I was not too lazy, I googled it) there was a solution template with a working verification code, and for some reason you replaced it with a strange one, I won’t repeat what. In essence: the complexity of the algorithm is estimated not by measurements, but by brains. _heapify_max , source comments:
And where is O(logn) ?
Try to come up with a reasonable solution.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question