Book Replacement
Time limit1sMemory limit128 MB
Simulate a library queue where a librarian relocates books between capacity-limited desks and a shelf using an LRU-like eviction rule, and compute total access cost.
- Level
Medium6 of 10
- Topics
- Simulation, Queue, Implementation
- Solved
- No attempts yet
Problem
The deadline for Professor Hachioji's assignment is tomorrow. To finish it, students must copy pages from many reference books in the library. Every reference book is kept in a storeroom that only the librarian may enter, so a student who wants a copy must ask the librarian, who fetches the requested book from the storeroom and makes the copy.
Students wait in a single queue in front of the counter. A student may request only one book at a time. After a request has been served, if the student still has more books to request, the student goes to the back of the queue.
The overall situation is shown in Figure 1 (The Library).
The storeroom contains desks and one shelf, placed in a line in that order from the door toward the back of the room. Each desk holds at most books. Initially every desk is empty (all books start on the shelf), and no new students arrive after the library opens.
To serve a request, the librarian looks for the book on in that order and finally on the shelf. After finding the book, the librarian takes it and gives a copy of the page to the student. The librarian then returns the book to using the following procedure.
- If is not full (it currently holds fewer than books), put the requested book on .
- If is full, the librarian:
- temporarily puts the requested book on the non-full desk closest to the entrance, or on the shelf if every desk is full;
- takes from the book that has not been requested for the longest time (the least recently used book);
- puts that book on the non-full desk other than closest to the entrance, or on the shelf if every desk other than is full;
- takes the requested book back from its temporary place;
- finally puts the requested book on .
Cost. Only accesses to a desk or the shelf cost anything: each time a book is put onto or taken from a location, that counts as one access. An access to desk costs , and an access to the shelf costs . Every other action is free. Simulate the students and the librarian and report the total cost of processing all requests.
Cost accounting example. Suppose there are desks, each holding at most book, and two students: the first requests books and the second requests . Because a student who still has requests rejoins the back of the queue, the books are served in the order . Serving book first costs (take it from the shelf for , put it on for ). Serving then costs , and serving costs respectively, for a total of .
Input
The input consists of several datasets. Each dataset has the following form.
m c n
k1
b11 ... b1k1
...
kn
bn1 ... bnkn
All values are positive integers. is the number of desks with . is the maximum number of books allowed on one desk with . is the number of students with . For the -th student, is the number of books requested with , and are the IDs of the requested books, given in request order. Every book has a distinct ID, and each ID is less than ; a student may request the same book more than once.
The end of the input is a line containing three zeros separated by spaces (0 0 0); it is not a dataset.
Output
For each dataset, output the total cost of processing all of its requests on its own line.