CF1114D.Flood Fill
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 ci.
Let's say, that two squares i and j belong to the same connected component if ci=cj, and ci=ck 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.
给你一行 n 个有颜色的方块,从左到右依次编号为 1 到 n。第 i 个方块初始颜色为 ci。
我们称两个方块 i 和 j 属于同一个连通块,当且仅当 ci=cj,且对所有满足 i<k<j 的 k,均有 ci=ck。换言之,从 i 到 j 这一区间内所有方块的颜色必须完全相同。
例如,序列 [3,3,3] 有 1 个连通块,而序列 [5,2,4,4] 有 3 个连通块。
“洪水填充”(flood fill)游戏在该行方块上进行,规则如下:
- 游戏开始时,你可以任选一个起始方块(此操作不计入回合数);
- 此后,每一轮游戏中,将包含起始方块的连通块的颜色更改为任意其他颜色。
求使整行方块变为同一种颜色所需的最少回合数。
输入格式
The first line contains a single integer n (1≤n≤5000) — the number of squares.
The second line contains integers c1,c2,…,cn (1≤ci≤5000) — the initial colors of the squares.
第一行包含一个整数 n(1≤n≤5000)——正方形的数量。
第二行包含整数 c1,c2,…,cn(1≤ci≤5000)——各正方形的初始颜色。
输出格式
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.
在第一个例子中,一种达到最优答案的可行方法是选择索引为 2 的方格作为起始方格,然后按如下方式操作:
- [5,2,2,1]
- [5,5,5,1]
- [1,1,1,1]
在第二个例子中,一种达到最优答案的可行方法是选择索引为 5 的方格作为起始方格,然后依次将其重染为颜色 2,3,5,4。
在第三个例子中,该行已全部为同一种颜色。
输入解题思路,AI测评打分。不知道怎么写?