Number tag exchange
InterviewTime limit2sMemory limit512 MB
Simulate M bubble passes over the line, swapping neighbors when the front tag has the larger remainder modulo the pass number.
- Level
Easy2 of 10
- Topics
- Simulation, Implementation
- Solved
- No attempts yet
Problem
students stand in one line in a classroom. The -th student from the front holds one number tag, and the number written on it is .
There are cards, numbered 1 to . The cards are used in order starting from card 1, and card () is used after card has been used.
Card is used like this.
- The teacher gives card to student 1.
- Student , who holds the card, passes it to student . If is greater than , the two students exchange their number tags. Here is the number on the tag student holds at that moment.
- Once the last student receives the card, that card is thrown away.
The game ends when card has been thrown away. Write a program that prints the number tags the students hold after the game ends, in order from the front of the line.
Input
The first line has the number of students and the number of cards , separated by a space. (, )
Each of the next lines has the number on the tag that student starts with. ()
Output
Print the number on the tag each student holds after the game ends, one per line, in order from the front of the line.
Hint
Suppose six students hold the tags 3, 2, 8, 3, 1, 5 in that order and there are four cards. The line changes as each card is used.
- after card 1: 3 2 8 3 1 5
- after card 2: 2 8 3 3 1 5
- after card 3: 2 3 3 1 8 5
- after card 4: 2 3 1 8 5 3