PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000137. Sắp xếp các đống cỏ khô (Minimizing Haybales)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Sắp xếp các đống cỏ khô (Minimizing Haybales)

Minimizing Haybales

Source: USACO

Version: Phuoc Hung OJ Extended

Problem Statement

Farmer John has NN stacks of haybales arranged in a row. The ii-th stack has height hih_i haybales. Bessie may rearrange the stacks by using the following operation:

Choose two adjacent stacks with heights x,yx,y. If ∣x−y∣≤K|x-y|\le K, the stacks may be swapped; otherwise that swap is forbidden.

She may perform any number of valid swaps, including zero. Among all reachable sequences of stack heights, find the lexicographically smallest one: the sequence with the smaller value at the first position where two sequences differ.

Stacks of equal height are still separate occurrences in the initial sequence; the output contains only their heights.

Input

  • The first line contains N,KN,K.
  • Each of the next NN lines contains one height hih_i, in the original left-to-right order.

Output

Print exactly NN lines. Line ii contains the height at position ii in the lexicographically smallest reachable sequence.

Subtasks

  • Subtask 1 (10 points): 1≤N≤1001\le N\le100; 1≤K≤1091\le K\le10^9; 1≤hi≤1091\le h_i\le10^9 for every 1≤i≤N1\le i\le N.
  • Subtask 2 (20 points): 1≤N≤5 0001\le N\le5\,000; 1≤K≤1091\le K\le10^9; 1≤hi≤1091\le h_i\le10^9 for every 1≤i≤N1\le i\le N.
  • Subtask 3 (70 points): 1≤N≤100 0001\le N\le100\,000; 1≤K≤1091\le K\le10^9; 1≤hi≤1091\le h_i\le10^9 for every 1≤i≤N1\le i\le N.

Examples

Example 1

Input

5 3
7
7
3
6
2

Output

6
7
7
2
3

Explanation

Initially the stacks are 7 7 3 6 2, with K=3K=3. A valid sequence is 7 7 3 6 2 →\to 7 7 6 3 2 →\to 7 7 6 2 3 →\to 7 6 7 2 3 →\to 6 7 7 2 3. Height 22 cannot cross either height-77 stack because their difference is 5>K5>K. Height 33 cannot cross a preceding height-77 stack either. Therefore 66 is the smallest possible first value, and the remaining choices yield the displayed lexicographic minimum.

Example 2

Input

4 1
5
1
3
2

Output

5
1
2
3

Explanation

Stacks 33 and 22 differ by one, so they may be swapped to obtain 5 1 2 3. Height 55 cannot cross height 11 because they differ by four, and height 11 cannot cross height 33 because they differ by two. Thus the first two heights remain 5,15,1, while 2 3 is the best order for the last two.