Each arriving bear takes the smallest empty chair at or above its wanted number that is at least d away from every seated bear.
Medium4ImplementationGreedySortingInterviewNo attempts yetTime limit2sMemory limit512 MBBears normally do not sit on a chair to eat. Lately, though, some bears eat while seated.
Kangho is a waiter at a huge restaurant for bears. The restaurant has infinitely many chairs, placed in a single row. The chairs are numbered 1, 2, ... starting from the one closest to the entrance.
Bears are very large, so they cannot sit right next to each other. Whenever two bears are seated, the difference between the numbers of their chairs must be at least d. For example, if d=10, two bears may sit on chairs 47 and 57, but they may not sit on chairs 47 and 56.
The restaurant has just opened and every chair is empty.
Today N bears visit the restaurant, and they arrive one at a time. A bear does not get up from its chair until the restaurant closes.
Each bear that visits has a seat it wants. If the i-th bear to arrive wants seat Ai, the number of the chair it takes must be at least Ai.
When Kangho leads a bear to its seat, he picks the smallest-numbered chair that the bear can sit on.
Given the seats the bears want in order of arrival, write a program that finds which chair each bear sits on.
The first line contains the number of bears N and the distance d. (1≤N≤1000, 1≤d≤106)
The second line contains the seats the bears want, in order of arrival. A wanted seat is a natural number that is at least 1 and at most 106.
Print the chair each bear sits on, in order of arrival, separated by spaces.
Chair numbers have no upper bound, so an answer can be larger than 106.