Sociophobe
Time limit2sMemory limit256 MB
Simulate a ticket system that seats each buyer in the emptiest compartment and rebalances passengers when a return creates a gap of two, then print the final compartment rosters.
- Level
Medium7 of 10
- Topics
- Simulation, Heap, Implementation, Greedy
- Solved
- No attempts yet
Problem
Not everyone enjoys being in a crowd all the time. Many people prefer solitude and are even willing to pay for it. That is why OAO "Joyful Railways" introduced a new service called "Sociophobe" on its ticket sales website. The service exists to let every passenger ride in a compartment with as few neighbors as possible. Its rules are as follows.
Suppose tickets are sold for a certain car in which some compartments are already occupied. If at some moment the next passenger buys a ticket for this car, they are sold a seat in the emptiest compartment, that is, a compartment whose number of people is no greater than in all the others. If there are several such compartments, the one with the smallest number is chosen. If a passenger from some compartment returns their ticket and the difference between the number of people in that compartment and in the most occupied compartment becomes at least two, then a passenger moves from the most occupied compartment into the freed seat, namely the passenger who bought their ticket earlier than the others, that is, the passenger with the smallest number. If there are several most occupied compartments, the one closest to the compartment where the ticket was returned is chosen. If there are several such compartments as well, the one with the smallest number is chosen.
Given the records of how passengers bought and returned tickets, output the final distribution of passengers among the compartments. It is guaranteed that every time a passenger buys a ticket, the car has at least one free seat.
Input
The first line contains three integers m, n, and k (1 ≤ m ≤ 200000, 1 ≤ n, k ≤ 50000): the number of ticket purchase or return operations, the number of compartments in the car, and the number of seats in each compartment, respectively. The next m lines give the sequence of ticket purchases and returns.
If a line contains the single character "+", it means the next passenger bought a ticket for the car, and the passenger's number equals the ordinal number of the plus sign in the input file. If a line has the form "− id", where id is an integer from one to m, it means the passenger with number id returned their ticket. It is guaranteed that this passenger holds a ticket at that moment.
Output
Output n lines, where the first number li of the i-th line is how many tickets were sold in the i-th compartment, followed by li numbers: the numbers of the passengers who will ride in the i-th compartment, listed in increasing order. It is guaranteed that an answer always exists.