Two relay towers each keep a log of the beacon transmissions they've picked up. Every entry in a log lists a frequency channel and the power level broadcast on it, and within a single tower's own log no frequency channel is ever logged twice. The network operations team wants to consolidate both towers' logs into one master log: whenever a frequency channel shows up in both logs, the consolidated entry for that channel should carry the sum of the two towers' power levels for it; a channel that appears in only one log keeps that log's power level unchanged. Produce the consolidated log sorted by frequency channel in increasing order.
The first line contains an integer n1, the number of entries in the first tower's log. Each of the next n1 lines contains two integers freq and power describing one logged transmission. The next line contains an integer n2, the number of entries in the second tower's log, followed by n2 lines in the same two-integer format.
Print an integer k, the number of distinct frequency channels appearing across both logs. Then print k lines, each containing two integers: the frequency channel and its consolidated power level, ordered by frequency channel in strictly increasing order.
Example 1
Input
3 1 1 4 5 3 8 2 3 1 1 5
Expected
3 1 6 3 9 4 5
Explanation
Tower one logs channel 1 at power 1, channel 4 at power 5, and channel 3 at power 8. Tower two logs channel 3 at power 1 and channel 1 at power 5. Channel 1 appears in both logs, so its consolidated power is 1 + 5 = 6. Channel 3 also appears in both, giving 8 + 1 = 9. Channel 4 appears only in tower one and keeps its power of 5. Sorted by channel, the output is 1 6, 3 9, 4 5.
Example 2
Input
3 1 1 3 2 2 3 3 2 1 3 2 1 3
Expected
3 1 4 2 4 3 4
Explanation
Tower one logs channels 1, 3, 2 with powers 1, 2, 3. Tower two logs channels 2, 3, 1 with powers 1, 2, 3. Every channel appears in both logs: channel 1 gets 1 + 3 = 4, channel 2 gets 3 + 1 = 4, channel 3 gets 2 + 2 = 4. Sorted by channel, the output is 1 4, 2 4, 3 4.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →