A tape archive keeps its reels on a supply rack, numbered 1 through n, and a robotic arm feeds them one at a time in increasing numeric order onto a single takeup spool that behaves exactly like a stack. The arm has two moves: MOUNT takes the next reel from the supply rack and places it on top of the spool, and EJECT removes whatever reel currently sits on top of the spool and discards it for good. The archivist has a manifest listing exactly which reels the spool should hold, read from bottom to top, once the arm is done. Reconstruct the shortest operation log the arm performs to reach that manifest, and stop issuing operations the instant the spool matches the manifest (reels that are never needed are never even mounted).
Line 1: two integers n and m — the number of reels available on the supply rack and the length of the manifest. Line 2: m integers, the manifest, given in strictly increasing order, each between 1 and n.
Print a single line: the operation log as the words MOUNT and EJECT, separated by single spaces, in the exact chronological order the arm performs them. Do not print any operations for reels beyond the point where the spool already matches the manifest.
1 <= n <= 1000 1 <= m <= n The manifest is strictly increasing and every value lies in [1, n].
Example 1
Input
5 3 2 4 5
Expected
MOUNT EJECT MOUNT MOUNT EJECT MOUNT MOUNT
Explanation
Reel 1 is mounted then immediately ejected since the manifest's next value is 2, not 1. Reel 2 is mounted and kept (matches manifest[0]=2). Reel 3 is mounted then ejected (next needed value is 4). Reel 4 is mounted and kept. Reel 5 is mounted and kept, completing the manifest, so the log stops: MOUNT EJECT MOUNT MOUNT EJECT MOUNT MOUNT.
Example 2
Input
3 3 1 2 3
Expected
MOUNT MOUNT MOUNT
Explanation
Every reel from 1 to 3 is exactly the next value the manifest needs, so each is mounted and never ejected: MOUNT MOUNT MOUNT.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →