#K51327. Simulate Shelves
Simulate Shelves
Simulate Shelves
You are given a collection of shelves and a list of instructions that either place a box on a shelf or remove a box from a shelf. Each shelf can hold at most one box at a time. The instructions are provided in the following format:
- P x y: Place a box with number y on shelf number x if the shelf is currently empty.
- R x: Remove the box from shelf number x.
Note that the shelf numbers are 1-indexed and if an instruction refers to an invalid shelf (i.e. outside the range of available shelves) it should be ignored. After processing all instructions, you should output the final state of the shelves.
Hint: Use arrays to simulate the shelves. Initialize the array with zeros indicating an empty shelf, and update it based on each instruction. When printing the final state, output the box numbers (or 0 for empty) separated by a space.
inputFormat
The first line of input contains two integers n and m separated by a space, where n is the number of instructions and m is the number of shelves. This is followed by n lines each containing an instruction in one of the following forms:
P x y
: Place the box with number y on shelf x if the shelf is empty.R x
: Remove the box from shelf x.
outputFormat
Print a single line with m integers separated by a single space. Each integer represents the final state of a shelf (a box number if present, or 0 if empty) in increasing order of shelf numbers.
## sample7 3
P 1 5
P 2 3
P 3 6
P 1 4
R 2
P 2 7
R 3
5 7 0