#C4096. Priority Messaging System
Priority Messaging System
Priority Messaging System
You are tasked with implementing a messaging system that processes two types of operations:
- SEND: Send a message with an associated integer priority.
- RECEIVE: Retrieve and remove the message with the highest priority.
If two messages share the same priority, the message that was sent earlier should be retrieved first. In case a RECEIVE operation is executed when there are no messages, output NO_MESSAGES
.
The input begins with an integer n indicating the number of operations. The following n lines each contain an operation in one of the two forms:
SEND x message
wherex
is an integer representing the priority andmessage
is the content.RECEIVE
Process each operation in the order given and for each RECEIVE
command, output the retrieved message on a new line.
inputFormat
The input is read from standard input (stdin
) and consists of:
- An integer
n
on the first line, representing the number of operations. n
subsequent lines, each being either aSEND
operation of the formSEND x message
or aRECEIVE
operation.
outputFormat
For each RECEIVE
operation, output the result on a separate line to standard output (stdout
). If there are no messages to receive, output NO_MESSAGES
.
5
SEND 1 Hello
SEND 3 World
RECEIVE
SEND 2 !
RECEIVE
World
!
</p>