895. The Smallest Range Covering Every List
L non-empty lists are given, each sorted in non-decreasing order. Find the
smallest range [lo, hi] that contains at least one value from EVERY list.
Among ranges of equal width, take the one with the smaller lo.
Print lo and hi on one line.
[4 10 15 24 26] [0 9 12 20] [5 18 22 30 40] -> 20 24
[1 2 3] -> 1 1
[1 2 3] [4 5 6] -> 3 4
[1 3 5 7 9] [2 4 6 8 10] -> 1 2
Check the first one. The range [20, 24] has width 4 and contains 24 from the
first list, 20 from the second and 22 from the third. Nothing narrower works:
[20, 23] misses the first list, [21, 24] misses the second.
With a single list the answer is a single value repeated, since one value covers everything.
## What most people try first
Trying every pair of endpoints. There are up to 200000 values in total,
so that is 2 * 10^10 candidate ranges before any of them is checked.
Taking the largest and smallest of the whole input. That covers everything and is almost never smallest.
Fixing `lo` on each list's values and binary-searching `hi` in every other
list. Correct, and O(N L log N), which with L = 1000 is far too slow.
## The candidate argument
Keep one pointer into each list, and consider the L values they point at.
Let lo be the smallest of them and hi the largest. That range covers
every list by construction.
The claim is that the OPTIMAL range is one of the ranges you see if you start every pointer at 0 and then, repeatedly, advance the pointer that is at the minimum.
Why: any covering range [p, q] must contain, for each list, a value in that
list. Consider the smallest such value per list; call the largest of those
hi' and the smallest lo'. Then [lo', hi'] is inside [p, q], and it
is exactly one of the configurations the walk visits, because the walk enumerates
every configuration in which each pointer sits at the first value at or above a
common lower bound. Advancing the minimum pointer is what moves that lower
bound up by the smallest possible step, so nothing is skipped.
## The method
h = min-heap of (value, list index, position)
hi = the largest first value over all lists
seed h with every list's first value
best = nothing
loop:
(lo, i, j) = top of h
if [lo, hi] beats best: best = [lo, hi]
if list i has no value at j+1: stop
replace the top with (list[i][j+1], i, j+1)
hi = max(hi, list[i][j+1])
print best
The heap gives lo in O(log L); hi needs no structure at all, because
advancing a pointer only ever introduces a LARGER value, so the running
maximum never has to decrease.
## Why the loop stops at an exhausted list
When any list runs out, no further range can cover it, so no later configuration is valid. Continuing past that point is the single most common bug here, and it silently reports a range that is missing one list entirely.
## The tie rule, and why it is stated
Two different ranges can have the same width, as in the third example where
[3, 4] and several others are all width 1 in other inputs. The rule fixes
the smaller lo, so the comparison is on the pair (hi - lo, lo), and the
update must be strict so that the FIRST range of a given width and lo wins.
## What is NOT an overflow here
Values are at most 10^9 in magnitude, so a width reaches 2 * 10^9. The
signed 32-bit ceiling is 2147483647, and 2000000000 is smaller, so even
the widest possible range fits. Nothing is summed. There is no overflow here.
## Related problems
Problem 887 runs the same k-way merge and counts pops. Problem 866 runs it and prints everything. This one runs it and watches the SPREAD between the smallest and largest candidates, which is a third thing to do with the same loop.
## Cost
Time O(N log L) where N is the total number of values. Space O(L).
Constraints - `1 ≤ L ≤ 1000` - `1 ≤ m` - The total number of values is at most 200000. - `-1000000000 ≤ value ≤ 1000000000`
Input
The first line contains one integer L.
Each of the next L lines describes one list: an integer m, then m space-separated integers in non-decreasing order.
Output
Print two integers on one line, separated by a single space: the smallest lo and hi of a range that contains at least one value from every list, choosing the smaller lo among ranges of equal width. Print no other words.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.