CF1114D.Flood Fill

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a line of nn colored squares in a row, numbered from 11 to nn from left to right. The ii -th square initially has the color cic_i .

Let's say, that two squares ii and jj belong to the same connected component if ci=cjc_i = c_j , and ci=ckc_i = c_k for all kk satisfying i<k<ji < k < j . In other words, all squares on the segment from ii to jj should have the same color.

For example, the line [3,3,3][3, 3, 3] has 11 connected component, while the line [5,2,4,4][5, 2, 4, 4] has 33 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 nn ( 1n50001 \le n \le 5000 ) — the number of squares.

The second line contains integers c1,c2,,cnc_1, c_2, \ldots, c_n ( 1ci50001 \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 22 as the starting square and then play as follows:

  • [5,2,2,1][5, 2, 2, 1]
  • [5,5,5,1][5, 5, 5, 1]
  • [1,1,1,1][1, 1, 1, 1]

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

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

首页