Center Rearranging

AtCoder
IDacl1_f
Time2000ms
Memory256MB
Difficulty
Given are integer sequences $A$ and $B$ of length $3N$. Each of these two sequences contains three copies of each of $1, 2, \dots, N$. In other words, $A$ and $B$ are both arrangements of $(1, 1, 1, 2, 2, 2, \dots, N, N, N)$. Tak can perform the following operation to the sequence $A$ arbitrarily many times: * Pick a value from $1, 2, \dots, N$ and call it $x$. $A$ contains exactly three copies of $x$. Remove the middle element of these three. After that, append $x$ to the beginning or the end of $A$. Check if he can turn $A$ into $B$. If he can, print the minimum required number of operations to achieve that. ## Constraints * $1 \leq N \leq 33$ * $A$ and $B$ are both arrangements of $(1, 1, 1, 2, 2, 2, \dots, N, N, N)$. * All values in input are integers. ## Input Input is given from Standard Input in the following format: $N$ $A_1$ $A_2$ ... $A_{3N}$ $B_1$ $B_2$ ... $B_{3N}$ [samples]
Samples
Input #1
3
2 3 1 1 3 2 2 1 3
1 2 2 3 1 2 3 1 3
Output #1
4

For example, Tak can perform operations as follows:

*   `2 3 1 1 3 2 2 1 3` (The initial state)
*   `2 2 3 1 1 3 2 1 3` (Pick $x = 2$ and append it to the beginning)
*   `2 2 3 1 3 2 1 3 1` (Pick $x = 1$ and append it to the end)
*   `1 2 2 3 1 3 2 3 1` (Pick $x = 1$ and append it to the beginning)
*   `1 2 2 3 1 2 3 1 3` (Pick $x = 3$ and append it to the end)
Input #2
3
1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3
Output #2
0
Input #3
3
2 3 3 1 1 1 2 2 3
3 2 2 1 1 1 3 3 2
Output #3
\-1
Input #4
8
3 6 7 5 4 8 4 1 1 3 8 7 3 8 2 4 7 5 2 2 6 5 6 1
7 5 8 1 3 6 7 5 4 8 1 3 3 8 2 4 2 6 5 6 1 4 7 2
Output #4
7
API Response (JSON)
{
  "problem": {
    "name": "Center Rearranging",
    "description": {
      "content": "Given are integer sequences $A$ and $B$ of length $3N$. Each of these two sequences contains three copies of each of $1, 2, \\dots, N$. In other words, $A$ and $B$ are both arrangements of $(1, 1, 1, 2",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "acl1_f"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Given are integer sequences $A$ and $B$ of length $3N$. Each of these two sequences contains three copies of each of $1, 2, \\dots, N$. In other words, $A$ and $B$ are both arrangements of $(1, 1, 1, 2...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments