Problem 896
Build an Array With Stack Operations
Medium · 20 pts · Array, Stack, Simulation
Problem
You read the numbers 1, 2, 3, ... one at a time and may use two stack operations: Push (push the number just read) and Pop (remove the top). Build the strictly increasing array TARGET from bottom to top, stopping as soon as the stack equals TARGET, and never reading past N. Print the operations. For each number up to the last target value, Push it, and Pop it right away if it is not in TARGET.
Input
The first line contains M and N. The second line contains the M strictly increasing target values.
Output
Print the operations separated by spaces.
Constraints
1 <= M <= 100; 1 <= N <= 100; 1 <= TARGET[i] <= N; TARGET is strictly increasing
Example input
2 3 1 3
Example output
Push Push Pop Push
Explanation
For TARGET = 1 3 with N = 3: Push 1, then Push 2 and Pop it (2 is not wanted), then Push 3. The stack is 1 3, so the answer is Push Push Pop Push.