We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Data Structures
- Heap
- QHEAP1
- Discussions
QHEAP1
QHEAP1
Sort by
recency
|
421 Discussions
|
Please Login in order to post a comment
I learned from Google AI, but I think I improved upon it :)
import heapq
myheap = [] heapq.heapify(myheap)
Q = int(input()) valid = set()
for _ in range(Q):
Are we supposed to implement the heap ourselves or can we use Python's built-in heapq for the heap?
How is this easy lol
Try this One:- python3-
from sys import stdin from heapq import heappush, heappop
heap = [] # Min-heap item_lookup = set() # Set to track valid elements
def push(v): heappush(heap, v) item_lookup.add(v)
def discard(v): item_lookup.discard(v)
def print_min(): # Remove elements from heap that are no longer valid while heap[0] not in item_lookup: heappop(heap)
cmds = { 1: push, 2: discard, 3: print_min }
n = int(stdin.readline()) for _ in range(n): data = list(map(int, stdin.readline().split(" "))) cmdsdata[0]