Bubble Sort
Time limit1sMemory limit64 MB
Compute the array after K left-to-right bubble sort passes over N numbers without simulating every swap.
- Level
Medium6 of 10
- Topics
- Segment tree, Sorting, Simulation
- Solved
- No attempts yet
Problem
Bubble sort orders a sequence of length by comparing two adjacent values and swapping them. Call one execution of the procedure below a round.
- Compare the value at position 1 with the value at position 2, and swap them if the earlier one is larger.
- Compare the value at position 2 with the value at position 3, and swap them if the earlier one is larger.
- Continue the same way to the right end.
- Compare the value at position with the value at position , and swap them if the earlier one is larger.
Repeating a round times sorts the sequence in ascending order. Sechan already knows the sorted result, so he wants to look at an intermediate state instead. is very large, so imitating rounds directly takes too long. Help Sechan and write a program that computes the state of the sequence after rounds.
Input
The first line contains and , separated by a space.
The second line contains the integers of the initial sequence in order, separated by spaces.
- Each term of the sequence is an integer between and .
Output
Print the state of the sequence after rounds on one line. Separate the numbers with a single space.