#K45967. Merge Sorted Data Streams
Merge Sorted Data Streams
Merge Sorted Data Streams
You are given k streams of data points. Each stream contains data points which are represented as pairs of integers (timestamp, value) and are sorted in ascending order by timestamp. Your task is to merge these streams to produce a single list of data points that is sorted by timestamp.
In formal terms, if the total number of data points across streams is N and the number of streams is k, an efficient algorithm should run in O($N \log k$) time. Your solution should read input from stdin
and write the merged list to stdout
.
Input Format: The first line contains an integer k denoting the number of streams. For each stream, the first line contains an integer n denoting the number of data points in that stream, followed by n lines, each containing two space-separated integers: timestamp and value.
Output Format: Print the merged list of data points. Each line of the output should contain two space-separated integers representing the timestamp and its corresponding value.
inputFormat
The input is read from standard input (stdin
) with the following format:
k timestamp1 value1 ... timestampn1 valuen1 ...
Here, k (1 ≤ k ≤ 105) is the number of streams. For each stream, the first number n indicates the number of data points in that stream. This is followed by n lines, each with two integers representing a data point's timestamp and value.
outputFormat
The output should be written to standard output (stdout
) with each line containing two space-separated integers. These integers represent a data point's timestamp and value in the merged order sorted by timestamp.
2
3
1 100
2 200
5 500
2
3 300
4 400
1 100
2 200
3 300
4 400
5 500
</p>