█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 6362

Problema 0xb0 - Implementar una cola de prioridad

  • Autor: Ikerlb
  • Fecha: 2022-06-27 04:59:07 UTC (Hace más de 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.014 s 3 KBi
#2
Correcto
0.033 s 3 KBi
#3
Correcto
0.017 s 3 KBi
#4
Correcto
0.019 s 3 KBi
#5
Correcto
0.035 s 3 KBi
#6
Correcto
0.032 s 3 KBi
#7
Correcto
0.019 s 3 KBi
#8
Correcto
0.014 s 3 KBi
#9
Correcto
0.033 s 3 KBi
#10
Tiempo límite excedido
1.514 s 14 KBi
#11
Correcto
0.481 s 16 KBi
#12
Correcto
1.29 s 10 KBi
#13
Correcto
0.947 s 15 KBi
#14
Correcto
0.966 s 15 KBi
#15
Correcto
0.464 s 10 KBi
#16
Tiempo límite excedido
1.517 s 16 KBi
#17
Correcto
0.988 s 16 KBi
Puntos totales: 89 / 100

Código

from functools import total_ordering

@total_ordering
class Node:
    def __init__(self, s, p):
        self.s = s
        self.p = -p

    def __lt__(self, other):
        if self.p < other.p:
            return True
        elif self.p > other.p:
            return False
        return self.s < other.s
    
    def __eq__(self, other):
        return self.s == other.s and self.p == other.p

    def __repr__(self):
        return f"{self.s}"

class Heap:
    def __init__(self):
        self.tree = []

    def right(self, i):
        return (i << 1) + 1

    def left(self, i):
        return (i << 1) + 2

    def parent(self, i):
        return (i - 1) >> 1

    def __len__(self):
        return len(self.tree)

    def heapify_down(self, i):
        l = self.left(i)
        r = self.right(i)
        children = [c for c in [l, r] if c < len(self.tree)]
        #print(i, children, self.tree)
        if all(self.tree[i] < self.tree[c] for c in children):
            return     
        sm = min(children, key = lambda x: self.tree[x])
        self.tree[sm], self.tree[i] = self.tree[i], self.tree[sm]
        self.heapify_down(sm)

    def heapify_up(self, i):
        while i != 0:
            p = self.parent(i)
            #print(f"{i=} {p=}")
            if self.tree[p] <= self.tree[i]:
                break    
            self.tree[p], self.tree[i] = self.tree[i], self.tree[p]
            i = p

    def add(self, elem):
        self.tree.append(elem)
        self.heapify_up(len(self.tree) - 1)


    def pop(self): 
        self.tree[-1], self.tree[0] = self.tree[0], self.tree[-1]
        res = self.tree.pop()
        self.heapify_down(0)
        return res

    def peek(self):
        return self.tree[0]

    def __repr__(self):
        return f"{self.tree}"    

h = Heap()
num_lines = int(input())
for _ in range(num_lines):
    line = input()
    if line[0] == "I":
        _, s, p = line.split(" ")
        n = Node(s, int(p))
        h.add(n)
    elif line[0] == "G":
        n = h.peek()
        print(n)
    else:
        h.pop()