QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 512 MB 満点: 120

#11437. 鞋子

統計

Lana 有 $n$ 双鞋,编号为 $1$ 到 $n$。所有的鞋子都存放在一个长衣柜中。编号为 $1$ 的鞋子最初位于衣柜的最上方(靠近门),而编号为 $n$ 的鞋子位于最下方(远离门)。

在接下来的 $q$ 天中,第 $i$ 天 Lana 想要穿编号为 $a_i$ 的鞋子。为了从衣柜中取出某双鞋,她必须先移走所有比这双鞋更靠近门的鞋子,然后才能取出目标鞋子。取出目标鞋子后,她会将其他鞋子按原来的顺序放回衣柜。从衣柜中取出一双鞋需要 $1$ 秒,而将鞋子放回衣柜不需要额外时间。

在每天结束时,Lana 会脱下鞋子,并执行以下操作之一: 将它们放回衣柜的最上方,或者 如果走廊有空间,将它们留在走廊里。

走廊最多可以容纳 $m$ 双鞋。此外,Lana 可以随时将走廊里的任何鞋子移到衣柜的最上方(取鞋过程中除外)。如果某天开始时,目标鞋子已经在走廊里,Lana 可以立即穿上它们,而无需花费任何时间取鞋。

Lana 非常忙,想要最小化她在接下来的 $q$ 天中取鞋所花费的总时间。请帮助她确定她能花费的最小取鞋时间。

输入格式

第一行包含整数 $n, m$ 和 $q$ ($1 \le n \le 2 \cdot 10^5, 0 \le m \le 2 \cdot 10^5, 1 \le q \le 10^6$),分别表示鞋子的数量、走廊的容量以及天数。

第二行包含 $q$ 个整数 $a_i$ ($1 \le a_i \le n$),表示 Lana 在第 $i$ 天想要穿的鞋子编号。

输出格式

输出一行,包含在 $q$ 天内取鞋所需的最少总时间。

样例

样例输入 1

5 1 6
2 1 2 1 2 1

样例输出 1

5

说明 1

在第一天,Lana 从衣柜中取出编号为 $2$ 的鞋子。此操作花费她 $2$ 秒。在这一天结束时,她将这些鞋子留在走廊里并无限期地保留在那里。

现在,每当她需要从衣柜中取出编号为 $1$ 的鞋子时,都需要花费 $1$ 秒。然而,如果她需要编号为 $2$ 的鞋子,她可以直接从走廊穿上它们,无需花费任何时间。

她取鞋花费的总时间为:$2 + 1 + 0 + 1 + 0 + 1 = 5$ 秒。

样例输入 2

6 0 4
5 4 3 4

样例输出 2

17

样例输入 3

3 2 7
1 2 3 2 3 1 3

样例输出 3

4

子任务

子任务 分值 数据范围
1 17 $n, m, q \le 1\,000$
2 27 $n = m$
3 37 $m = 0$
4 24 $q \le 2 \cdot 10^5$
5 15 无附加限制

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.