Flipping a Pack of Cards
Time limit1sMemory limit128 MB
Simulate a deck of n cards under m prefix-reversal-and-flip operations, then report the final position and face direction of s queried cards.
- Level
Medium6 of 10
- Topics
- Simulation, Implementation, Math, Array
- Solved
- No attempts yet
Problem
A pack of cards lies on the table, stacked one on top of another. Each card has a positive integer written on exactly one side, and the other side is blank. The top card shows , the next one shows , and so on down to the bottom card, which shows . Initially every card lies with its number facing up.
Archibald performs turns. On the -th turn he lifts the top cards, flips this whole group upside down as a single block (their order is reversed and every card is turned over), and puts the group back on top of the pack.
After all turns are finished, determine the final position and orientation of several chosen cards.
Input
The first line contains two integers and , separated by a space (, ): the number of cards and the number of turns.
Each of the next lines contains one integer (): the number of top cards flipped on that turn.
The next line contains one integer (): the number of queries.
Each of the next lines contains one integer: the number written on a card whose final position and orientation must be reported.
Output
Print exactly lines. For the -th query, let be the final position of that card counted from the top (the top card is position ). Print if the card's number ends up facing up, or if it ends up facing down.