Codeforces Round 538 (Div. 2)
D
Flood Fill
Codeforces
1900
原题
← CE →
翻译中…

You are given a line of $n$ colored squares in a row, numbered from $1$ to $n$ from left to right. The $i$-th square initially has the color $c_i$.

翻译中…

Let's say, that two squares $i$ and $j$ belong to the same connected component if $c_i = c_j$, and $c_i = c_k$ for all $k$ satisfying $i \lt k \lt j$. In other words, all squares on the segment from $i$ to $j$ should have the same color.

翻译中…

For example, the line $[3, 3, 3]$ has $1$ connected component, while the line $[5, 2, 4, 4]$ has $3$ connected components.

翻译中…

The game "flood fill" is played on the given line as follows:

翻译中…
  • At the start of the game you pick any starting square (this is not counted as a turn).
  • Then, in each game turn, change the color of the connected component containing the starting square to any other color.
翻译中…

Find the minimum number of turns needed for the entire line to be changed into a single color.

翻译中…

Input

翻译中…

The first line contains a single integer $n$ ($1 \le n \le 5000$) — the number of squares.

翻译中…

The second line contains integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 5000$) — the initial colors of the squares.

翻译中…

Output

翻译中…

Print a single integer — the minimum number of the turns needed.

翻译中…

Note

翻译中…

In the first example, a possible way to achieve an optimal answer is to pick square with index $2$ as the starting square and then play as follows:

翻译中…
  • $[5, 2, 2, 1]$
  • $[5, 5, 5, 1]$
  • $[1, 1, 1, 1]$
翻译中…

In the second example, a possible way to achieve an optimal answer is to pick square with index $5$ as the starting square and then perform recoloring into colors $2, 3, 5, 4$ in that order.

翻译中…

In the third example, the line already consists of one color only.

翻译中…

样例

Input #1
4
5 2 2 1
Output #1
2
InputCopy OutputCopy
Input #2
8
4 5 2 2 1 3 5 5
Output #2
4
InputCopy OutputCopy
Input #3
1
4
Output #3
0
InputCopy OutputCopy
题目信息
题号
1114D
时间限制
2 s
内存限制
256 MB
标签
动态规划

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。