Colorful Reversi

AtCoder
IDarc181_f
Time2000ms
Memory256MB
Difficulty
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$ [samples]
Samples
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
API Response (JSON)
{
  "problem": {
    "name": "Colorful Reversi",
    "description": {
      "content": "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$,",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "arc181_f"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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:\n\n*   Choose $l$ and $r$ $(1\\leq l < r \\leq N)$ such that $A_l=A_r$,...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments