def main(): d = dd(list) for _ in range(int(input())): name, like, bday = input().split() hpush(d[bday], (-int(like), name)) print(len(d)) print("\n".join(sorted(map(lambda z: hpop(z)[1], d.values()))))
def dijk(graph, start): """ Parameters ---------- graph : dict Keys are nodes, values are lists containing edges in the form of (weight, adjacent) start : int The root node where we will compute distances of other nodes. Returns ------- animation_dists: list List of dictionaries, each dictionary contains distances of nodes at a certain point in time order_visited: list An array with the order of nodes visited """ # Our adjacency list (the data structure is stored as a dictionary) is in the wrong format for the module HeapQ. # G is in the format {node : [[neighbor, weight]]} # Flip G to be in the format {node : [[weight, neighbor]]} order_visited = [start] dists = dict(zip(list(graph.keys()), [INF for i in range(len(graph.keys()))])) heap = [(0, start)] dists[start] = 0 animation_dists = [dists.copy()] while heap: cur = hpop(heap)[1] for wt, node in graph[cur]: if dists[cur] + wt < dists[node]: dists[node] = dists[cur] + wt hpush(heap, (dists[node], node)) order_visited.append(node) animation_dists.append(dists.copy()) return animation_dists, order_visited
def _floodfill(self): other_color_nodes = [] while not other_color_nodes: if not self.heap: raise EmptyHeapError() current_node = self.sidx[hpop(self.heap)] current_color = self.color[current_node] for node in self.gf.neighbors_idx(current_node): node_color = self.color[node] if node_color == current_color: continue if node_color == 0: self.color[node] = current_color self.p_idx[node] = current_node hpush(self.heap, self.inv_sidx[node]) else: other_color_nodes.append(self.inv_sidx[node]) node = self.sidx[min(other_color_nodes)] node_color = self.color[node] if current_color < node_color: return (current_node, node) else: return (node, current_node)
def add_task(self, task): # take next processor that become free and add task weight to it = it works for this time at this task #debug('adding task %s', task) processor = hpop(self.processors) if any(self.processors) else None task.time = processor[0] task.processor = processor[1] processor = (processor[0] + task.weight, processor[1]) hpush(self.processors, processor)
def _flood_init(self, idx1, idx2): self.heap = [] self.color[:] = 0 self.p_idx[:] = -1 self.color[idx1] = 1 self.color[idx2] = 2 hpush(self.heap, self.inv_sidx[idx1]) hpush(self.heap, self.inv_sidx[idx2])
def simulate(k): global N, T, cows heap = cows[:k] heapify(heap) for i in range(k, N): new = hpop(heap) + cows[i] if new > T: return False hpush(heap, new) return True
def minMeetingRooms(self, intervals: 'List[Interval]') -> 'int': if not intervals: return 0 rooms = [] intervals.sort( key=lambda x: x.start) # In-place sort is more memory efficient hpush(rooms, intervals[0].end) for i in intervals[1:]: if rooms[0] <= i.start: hpop(rooms) hpush(rooms, i.end) return len(rooms)
def minimumDeviation(self, nums: List[int]) -> int: q = [-a if a % 2 == 0 else -a * 2 for a in nums] hfy(q) min_val = -max(q) ret = -q[0] - min_val while q[0] % 2 == 0 : nxt = hpop(q) // 2 hpush(q, nxt) min_val = min(min_val, -nxt) ret = min(ret, -q[0] - min_val) return ret
def get_shorty(graph, n): height = [-1.0 for i in xrange(n)] pq = [] height[0] = 1.0 hpush(pq, (height[0] * -1, 0)) while len(pq) > 0: h, curr = hpop(pq) h = -1 * h for n, w in graph[curr]: new_height = h * w if height[n] == -1 or new_height > height[n]: height[n] = new_height hpush(pq, (new_height * -1, n)) return "{:.4f}".format(height[-1])
def maxResult(self, nums: List[int], k: int) -> int: n = len(nums) dp = [0] * n dp[0] = nums[0] q = [[-dp[0], 0]] for i in range(1, n): while q and q[0][1] + k < i: hpop(q) dp[i] = nums[i] - q[0][0] hpush(q, [-dp[i], i]) return dp[n - 1]
def mostCompetitive(self, nums: List[int], k: int) -> List[int]: n = len(nums) A = [] q = [] for i in range(n - k + 1): hpush(q, [nums[i], i]) A.append(hpop(q)) for i in range(n - k + 1, n): while q and q[0][1] < A[-1][1]: hpop(q) hpush(q, [nums[i], i]) A.append(hpop(q)) ret = [a[0] for a in A] return ret
def guided_refinement(rec_set, oracle, cost, prune=lambda *_: False): """Generator for iteratively approximating the oracle's threshold.""" # TODO: automatically apply bounding box computation. Yield that first. refiner = _refiner(oracle) next(refiner) queue = [(cost(rec), bounding_box(rec, oracle)) for rec in rec_set] heapify(queue) # TODO: when bounding box is implemented initial error is given by that while queue: # TODO: when bounding yield queue _, rec = hpop(queue) subdivided = refiner.send(rec) for r in subdivided: if prune(r): continue hpush(queue, (cost(r), r))
def addNum(self, num): """ :type num: int :rtype: None """ if self.lh == [] or -self.lh[0] >= num: hpush(self.lh, -num) else: hpush(self.rh, num) if len(self.lh) > len(self.rh): hpush(self.rh, -hpop(self.lh)) if len(self.lh) < len(self.rh): hpush(self.lh, -hpop(self.rh))
def solution(jobs: list): answer = 0 cnt, last, time = 0, -1, 0 heap = [] jobs.sort(key=lambda x: (x[1], x[0])) while cnt < len(jobs): for a, b in jobs: if last < a <= time: hpush(heap, [b, a]) if heap: a, b = hpop(heap) last = time time += a answer += (time - b) cnt += 1 else: time += 1 return answer // len(jobs)
def main(): import sys from heapq import heappush as hpush, heappop as hpop input = sys.stdin.readline X, Y, Z, K = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) C = list(map(int, input().split())) A.sort(reverse=True) B.sort(reverse=True) C.sort(reverse=True) memo = set([(0, 0, 0)]) h = [(-(A[0] + B[0] + C[0]), (0, 0, 0))] for _ in range(K): val, (i, j, k) = hpop(h) if i + 1 < len(A) and (i + 1, j, k) not in memo: hpush(h, (-(A[i + 1] + B[j] + C[k]), (i + 1, j, k))) memo.add((i + 1, j, k)) if j + 1 < len(B) and (i, j + 1, k) not in memo: hpush(h, (-(A[i] + B[j + 1] + C[k]), (i, j + 1, k))) memo.add((i, j + 1, k)) if k + 1 < len(C) and (i, j, k + 1) not in memo: hpush(h, (-(A[i] + B[j] + C[k + 1]), (i, j, k + 1))) memo.add((i, j, k + 1)) print(-val)
def addNum(self, num): """ :type num: int :rtype: None """ if not self.maxh: hpush(self.maxh, -num) return if len(self.minh) == len(self.maxh): hpush(self.maxh, -num) else: hpush(self.minh, num) """ minheap max heap 1 3 2 """ top_min, top_max = hpop(self.maxh) * -1, hpop(self.minh) hpush(self.maxh, -1 * min(top_min, top_max)) hpush(self.minh, max(top_min, top_max)) return
def constrainedSubsetSum(self, A: List[int], k: int) -> int: n = len(A) q = [] ret = -float("inf") for i in range(n): candi = A[i] while q : a = hpop(q) val, prev_i = -a[0], a[1] if i - prev_i > k : continue if val > 0 : candi += val hpush(q, a) break ret = max(ret, candi) hpush(q, [-candi, i]) return ret
def longestSubarray(self, nums: List[int], limit: int) -> int: n = len(nums) max_que = [] min_que = [] left = right = ret = 0 while right < n : hpush(max_que, [-nums[right], right]) hpush(min_que, [nums[right], right]) while True : max_val, idx = hpop(max_que) max_val *= -1 if idx < left : continue elif max_val - nums[right] > limit : left = idx + 1 continue else : hpush(max_que, [-max_val, idx]) break while True : min_val, idx = hpop(min_que) if idx < left : continue elif nums[right] - min_val > limit : left = idx + 1 continue else : hpush(min_que, [min_val, idx]) break ret = max(ret, right + 1 - left) right += 1 return ret
def rainfall(island, N, M): queue = [] visited = set() collected = 0 for i in ((x, y) for x in (0, N - 1) for y in range(1, M - 1)): hpush(queue, (island[i[0]][i[1]], i)) for i in ((x, y) for x in range(1, N - 1) for y in (0, M - 1)): hpush(queue, (island[i[0]][i[1]], i)) while queue: lh, v = queue[0] while queue: h, v = hpop(queue) if v not in visited: if h > lh: hpush(queue, (h, v)) break visited.add(v) collected += lh - h for dxy in ((0, -1), (-1, 0), (1, 0), (0, 1)): x, y = v[0] + dxy[0], v[1] + dxy[1] if N - 1 > x > 0 and M - 1 > y > 0: hpush(queue, (island[x][y], (x, y))) return collected
def nthUglyNumber(self, x): #d = {1} #either use set or diticonary (200ms vs 196 ms) d = {1: 1} n = [1] hi(n) while True: l = hpop(n) # get lowest value in the set x -= 1 if x == 0: return l l2, l3, l5 = l * 2, l * 3, l * 5 if l2 not in d: #d.add(l2) d[l2] = 1 hpush(n, l2) if l3 not in d: #d.add(l3) d[l3] = 1 hpush(n, l3) if l5 not in d: #d.add(l5) d[l5] = 1 hpush(n, l5)
def write(self, s): t = time.time() hpush(self.heap, (t, s)) if self.stream: self.stream.write(s)
def reachableNodes(self, edges: List[List[int]], M: int, N: int) -> int: '''Create graph''' graph = {} for vetex in range(N): graph[vetex] = {} for vetex1, vetex2, distance in edges: graph[vetex1][vetex2] = distance graph[vetex2][vetex1] = distance '''initialize heap queue''' heap_queue = [] move, src = 0, 0 hpush(heap_queue, (move, src)) '''initialize others''' visited = set() count = 0 while heap_queue: move, vetex = hpop(heap_queue) '''Use all the step, break''' if move > M: break '''This node has been visited, pass''' if vetex in visited: continue visited.add(vetex) count += 1 for neighbor in graph[vetex]: weight = graph[vetex][neighbor] next_move = move + weight + 1 ''' if the neighboring vetex has been visited, we check if the remaining step (M - move) is enough to cover all nodes between vetex and neighbor Later on, we'll see we only save nodes that have not been visited. ''' if neighbor in visited: count = count + min(M - move, graph[vetex][neighbor]) else: if next_move > M: count = count + (M - move) '''Save nodes that are only not been visited.''' graph[neighbor][vetex] -= (M - move) else: count = count + weight ''' Save nodes that are only not been visited. Here, all have been visited, so updated to 0 ''' graph[neighbor][vetex] = 0 '''Update next_move as move''' hpush(heap_queue, (next_move, neighbor)) return count
from heapq import heappush as hpush, heappop as hpop t = int(raw_input()) # read a line with a single integer for i in xrange(1, t + 1): N, K = [int(s) for s in raw_input().split(" ") ] # read a list of integers, 2 in this case h = [] hpush(h, (-N, N)) for _ in xrange(K - 1): # print h _, a = hpop(h) # print a, if a > 1: hpush(h, (-(a / 2), a / 2)) if a > 2: hpush(h, (-((a - 1) / 2), (a - 1) / 2)) _, a = hpop(h) # print a p, q = a / 2, (a - 1) / 2 print "Case #{}: {} {}".format(i, p, q)
def push_fringe(fringe, pos): hpush(fringe, (pos.t, pos))
def __init__(self, n): assert(n > 0) self.processors = [] for i in range(n): hpush(self.processors, (0, i))
T = int(input()) for t in range(1, T + 1): print("Case #%d: " % t, end="") n, k = map(int, input().split()) u = int(float(input()) * base) p = [int(base * float(x)) for x in input().split()] heapify(p) while u > 0: oldu = u small = hpop(p) poc = 1 while len(p) > 0 and p[0] == small: poc += 1 hpop(p) second = base if len(p) == 0 else p[0] rozdiel = second - small kolko = rozdiel if kolko * poc > u: kolko = u // poc co = min(small + kolko, base) u -= (co - small) * poc for i in range(poc): hpush(p, co) if oldu == u: break prod = 1.0 for x in p: prod *= (x / (base + .0)) print("%.6f" % prod)
def push(self, n): hpush(self.hq, -n)
def push_fringe(fringe, pos): hpush(fringe, (pos.num_digs, pos))
def push(self, o): hpush(self.items, o)