#K92897. Warehouse Stock Management
Warehouse Stock Management
Warehouse Stock Management
This problem involves managing stock levels in a warehouse by processing various commands. You are given a series of commands to update or query the stock level of products. The commands are as follows:
ADD product_id quantity
: Add a specified quantity of the product to the warehouse.REMOVE product_id quantity
: Remove a specified quantity from the warehouse, ensuring that the stock does not fall below zero.TRANSFER product_id quantity location
: Transfer a specified quantity of the product to a different location. This command acts like a removal, reducing the stock.QUERY product_id
: Output the current stock level of the specified product.
For a given sequence of commands, process each command in order. For every QUERY
command, output the stock level on a new line.
Note: All quantities and product IDs are positive integers. The warehouse initially has a stock of 0 for every product.
In mathematical terms, if we denote the stock of a product i after a command as S(i), then for an addition command:
\[ S(i) = S(i) + q \]For a removal or transfer command:
\[ S(i) = \max(0, S(i) - q) \]</p>inputFormat
The input is given via standard input (stdin) and consists of:
- An integer n on the first line, representing the number of commands.
- Followed by n lines each containing one command.
Each command will be in one of the following forms: ADD product_id quantity
, REMOVE product_id quantity
, TRANSFER product_id quantity location
, or QUERY product_id
.
outputFormat
For each QUERY
command, output the current stock level of the specified product on a new line.
4
ADD 1 100
ADD 2 150
QUERY 1
QUERY 2
100
150
</p>