Priority Queue with Custom Comparator
core
Implement vector<pair<int,string>> pop_all(const vector<pair<int,string>>& items) (items is a list of (int, str) tuples in Python, an array of [number, string] pairs in JavaScript) using a priority_queue<pair<int,string>, vector<pair<int,string>>, Cmp> with a custom comparator Cmp. Python's heapq takes no comparator, so the equivalent there is pushing a decorated key tuple (e.g. negating the int field so larger ints pop first, since heapq is min-first) and popping by that key. JavaScript has neither priority_queue nor heapq (no built-in heap at all), so there you hand-roll the heap and put the ordering rule directly in its comparison. Push every item, then pop until empty, recording the order. The pop order ranks by the int field descending (larger int pops first); when two items share the same int, the tie is broken by the string field ascending (lexicographically smaller string pops first). Return the recorded order as a vector the same length as items.
Example: pop_all({{2, "b"}, {2, "a"}, {5, "z"}}) returns {{5, "z"}, {2, "a"}, {2, "b"}}: 5 pops first (largest int); the two 2s tie, so "a" pops before "b".
Input format: each test in tests/*.in is laid out as:
- Line 1:
n, the number of items. - Next
nlines:p w, an integer priority and a word.
Test 02.in reads:
4
3 b
5 a
1 z
5 b
This is four items; two share priority 5, so pop_all has to break that tie by word (a before b).
Where you'll use it:
✦ Solution & editorial unlock with the pass.