AtCoder Regular Contest 181
F
Colorful Reversi
AtCoder
⌀ 3664
原题
← E→
翻译中…

Problem Statement

翻译中…

You are given an integer sequence $A=(A_1,A_2,\dots,A_N)$ of length $N$. On this sequence, the following operation can be performed:

翻译中…
  • Choose $l$ and $r$ $(1\leq l < r \leq N)$ such that $A_l=A_r$, $A_{l+1}=A_{l+2}=\dots=A_{r-1}$, and $A_{l+1}\neq A_l$. Replace each of $A_{l+1},A_{l+2},\dots,A_{r-1}$ with $A_l$. The cost of this operation is $r-l-1$.
翻译中…

You will repeat this operation until there is no $l$ and $r$ $(1\leq l < r \leq N)$ such that $A_l=A_r$, $A_{l+1}=A_{l+2}=\dots=A_{r-1}$, and $A_{l+1}\neq A_l$. Find the minimum total cost of such a series of operations.

翻译中…

Constraints

翻译中…
  • $3 \leq N \leq 5 \times 10^5$
  • $1 \leq A_i \leq N$
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

N
A_1 A_2 \dots A_N

翻译中…

Output

翻译中…

Print the answer.

翻译中…

样例

Input #1
7
1 2 3 2 3 2 1
Output #1
7
For example, if you perform the operation with $(l,r)=(3,5), (2,6), (1,7)$ in this order, $A$ changes as follows: $(1,2,3,2,3,2,1)\rightarrow (1,2,3,3,3,2,1) \rightarrow (1,2,2,2,2,2,1) \rightarrow (1,1,1,1,1,1,1)$, after which there is no $l$ and $r$ with the said property. The total cost of this series of operations is $1+3+5=9$. On the other hand, if you perform the operation with $(l,r)=(2,4), (4,6), (1,7)$ in this order, $A$ changes as follows: $(1,2,3,2,3,2,1)\rightarrow (1,2,2,2,3,2,1) \rightarrow (1,2,2,2,2,2,1) \rightarrow (1,1,1,1,1,1,1)$. The total cost of this series of operations is $1+1+5=7$.
Input #2
5
1 2 3 4 5
Output #2
0
Input #3
40
1 2 3 4 5 6 7 8 7 6 5 6 7 8 7 6 5 4 3 2 2 1 2 3 4 5 4 5 6 7 8 7 7 6 5 6 6 7 8 8
Output #3
44
题目信息
题号
arc181_f
时间限制
2 s
内存限制
1024 MB

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