Concert Ticket Packets
EasyStreams · Optiver
A ticket exchange receives a stream of packets and must process them strictly in arrival order. Each packet is a list of four values:
["OFFER", ticket_id, price, quantity]: the seller now offersquantitytickets forticket_idatpriceeach. This replaces any earlier offer for the sameticket_identirely (both price and remaining quantity); leftover tickets from the old offer are gone.["BUY", ticket_id, max_price, quantity]: a buyer wants exactlyquantitytickets forticket_idand will pay at mostmax_priceper ticket.
A buy succeeds only if, at the moment it arrives, there is an offer for that ticket_id whose price is <= max_price and whose remaining quantity is >= quantity. Partial fills are not allowed. A successful buy reduces the offer's remaining quantity and produces the allocation [ticket_id, quantity, total_cost], where total_cost is the offer price times quantity. A failed buy changes nothing and is not retried later.
Return all allocations in the order the successful buys arrived.
Example 1
- Input:
- packets = [["OFFER", "GA", 80, 5], ["BUY", "GA", 100, 2], ["BUY", "GA", 75, 1], ["BUY", "GA", 90, 4], ["OFFER", "GA", 70, 4], ["BUY", "GA", 75, 4]]
- Output:
- [["GA", 2, 160], ["GA", 4, 280]]
- Explanation
- The first buy pays the offer price,
2 * 80 = 160, leaving 3 tickets. The75bid is below the price of 80, and the request for 4 exceeds the 3 left, so both fail. The new offer replaces the old one with 4 tickets at 70, and the final buy takes all 4 for280.
Example 2
- Input:
- packets = [["BUY", "VIP", 500, 1], ["OFFER", "VIP", 300, 2]]
- Output:
- []
- Explanation
- The buy arrives while no offer exists, so it fails, and it is not retried when the offer shows up.
Example 3
- Input:
- packets = [["OFFER", "A", 10, 1], ["OFFER", "B", 20, 3], ["BUY", "A", 50, 2], ["BUY", "B", 20, 3]]
- Output:
- [["B", 3, 60]]
- Explanation
- Ticket
Ahas only 1 left, so the request for 2 fails. TicketB's price exactly equals the limit of 20, which is allowed, and the buy costs60.
Constraints
- 1 ≤ packets.length ≤ 105
- Ticket ids are strings.
- 0 ≤ price, max_price ≤ 106
- 1 ≤ quantity ≤ 104