This page is still under construction.

Parts of this page are still being built. What you see may change.

Cleaning the Dishes

Time limit1sMemory limit128 MB

Summary
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 NN (1≤N≤10,0001 \le N \le 10{,}000) dirty dishes. Bessie washes the dishes; Canmuu dries them.

Each dish has a unique serial number from 11 to NN. At the start, all dishes form a single unwashed pile, stacked in order with dish 11 on top and dish NN on the bottom.

The two cows take turns following a list of commands. Each command has a type CiC_i (1≤Ci≤21 \le C_i \le 2) and a count DiD_i (1≤Di≤N1 \le D_i \le N):

  • Ci=1C_i = 1 (wash): Bessie takes DiD_i 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.
  • Ci=2C_i = 2 (dry): Canmuu takes DiD_i 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 55 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 11: a single integer NN, the number of dishes to wash and dry.
  • Lines 22 and onward: each line contains a command type CiC_i and a count DiD_i, separated by a space.

Output

  • Lines 11 to NN: line ii contains the serial number of the ii-th dish in the cleaned pile, counted from the top.

Examples1

  1. Example 1

    Input
    5
    1 3
    2 2
    1 2
    2 3
    
    Expected output
    1
    4
    5
    2
    3