
ZOJ Problem Set  4117
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 $k$ books, is surprisingly small. Today BaoBao decides to read some books for $n$ minutes by the desk. According to his reading plan, during the $i$th minute, he is scheduled to read book $a_i$. 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 Least Recently Used (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 {4, 3, 4, 2, 3, 1, 4} 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 $(a, b)$ to represent a book, where $a$ is the index of the book, and $b$ is the last time when this book is read.
Given the reading plan, what's the number of times BaoBao fetches a book from the shelf if the value of $k$ (the capacity of the desk) ranges from 1 to $n$ (both inclusive)? InputThere are multiple test cases. The first line of the input contains an integer $T$, indicating the number of test cases. For each test case: The first line contains an integer $n$ ($1 \le n \le 10^5$), indicating the length of the reading plan. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), indicating the indices of the books to read. It's guaranteed that the sum of $n$ of all test cases will not exceed $10^6$. OutputFor each test case output one line containing $n$ integers $f_1, f_2, \dots, f_n$ separated by a space, where $f_i$ indicates the number of times BaoBao fetches a book from the shelf when the capacity of the desk is $i$. Please, DO NOT output extra spaces at the end of each line, or your solution may be considered incorrect! Sample Input1 7 4 3 4 2 3 1 4 Sample Output7 6 5 4 4 4 4 Author: WANG, Yucheng Source: The 10th Shandong Provincial Collegiate Programming Contest 