#P8996. Tin's Magical Shuffle
Tin's Magical Shuffle
Tin's Magical Shuffle
Tin is a famous magician whose classic trick involves shuffling a deck of cards. He prepares a deck of \( n \) cards (with \( n \) even) numbered from \(1\) to \(n\). Initially, the cards are face down in a random order. Before performing the trick, Tin memorizes the initial order. Then he shuffles the deck using the following method:
- Pick the top \( \frac{n}{2} \) cards (in order from top to bottom) and hold them in his right hand, and pick the bottom \( \frac{n}{2} \) cards (in order from bottom to top) and hold them in his left hand. All cards are held face up.
- Using his memory, he repeatedly compares the bottom card of each hand and places down the card with the smaller number. This is repeated until one hand becomes empty.
- Finally, he places down all the remaining cards from the non-empty hand.
The cards are placed on the table in the order they are laid down; the first card placed becomes the bottom of the final pile. At any time during the shuffling, a spectator may ask, "What is the \( t^{\text{th}} \) card from the bottom?" Tin, with his remarkable memory, will answer correctly. Your task is to simulate this magic trick.
Note: All formulas are represented in \( \LaTeX \) format.
inputFormat
The input is given as follows:
- The first line contains an even integer \( n \) (the number of cards).
- The second line contains \( n \) integers representing the initial order of the deck from top to bottom.
- The third line contains an integer \( q \), the number of queries.
- The following \( q \) lines each contain a single integer \( t \) indicating the position from the bottom (1-indexed) of the final deck.
outputFormat
For each query, output the number on the card at the \( t^{\text{th}} \) position from the bottom in the final shuffled deck. Each answer should be printed on a new line.
sample
6
3 1 5 6 2 4
3
1
3
6
5
3
4
</p>