#C474. Simulate Fixed-Height Stack Operations
Simulate Fixed-Height Stack Operations
Simulate Fixed-Height Stack Operations
You are given a stack with a fixed maximum height H. The stack can hold packages with a given height. You will receive a series of operations, each of which is either a Push
or a Pop
command. For a Push X
operation, if the current stack height plus X does not exceed H (i.e. $$current\_height + X \leq H$$), the operation is executed and the package is added (increasing the current height by X). For a Pop -1
operation, if the stack is not empty (i.e. current_height > 0), the operation is executed and instead of reducing the height, the logic subtracts a negative value (thus effectively adding 1 to the current height). Each successful operation increases a counter. Your task is to simulate these operations and output the total number of successful operations.
Note: Although the Pop
command might seem to remove a package, according to the simulation logic the command simply checks if the stack is non-empty and then increases the height by 1 (due to the subtraction of -1). Ensure you carefully follow the condition checks described above.
inputFormat
The first line contains an integer H representing the maximum height of the stack. The second line contains an integer N representing the number of operations. Each of the following N lines contains a command in one of the following formats:
Push X
— whereX
is an integer representing the package height to push.Pop -1
— where the value -1 is a placeholder; the operation is valid if the current stack height is greater than 0.
outputFormat
Output a single integer representing the total number of successful operations performed.
## sample10
5
Push 3
Push 4
Pop -1
Push 5
Pop -1
4