1D puyopuyo

AtCoder
IDabc438_c
Time2000ms
Memory256MB
Difficulty
You are given an integer sequence $A=(A_1,A_2,\ldots,A_N)$ of length $N$. You can perform the following operation zero or more times in any order: * Choose an integer $k$ between $1$ and $|A|-3$, inclusive, such that $A_k=A_{k+1}=A_{k+2}=A_{k+3}$, and remove $A_k,A_{k+1},A_{k+2},A_{k+3}$ from $A$. (More formally, replace $A$ with $(A_1,A_2,\ldots,A_{k-1},A_{k+4},A_{k+5},\ldots,A_N)$.) Here, $|A|$ represents the length of the integer sequence $A$. Find the minimum possible value of the final $|A|$ after repeating the operation. ## Constraints * $1\le N\le 2\times 10^5$ * $1\le A_i\le N$ * All input values are integers. ## Input The input is given from Standard Input in the following format: $N$ $A_1$ $A_2$ $\ldots$ $A_N$ [samples]
Samples
Input #1
10
1 1 1 4 4 4 4 1 2 3
Output #1
2

You can make $|A|=2$ with two operations as follows:

*   Choose $k=4$. This choice is valid since $A_4=A_5=A_6=A_7=4$ holds. $A=(1,1,1,1,2,3)$ is obtained.
*   Choose $k=1$. This choice is valid since $A_1=A_2=A_3=A_4=1$ holds. $A=(2,3)$ is obtained.

It is impossible to make $|A|$ less than $2$, so output $2$.
Input #2
3
2 1 3
Output #2
3

You cannot perform any operation from the beginning.
Input #3
13
1 1 4 4 4 1 1 1 1 4 1 4 1
Output #3
5
API Response (JSON)
{
  "problem": {
    "name": "1D puyopuyo",
    "description": {
      "content": "You are given an integer sequence $A=(A_1,A_2,\\ldots,A_N)$ of length $N$. You can perform the following operation zero or more times in any order: *   Choose an integer $k$ between $1$ and $|A|-3$, i",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc438_c"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given an integer sequence $A=(A_1,A_2,\\ldots,A_N)$ of length $N$.\nYou can perform the following operation zero or more times in any order:\n\n*   Choose an integer $k$ between $1$ and $|A|-3$, i...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments