#K14066. Inventory Operations Processing
Inventory Operations Processing
Inventory Operations Processing
You are given a sequence of inventory operations applied to a warehouse. Each operation is represented as one of the following commands:
- RECEIVE q P: Increase the quantity of product P by q.
- SHIP q P: Decrease the quantity of product P by q.
- RESET: Clear the entire inventory (i.e., set the quantities of all products to 0 by removing them).
The operations are processed sequentially. After processing all operations, output the final quantities for each product sorted in lexicographical order (i.e. sorted by product name). If the inventory is empty, output nothing.
For example, consider the following sequence of operations:
RECEIVE 3 A RECEIVE 5 B SHIP 2 A RESET RECEIVE 8 A
After processing them, the final inventory is just product A with quantity 8.
The mathematical implementation can be expressed as follows:
\(\texttt{inventory} \xleftarrow{} \{\}\)
For each operation, update \(\texttt{inventory}\) accordingly; and when encountering a RESET, clear the inventory.
inputFormat
The first line of input contains a single integer \(n\) (\(1 \le n \le 1000\)), the number of operations.
Each of the next \(n\) lines contains an operation in one of the following formats:
RECEIVE q P
SHIP q P
RESET
Here, q
is an integer quantity and P
is a string representing the product type.
outputFormat
For each product in the final inventory, output a single line containing the product name and its final quantity separated by a space. The products should be listed in lexicographical order. If the inventory is empty, do not output anything.
## sample5
RECEIVE 3 A
RECEIVE 5 B
SHIP 2 A
RESET
RECEIVE 8 A
A 8
</p>