Minimizing Haybales
Time limit4sMemory limit512 MB
Given stacks of haybales where adjacent stacks whose heights differ by at most K can swap, output the lexicographically smallest reachable height sequence.
- Level
Medium7 of 10
- Topics
- Topological sort, Segment tree, Heap
- Solved
- No attempts yet
Problem
Bessie is bored and is causing trouble in Farmer John's barn again. FJ has () stacks of haybales. For each , the th stack has () haybales. Bessie does not want any haybales to fall, so the only operation she can perform is the following:
- If two adjacent stacks have heights that differ by at most (), she can swap the two stacks.
What is the lexicographically smallest sequence of heights that Bessie can obtain after some sequence of these operations?
Input
The first line contains and . The -st line contains the height of the -th haybale.
Output
Print lines. The -th line contains the height of the -th haybale in the solution.
Hint
Bessie can swap the stacks as follows:
7 7 3 6 2
-> 7 7 6 3 2
-> 7 7 6 2 3
-> 7 6 7 2 3
-> 6 7 7 2 3