Frog Leaps
InterviewTime limit1sMemory limit128 MB
Simulate a circular arrangement where a proclaimed frog repeatedly swaps forward with its neighbor B times, then output the final order starting from frog 1.
- Level
Easy3 of 10
- Topics
- Simulation, Array
- Solved
- No attempts yet
Problem
The Frog Regent has arranged his frog servants in a circle, each frog facing the back of the frog directly ahead of it. Every frog carries a unique integer ID from to .
The arrangement is written as a sequence of IDs that always begins with the frog whose ID is , followed by the frog directly ahead of it, then the next frog ahead, and so on all the way around the circle, ending with the frog directly behind frog .
A frog performs one leap by jumping over the single frog directly ahead of it, swapping places with that frog. Because the frogs stand in a circle, the frog directly ahead of the last one in the written sequence is frog again. When the Regent proclaims a number , the frog whose ID is performs exactly leaps, one after another.
For example, from the arrangement 1 5 4 3 2 6, proclaiming makes frog perform leaps and the arrangement becomes 1 2 5 4 3 6. The arrangement is always rewritten so that it starts with frog .
The Regent will issue a list of proclamations, one after another. Given the starting arrangement and the whole list of proclamations, determine the final arrangement of the frogs once every proclamation has been carried out in order.
Input
The first line contains an integer , the number of frogs ().
The second line contains a permutation of the integers through : the starting arrangement, beginning with frog .
The third line contains an integer , the number of proclamations ().
The fourth line contains integers (), the proclamations in the order the Regent issues them. If , this line is empty.
Output
Output one line with the final arrangement after all proclamations have been applied: integers separated by single spaces, written so that the sequence begins with frog .