QOJ.ac

QOJ

Time Limit: 10 s Memory Limit: 256 MB Total points: 100

#23854. 整理毛绒玩具

Statistics

题目描述

某位 JOI 相关人员在一家玩具店工作。今天,他负责整理店内的毛绒玩具专区。

在毛绒玩具专区的货架上,从左到右排成一列摆放着 $N$ 个毛绒玩具。货架被隔板划分为 $N$ 个格子,每 $1$ 个格子里放 $1$ 个毛绒玩具。这家玩具店总共出售 $M$ 种毛绒玩具,分别编号为 $1$ 到 $M$。摆放在货架上的 $N$ 个毛绒玩具分别属于这 $M$ 种中的某一种。此外,每种毛绒玩具都至少存在 $1$ 个。

为了美观,想要重新排列毛绒玩具,使得相同种类的毛绒玩具全部在货架上连续摆放。为此决定按如下方法重新排列毛绒玩具:

  • 从 $N$ 个毛绒玩具中选出若干个,将它们从货架上取出。不移动未被取出的毛绒玩具的位置。
  • 将取出的毛绒玩具按任意顺序放回货架上空着的格子中。

重新排列后,相同种类的毛绒玩具必须全部在货架上连续摆放。请编写一个程序,求出为了完成重新排列所需取出的毛绒玩具数量的最小值。

输入格式

输入由 $1 + N$ 行组成。

在第 $1$ 行中,写有以空格分隔的 $2$ 个整数 $N, M$($1 \le N \le 100000$,$1 \le M \le 20$),表示共有 $N$ 个毛绒玩具,且共有 $M$ 个种类。

在接下来的 $N$ 行中,每行写有一个在 $1$ 以上且在 $M$ 以下的整数。在 $N$ 行中的第 $i$ 行($1 \le i \le N$)所写的整数,表示摆放在货架从左数第 $i$ 个格子里的毛绒玩具的种类。保证对于每种毛绒玩具,都至少存在 $1$ 个该种类的毛绒玩具。

输出格式

请用 $1$ 行输出为了完成重新排列所需取出的毛绒玩具数量的最小值。

样例输入与输出

样例输入 1

7 2
1
2
2
2
1
2
1

样例输出 1

2

样例输入 2

12 4
1
3
2
4
2
1
2
3
1
1
3
4

样例输出 2

7

在样例输入 1 中,最初摆放的毛绒玩具种类从左往右依次为 $1, 2, 2, 2, 1, 2, 1$。为了使重新排列时取出的毛绒玩具数量最小,可以取出从左数第 $1$ 个和第 $6$ 个毛绒玩具,并在从左数第 $1$ 个位置放上种类为 $2$ 的毛绒玩具,在从左数第 $6$ 个位置放上种类为 $1$ 的毛绒玩具。此时,取出的毛绒玩具数量为 $2$ 个。

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.