#P17380. [PacNW 2025] Bus Seating

[PacNW 2025] Bus Seating

Problem Description

A bus has nn rows, numbered 11 through nn, with kk seats in each row. A total of mm people will enter the bus. Each person has a favorite row and receives utility CC from sitting in that row. If a person's favorite row is rxr_x and they sit in row ryr_y, their utility before accounting for other passengers is CrxryC-|r_x-r_y|.

However, the passengers are introverts. Each person already sitting in a row halves the utility a new passenger would receive from that row. Formally, if pp people are already sitting in row ryr_y, then a passenger whose favorite row is rxr_x receives

Crxry2p\frac{C-|r_x-r_y|}{2^p}

utility by sitting in row ryr_y. A row with all kk seats occupied cannot be selected.

Each person chooses a row that maximizes their utility at the moment they sit down. If several rows give the same maximum utility, they choose the row with the smallest number. Nobody changes rows after sitting down. Determine where every passenger sits.

Input Format

The first line contains four integers nn, kk, mm, and CC (1n,k,m21051\le n,k,m\le2\cdot10^5, nC109n\le C\le10^9, and mnkm\le n\cdot k): the number of rows, the number of seats per row, the number of passengers, and the utility of sitting in one's favorite row.

The second line contains mm integers a1,a2,,ama_1,a_2,\ldots,a_m (1ain1\le a_i\le n), where aia_i is passenger ii's favorite row. Passengers sit down in input order.

Output Format

Output one line containing mm integers b1,b2,,bmb_1,b_2,\ldots,b_m, where bib_i is the row chosen by passenger ii.

3 2 6 4
3 2 3 2 2 1
3 2 1 2 1 3
2 5 8 1000000000
2 2 2 2 2 2 2 2
2 1 2 1 2 1 2 1

Hint

In the first sample, the utilities available to each passenger are as follows:

  1. A passenger who prefers row 33 sees utilities [2,3,4][2,3,4] and chooses row 33.

  2. A passenger who prefers row 22 sees utilities [3,4,1.5][3,4,1.5] and chooses row 22.

  3. A passenger who prefers row 33 sees utilities [2,1.5,2][2,1.5,2] and chooses row 11.

  4. A passenger who prefers row 22 sees utilities [1.5,2,1.5][1.5,2,1.5] and chooses row 22.

  5. A passenger who prefers row 22 sees utilities [1.5,1,1.5][1.5,1,1.5] and chooses row 11.

  6. A passenger who prefers row 11 sees utilities [1,0.75,1][1,0.75,1]. Row 11 is full, so they choose row 33.