#P1844. Reading Room Simulation
Reading Room Simulation
Reading Room Simulation
A reading room opens at time \(O\) and closes at time \(T\). Every day a large number of readers arrive at different times. Each reader has a list of at most 5 publications they wish to read – ranked in order of preference. Upon arrival, a reader will try to find his most preferred publication that is currently available. If it is not available, he will try his second choice, and so on. If none of the desired publications is available at that moment, the reader will enter a waiting state and register at the service desk. Later when one or more of his desired publications are returned to the shelf, he will attempt to get one. When more than one reader tries to obtain the same publication at the same moment (either immediately upon arrival or after waiting), the following conflict resolution rule is applied:
- If one or more readers had registered (i.e. were waiting), the publication is given to the reader with the smallest registration number (i.e. the one who registered first). If two waiting readers registered at the same time, then the one with the earlier arrival time gets the publication.
- If none of the competing readers was waiting (i.e. they all immediately picked the publication), then the one with the earlier arrival time gets it.
When a reader starts reading a publication (even if he does not finish it), the read count for that publication is increased by 1. After finishing, the reader returns the publication and then proceeds to locate the next publication in his preference list that he has not read yet. This continues until he has read all the publications he desires or until the reading room closes. At closing time \(T\), all readers are forced to leave, even if they are waiting or reading.
Your task is to simulate this process and output the total number of times any publication was read.
Note: For simulation purposes, assume that reading any publication takes exactly 1 unit of time. That is, if a reader starts reading at time \(t\) (with \(t < T\)), he will finish and return the publication at time \(t+1\) (reading events starting at time \(T\) are not allowed).
inputFormat
The input begins with two integers \(O\) and \(T\) (with \(O < T\)), representing the opening and closing times of the reading room. The next line contains an integer \(n\) representing the number of readers. Each of the following \(n\) lines describes one reader in the following format:
arrival_time k p1 p2 ... pk
Here, arrival_time
is an integer (\(\ge O\) and \(< T\)) that indicates the time the reader enters the reading room, k
(\(1 \le k \le 5\)) is the number of publications the reader wishes to read, and p1, p2, ..., pk
are the identifiers (integers) for the publications, given in order of preference. Initially, each publication is available (there is exactly one copy of each publication).
outputFormat
Output a single integer — the total number of times any publication was read during the day.
sample
0 10
3
0 2 1 2
0 1 1
1 2 2 1
5