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.