A festival's raffle booth keeps its ticket stubs in a numbered array. Between draws, the booth operator issues one of two commands: reset the stubs to their original printed order, or run the shuffling machine once. The machine performs the classic back-to-front swap procedure: for i going from the last index down to index 1, it swaps whichever stub currently sits at position i with whichever stub currently sits at some position r (0 <= r <= i). Because true randomness can't be replayed for automated grading, every shuffle command in this problem supplies the exact sequence of r values the machine must use, one for each step from i = n-1 down to i = 1, in that order. After every command - whether a reset or a shuffle - print the resulting array of stub numbers on its own line. A reset always restores the array to its very first state (before any command ran), no matter how many shuffles happened in between.
Line 1: an integer n - the number of tickets. Line 2: n integers - the initial ticket numbers, in order. Line 3: an integer q - the number of commands. Each of the next q lines holds one command, either:
q lines: after each command, the resulting array on its own line, values separated by single spaces.
Example 1
Input
3 1 2 3 2 SHUFFLE 0 0 RESET
Expected
2 3 1 1 2 3
Explanation
Start with [1,2,3]. Step i=2 uses r_1=0: swap positions 2 and 0, giving [3,2,1]. Step i=1 uses r_2=0: swap positions 1 and 0, giving [2,3,1] - this is printed for the SHUFFLE command. The RESET command then restores the very first state [1,2,3], which is printed next.
Example 2
Input
1 5 1 SHUFFLE
Expected
5
Explanation
With only one ticket, there is no index below the last one to swap with (the loop from i=n-1 down to 1 never runs), so the SHUFFLE line carries no r values and the array stays [5], which is printed unchanged.
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 →