887. The kth Smallest Across Many Sorted Lists
L lists are given, each already sorted in non-decreasing order. Print the
kth smallest value across all of them taken together, counting duplicates
separately and counting from 1. A list may be empty.
lists [1 4 5] [1 3 4] [2 6], k = 4 -> 3
lists [] [5 6 7 8], k = 3 -> 7
lists [1 1] [1 1] [1 1], k = 5 -> 1
The merged order of the first row is 1 1 2 3 4 4 5 6, whose 4th entry is 3.
## What most people try first
Concatenating everything and sorting it. That is O(N log N) over the N
values in total, and it throws away the fact that each list was already
ordered. It also builds the whole merged sequence when only one position of
it was wanted.
With N = 200000 the sort is about 3.5 * 10^6 comparisons, which is
survivable. What it is NOT is scalable: the same approach on lists that are
too large to hold, or that arrive over a network, has nowhere to go.
## The k-way merge, stopped early
Hold one candidate from each list: the smallest value that list has not given
up yet. The global smallest remaining value is always one of those L
candidates, because each list is sorted, so nothing behind a candidate can
beat it.
h = min-heap of (value, which list, position in it)
for each non-empty list: push its first value
repeat k times:
(v, i, j) = pop
if list i has a value at j+1: push it
answer = the last v popped
The heap never holds more than L entries, so the cost is O(L + k log L)
and it stops the moment it has counted k values, no matter how much data
sits behind them.
## Why the heap entry must carry the list it came from
After taking a value you have to advance THAT list, and a heap of bare values cannot tell you which one that was. Two different lists can hold the same value, so even searching for it afterwards is ambiguous. Carrying the index, and the position within the list, is what makes the advance well defined. This is the same principle as the derived key in problem 881: put in the entry whatever you will need when it comes back out.
## Why the answer is the kth thing popped
The pops come out in non-decreasing order. To see it, note the invariant: the
heap always holds, for every list, its smallest value not yet popped. So the
minimum of the heap is the minimum of everything not yet popped, and it can
never be smaller than something already popped. Counting pops therefore
counts merged positions, and the kth pop is the kth smallest.
## The boundary that breaks a careless implementation
An empty list. It contributes no candidate at all, and reading its first value is an out-of-bounds access. Skip it at seeding time rather than trying to handle it inside the loop.
Duplicates across lists. Every copy is a separate merged position, so
[1 1] [1 1] [1 1] with k = 5 answers 1 rather than running out of values.
A program that keeps a set of values seen, to avoid "repeats", gets this
wrong.
## What is NOT an overflow here
Values are at most 10^9 in magnitude and nothing is summed. Both k and
the total number of values fit in 32 bits, since 200000 is small. There is
no overflow in this problem.
## Related problems
Problem 866 does the same merge but prints EVERY value, so it cannot stop
early and its cost is O(N log L) no matter what. Problem 628 is the same
question for exactly two lists, where two pointers do the job with no heap at
all. Problem 895 keeps the same merge running and watches the spread between
the smallest and largest candidates.
## Cost
Time O(L + k log L). Space O(L) on top of the input.
Constraints - `1 ≤ L ≤ 1000` - `0 ≤ m ≤ 200000` - The total number of values is at most 200000 and at least 1. - `1 ≤ k ≤ the total number of values` - `-1000000000 ≤ value ≤ 1000000000`
Input
The first line contains two integers L and k.
Each of the next L lines describes one list: an integer m, then m space-separated integers in non-decreasing order.
Output
Print one integer alone on a line: the kth smallest value across all the lists, counting duplicates separately and counting from 1. Print no other words.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.