#P8099. [USACO22JAN] Minimizing Haybales P
[USACO22JAN] Minimizing Haybales P
Problem Description
Bessie is bored again, so she is causing trouble in Farmer John's barn. FJ has () haybale stacks. For each , the -th stack has height (). Bessie does not want any hay to fall over, so the only operation she can do is:
- If the height difference between two adjacent stacks is at most (), she can swap these two stacks.
After a sequence of such operations, what is the lexicographically smallest height sequence Bessie can obtain?
Input Format
The first line contains and . Line contains the height of the -th haybale stack.
Output Format
Output lines. Line contains the height of the -th haybale stack in the answer.
5 3
7
7
3
6
2
6
7
7
2
3
Hint
Sample Explanation.
One possible way for Bessie to swap the haybales is:
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
Constraints.
- For of all testdata, .
- For another of all testdata, .
- For the remaining of the testdata, there are no additional constraints.
Problem by: Daniel Zhang, Benjamin Qi.
Translated by ChatGPT 5