#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 NN (1N1051 \le N \le 10^5) haybale stacks. For each i[1,N]i \in [1,N], the ii-th stack has height hih_i (1hi1091 \le h_i \le 10^9). 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 KK (1K1091 \le K \le 10^9), 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 NN and KK. Line i+1i+1 contains the height of the ii-th haybale stack.

Output Format

Output NN lines. Line ii contains the height of the ii-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 10%10\% of all testdata, N100N \le 100.
  • For another 20%20\% of all testdata, N5000N \le 5000.
  • For the remaining 70%70\% of the testdata, there are no additional constraints.

Problem by: Daniel Zhang, Benjamin Qi.

Translated by ChatGPT 5