#P3694. 邦邦的大合唱站队

    ID: 2661 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划,dp枚举,暴力状态压缩,状压前缀和洛谷月赛

邦邦的大合唱站队

Description

There are NN idols standing in a line, coming from MM different bands. Each band has at least one idol.

We want to rearrange the line so that idols from the same band stand together contiguously. The way to rearrange is: let some idols leave the line (the remaining idols do not move), then let the removed idols return one by one to fill the vacated positions; they may return to any empty positions.

What is the minimum number of idols that must leave the line?

Input Format

The first line contains 22 integers NN and MM.

Then there are NN lines, each containing an integer ai(1aiM)a_i(1\le a_i \le M), representing the band ID of the ii-th idol in the line.

Output Format

Output a single integer, the answer.

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

Hint

Sample explanation:

1  3   √
3  3
2  3   √
4  4
2  4   √
1  2   √
2  2
3  2   √
1  1
1  1
3  1   √
4  1   √

Constraints:

  • For 20%20\% of the testdata, N20,M=2N\le 20, M=2.
  • For 40%40\% of the testdata, N100,M4N\le 100, M\le 4.
  • For 70%70\% of the testdata, N2000,M10N\le 2000, M\le 10.
  • For all testdata, 1N105,M201\le N\le 10^5, M\le 20.

Translated by ChatGPT 5