Working at the Restaurant
InterviewTime limit1sMemory limit128 MB
Simulate a two-pile plate stacking strategy that always drops on pile 2 and serves from pile 1, moving plates when pile 1 runs out, and output the exact operation transcript.
- Level
Easy3 of 10
- Topics
- Stack, Simulation, Implementation
- Solved
- No attempts yet
Problem
Last night, Tom went on a date with a really nice girl. However, he forgot to bring his credit card and had no cash in his wallet, so he ended up working at the restaurant to pay the bill. His task is to take plates from the waiter when they come from the tables, and pass them along when the dishwasher requests them. It is very important that the plates are washed in the same order in which they are brought from the tables; otherwise a plate might wait too long before being washed and leftover food could get stuck. Holding all the plates in his hands is not a great idea, so Tom puts them on a table as soon as the waiter hands them over, and picks them up again when it is time to pass them to the dishwasher. There is room for only two piles of plates on the table, referred to as pile 1 and pile 2, and Tom has only this one table to use.
Tom won last year's SWERC, so he is certainly capable of optimizing for efficiency. Given the sequence of plates and requests he receives, you must output the transcript of the specific way, described below, in which Tom organizes the plates on the table.
Input
The input has several test cases. Each case begins with a line containing a number (), followed by lines, each of which is either DROP m or TAKE m, where is the number of plates to drop or take. DROP m means the next event is the waiter bringing plates to Tom, so he has to drop them on the table; TAKE m means the next event is Tom taking plates from the table and passing them along in the right order. You may assume that Tom never receives a TAKE m instruction when there are fewer than plates on the table, and that the sum of all values of in DROP operations does not exceed . Note that there may be plates left on Tom's table after the last request, since Tom might be relieved of his duty before the restaurant closes.
The input ends with a line with , which must not be processed.
Output
For each test case, output the transcript of the operations Tom performs. Each line is one of the following:
DROP 1 morDROP 2 m(): take a plate from the waiter and drop it on top of pile 1 (respectively pile 2), repeated times in total.TAKE 1 morTAKE 2 m(): take a plate from the top of pile 1 (respectively pile 2) and pass it to the dishwasher, repeated times in total.MOVE 1->2 morMOVE 2->1 m(): take a plate from the top of pile 1 (respectively pile 2) and drop it on top of the other pile, repeated times in total.
Tom always drops incoming plates on pile 2 and always serves the dishwasher from pile 1, following exactly this deterministic strategy (you must reproduce its transcript):
- For each
DROP mcommand, output one lineDROP 2 m. - For each
TAKE mcommand, processed in order:- If pile 1 currently holds plates, output
TAKE 1 kwith , and decrease the number of plates still needed by . - If plates are still needed afterwards (pile 1 is now empty), output
MOVE 2->1 b, where is the current number of plates on pile 2 — this moves every plate from pile 2 onto pile 1 — and then outputTAKE 1 r, where is the number of plates still needed.
- If pile 1 currently holds plates, output
Handle the commands in the exact order they are given. This strategy washes every plate in the same order it arrived, and uses at most output lines and at most plate movements. Print a blank line between the outputs of different test cases.