Shuffle Machine
InterviewTime limit1.5sMemory limit1024 MB
For each of the K shuffles, apply technique X_i a total of Y_i times in order, starting from the sorted deck, and print the final card order.
- Level
Medium4 of 10
- Topics
- Array, Divide and conquer
- Solved
- No attempts yet
Problem
Taejun bought a card shuffling machine to play card games with his friends. Wanting to win every game, Taejun analyzes the machine's structure to predict the shuffle results.
The machine has built-in shuffle techniques. The -th shuffle technique is represented by a sequence of length in which each number from to appears exactly once. When the -th shuffle technique is run once, the card at position from the top moves to position from the top.
When the machine is started, it performs shuffles in the given order and then stops. Each shuffle is given by two integers and , which means that shuffle technique is run times.
Initially, the cards are sorted from to from top to bottom. After the machine is started, find the final arrangement of the cards.
Input
The first line contains , , and , separated by spaces.
In each of the next lines, the -th line contains in order, separated by spaces, which represent the -th shuffle technique.
In each of the next lines, the -th line contains the number of the shuffle technique to run in the -th shuffle and the number of times to run it, separated by a space.
Output
Print the final order of the cards from top to bottom after the machine is started.
Constraints
- All given numbers are integers.
- Each of contains each number from to exactly once. ()
- ()
- ()