#C13759. Merge K Sorted Lists
Merge K Sorted Lists
Merge K Sorted Lists
You are given ( k ) sorted lists of integers. Your task is to merge these ( k ) lists into one sorted list while maintaining the order. The input is provided via standard input (stdin) and the output must be printed via standard output (stdout).
The merging process should use an efficient algorithm (such as using a min-heap) to combine the lists. If any of the lists is empty, it should be skipped.
Input Format: The first line contains an integer ( k ) representing the number of lists. For each list, the first integer indicates the number ( n_i ) of elements in that list, followed by ( n_i ) space-separated integers which are sorted in non-decreasing order.
Output Format: Print a single line containing the merged sorted list, with numbers separated by a single space.
For example, if you merge ([1, 4, 7]), ([2, 5, 8]), and ([3, 6, 9]), the output should be:
(1\ 2\ 3\ 4\ 5\ 6\ 7\ 8\ 9).
inputFormat
Input:
- The first line contains an integer \( k \) (the number of lists).
- For each list, the first number is an integer \( n_i \) indicating the number of elements in the list, followed by \( n_i \) space-separated integers in sorted order.
outputFormat
Output:
- Print a single line with the merged list, where the integers are in non-decreasing order separated by a single space.
3
3
1 4 7
3
2 5 8
3
3 6 9
1 2 3 4 5 6 7 8 9
</p>