QOJ.ac

QOJ

実行時間制限: 1 s メモリ制限: 256 MB 満点: 100 ハック可能 ✓

#6701. BaoBao 爱读书

統計

BaoBao 是一个热爱阅读的好学生,但他那巨大的书架上放着许许多多的书,而他的阅读桌却小得惊人,最多只能容纳 $k$ 本书。

今天,BaoBao 决定在桌边阅读 $n$ 分钟。根据他的阅读计划,在第 $i$ 分钟,他计划阅读书 $a_i$。阅读桌最初是空的,所有的书最初都在书架上。如果 BaoBao 决定阅读的书不在桌上,他就必须从书架上取下这本书。此外,如果桌子已满,而 BaoBao 需要从书架上取另一本书,他必须先将桌上的一本书放回书架,然后再取新书。

为了不再纠结该把哪本书放回书架,BaoBao 在网上搜索并发现了一种名为“最近最少使用”(Least Recently Used, LRU)的算法。根据该算法,当 BaoBao 需要将一本书从桌上放回书架时,他应该放回最近最少阅读的那本书。

例如,考虑阅读计划 $\{4, 3, 4, 2, 3, 1, 4\}$,并假设桌子的容量为 $3$。下表解释了 BaoBao 根据 LRU 算法应采取的操作。注意,在下表中,我们使用整数对 $(a, b)$ 来表示一本书,其中 $a$ 是书的编号,$b$ 是这本书最后一次被阅读的时间。

分钟 该分钟前桌上的书 BaoBao 的操作
1 {} 从书架取书 4
2 {(4, 1)} 从书架取书 3
3 {(4, 1), (3, 2)} 书 4 已在桌上,无需操作
4 {(4, 3), (3, 2)} 从书架取书 2
5 {(4, 3), (3, 2), (2, 4)} 书 3 已在桌上,无需操作
6 {(4, 3), (3, 5), (2, 4)} 将书 4 放回书架(因为它是最近最少阅读的),并从书架取书 1
7 {(3, 5), (2, 4), (1, 6)} 将书 2 放回书架(因为它是最近最少阅读的),并从书架取书 4

给定阅读计划,当桌子的容量 $k$ 从 $1$ 到 $n$(包含 $1$ 和 $n$)变化时,请计算 BaoBao 从书架取书的总次数。

输入包含多组测试数据。第一行包含一个整数 $T$,表示测试数据的组数。对于每组测试数据:

第一行包含一个整数 $n$ ($1 \le n \le 10^5$),表示阅读计划的长度。

第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$),表示要阅读的书的编号。

保证所有测试数据的 $n$ 之和不超过 $10^6$。

对于每组测试数据,输出一行,包含 $n$ 个整数 $f_1, f_2, \dots, f_n$,用空格分隔,其中 $f_i$ 表示当桌子容量为 $i$ 时,BaoBao 从书架取书的总次数。

请不要在每行末尾输出多余的空格,否则你的答案可能会被判为错误!

样例

输入格式 1

1
7
4 3 4 2 3 1 4

输出格式 1

7 6 5 4 4 4 4

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.