A12447 | Flood Fill
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 < k < 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.
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 < k < 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.
输入格式
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.
The second line contains integers $c_1, c_2, \ldots, c_n$ ( $1 \le c_i \le 5000$ ) — the initial colors of the squares.
输出格式
Print a single integer — the minimum number of the turns needed.
输入输出样例
输入 #1
4 5 2 2 1
输出 #1
2
输入 #2
8 4 5 2 2 1 3 5 5
输出 #2
4
输入 #3
1 4
输出 #3
0
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.
- $[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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted