#P14046. [SDCPC 2019] BaoBao Loves Reading
[SDCPC 2019] BaoBao Loves Reading
题目描述
BaoBao is a good student who loves reading, but compared with his huge bookshelf containing lots and lots of books, his reading desk, which can only hold at most books, is surprisingly small.
Today BaoBao decides to read some books for minutes by the desk. According to his reading plan, during the -th minute, he is scheduled to read book . The reading desk is initially empty and all the books are initially on the shelf. If the book BaoBao decides to read is not on the desk, BaoBao will have to fetch it from the shelf. Also, if the desk is full and BaoBao has to fetch another book from the shelf, he will have to put one book back from the desk to the shelf before fetching the new book.
Tired of deciding which book to put back, BaoBao searches the Internet and discovers an algorithm called the (LRU) algorithm. According to the algorithm, when BaoBao has to put a book back from the desk to the shelf, he should put back the least recently read book.
For example, let's consider the reading plan and assume that the capacity of the desk is 3. The following table explains what BaoBao should do according to the LRU algorithm. Note that in the following table, we use a pair of integer to represent a book, where is the index of the book, and is the last time when this book is read.
$$\begin{array}{|c|c|c|} \hline \textbf{Minute} & \textbf{Books on the Desk} & \textbf{BaoBao's Action} \\ & \textbf{Before This Minute} & \\ \hline 1 & \{\} & \text{Fetch book 4 from the shelf} \\ \hline 2 & \{(4, 1)\} & \text{Fetch book 3 from the shelf} \\ \hline 3 & \{(4, 1), (3, 2)\} & \text{Do nothing as book 4 is already on the desk} \\ \hline 4 & \{(4, 3), (3, 2)\} & \text{Fetch book 2 from the shelf} \\ \hline 5 & \{(4, 3), (3, 2), (2, 4)\} & \text{Do nothing as book 3 is already on the desk} \\ \hline 6 & \{(4, 3), (3, 5), (2, 4)\} & \text{Put book 4 back to the shelf as its the least recently read book,} \\ & & \text{and fetch book 1 from the shelf} \\ \hline 7 & \{(3, 5), (2, 4), (1, 6)\} & \text{Put book 2 back to the shelf as its the least recently read book,} \\ & & \text{and fetch book 4 from the shelf} \\ \hline \end{array}$$Given the reading plan, what's the number of times BaoBao fetches a book from the shelf if the value of (the capacity of the desk) ranges from 1 to (both inclusive)?
输入格式
There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:
The first line contains an integer (), indicating the length of the reading plan.
The second line contains integers (), indicating the indices of the books to read.
It's guaranteed that the sum of of all test cases will not exceed .
输出格式
For each test case output one line containing integers separated by a space, where indicates the number of times BaoBao fetches a book from the shelf when the capacity of the desk is .
Please, DO NOT output extra spaces at the end of each line, or your solution may be considered incorrect!
1
7
4 3 4 2 3 1 4
7 6 5 4 4 4 4