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.