← 返回 optiver 的题目列表Market Updates Stream Decoding
类型:online_judge
An exchange publishes a stream of market updates to all participants. For redundancy, the updates are sent on two physically separate channels, called A and B. Each update is sent as a separate data packet with a sequence number and a payload. The data packets sent on A and B are identical, but they can be delayed, duplicated, reordered, or dropped. To successfully decode the updates, we need all packets in order, without any gaps.
Your task is to receive the packets from both channels and combine them into a single stream of ordered and deduplicated updates.
Each packet contains a sequence number and a payload. You need to output the packets ordered by the sequence number without any gaps. When a packet is missing, you should buffer the following packets until you receive the missing one. The first expected sequence number is always 1.
You can use a buffer with enough capacity to hold N packets. Due to memory pressure, the buffer size N will change dynamically. If the buffer is not big enough to fit incoming packets, you should immediately output an error message. When such an error occurs, you should not output any more lines.
Example
Input
5
2,data2
1,data1
3,data3
4,data4
6,data6