ivolff

Untitled

Oct 21st, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. import heapq, sys
  2. sys.stdout = open("priorityqueue.out", "w")
  3. queue = []
  4. operations = open("priorityqueue.in").read().strip().split("\n")
  5. for op in operations:
  6.     op_parsed = op.split()
  7.     if op_parsed[0] == "push":
  8.         heapq.heappush(queue, int(op_parsed[1]))
  9.     elif op_parsed[0] == "extract-min":
  10.         try:
  11.             print(heapq.heappop(queue))
  12.         except IndexError:
  13.             print("*")
  14.     else:
  15.         queue.pop(int(op_parsed[1]) - 1)
  16.         while int(op_parsed[1]) - 1 < len(queue):
  17.             heapq._siftup(queue, int(op_parsed[1]) - 1)
  18.             heapq._siftdown(queue, 0 , int(op_parsed[1]) - 1)
  19.         heapq.heappush(queue, int(op_parsed[2]))
Advertisement
Add Comment
Please, Sign In to add comment