Cleaning the Dishes
Time limit1sMemory limit128 MB
Simulate two stacks where each wash or dry command reverses the order of the dishes it moves, then print the final cleaned pile top to bottom.
- Level
Medium5 of 10
- Topics
- Simulation, Stack, Implementation, Linked list
- Solved
- No attempts yet
Problem
Bessie and Canmuu are teaming up to wash a massive pile of () dirty dishes. Bessie washes the dishes; Canmuu dries them.
Each dish has a unique serial number from to . At the start, all dishes form a single unwashed pile, stacked in order with dish on top and dish on the bottom.
The two cows take turns following a list of commands. Each command has a type () and a count ():
- (wash): Bessie takes dishes one at a time from the top of the unwashed pile, washes each, and places it on top of the washed-but-not-dried pile. Because she moves them one by one, their order is reversed.
- (dry): Canmuu takes dishes one at a time from the top of the washed-but-not-dried pile, dries each, and places it on top of the cleaned pile. Again the order is reversed.
Every command always has enough dishes available to process, and after the last command every dish has been washed and dried. Report the final order of the cleaned pile, from top to bottom.
For example, suppose there are dishes. The unwashed pile starts like this:
1 <- top
2
3
4
5 <- bottom
After the commands "wash 3, dry 2, wash 2, dry 3", the cleaned pile ends up in this top-to-bottom order:
1 <- top
4
5
2
3 <- bottom
Input
- Line : a single integer , the number of dishes to wash and dry.
- Lines and onward: each line contains a command type and a count , separated by a space.
Output
- Lines to : line contains the serial number of the -th dish in the cleaned pile, counted from the top.