ExpertSimulation & Finance50 points

Nearest-driver dispatch in Jakarta

A ride-hailing dispatcher in Jakarta works along a single long avenue measured in kilometres. Drivers move at exactly 1 km per minute. The dispatch rule decides who waits and who drives, so it must be followed to the letter.

Task: line 1 holds the starting positions of the drivers (integers separated by spaces); driver numbers start at 1 and all drivers are idle at minute 0. Line 2 is the number of requests n. Then n lines t pickup dropoff (request minute and two positions; t never decreases). Requests are handled strictly one at a time in input order. Request i is assigned at moment A_i, the earliest moment that is at least its own t, at least A_(i-1) (use 0 for the first request) and at which some driver is idle; a driver whose trip ends at minute x is idle from minute x on. Among the idle drivers choose the one closest to the pickup position; ties go to the lowest driver number. That driver reaches the pickup at A_i + distance, so the passenger's wait is A_i + distance - t, then drives to the dropoff (taking |pickup - dropoff| minutes), becomes idle there and stays at the dropoff position. Print Request i: driver j wait w for each request (i from 1), then Average wait: X (2 decimals) and Final positions: p1 p2 ... in driver order.

Sample tests

Input

0 10 20
5
0 12 30
1 2 8
2 25 5
3 9 0
4 18 18

Expected output

Request 1: driver 2 wait 2
Request 2: driver 1 wait 2
Request 3: driver 3 wait 5
Request 4: driver 1 wait 7
Request 5: driver 1 wait 33
Average wait: 9.80
Final positions: 18 30 5

Input

5
3
0 5 15
1 20 0
2 7 7

Expected output

Request 1: driver 1 wait 0
Request 2: driver 1 wait 14
Request 3: driver 1 wait 40
Average wait: 18.00
Final positions: 7

Your answer is also checked against 3 hidden tests.