#abc206d. D - KAIBUNsyo

D - KAIBUNsyo

Score : 400400 points

问题陈述

给定一个包含 NN 个正整数的序列:A=(A1,A2,AN)A=(A_1,A_2, \dots A_N)。你可以执行零次或多次以下操作。至少需要多少次操作才能使 AA 变为回文序列?

  • 选择一对正整数 (x,y)(x,y),并在 AA 中用 yy 替换所有出现的 xx

这里我们说 AA 是回文序列,当且仅当对于每个 ii1iN1 \le i \le N),满足 Ai=AN+1iA_i=A_{N+1-i}

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a sequence of NN positive integers: A=(A1,A2,AN)A=(A_1,A_2, \dots A_N). You can do the operation below zero or more times. At least how many operations are needed to make AA a palindrome?

  • Choose a pair (x,y)(x,y) of positive integers, and replace every occurrence of xx in AA with yy.

Here, we say AA is a palindrome if and only if Ai=AN+1iA_i=A_{N+1-i} holds for every ii (1iN1 \le i \le N).

Constraints

  • All values in input are integers.
  • 1N2×1051 \le N \le 2 \times 10^5
  • 1Ai2×1051 \le A_i \le 2 \times 10^5

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \dots ANA_N

Output

Print the answer as an integer.

Sample Input 1

8
1 5 3 2 5 2 3 1

Sample Output 1

2
  • Initially, we have A=(1,5,3,2,5,2,3,1)A=(1,5,3,2,5,2,3,1).
  • After replacing every occurrence of 33 in AA with 22, we have A=(1,5,2,2,5,2,2,1)A=(1,5,2,2,5,2,2,1).
  • After replacing every occurrence of 22 in AA with 55, we have A=(1,5,5,5,5,5,5,1)A=(1,5,5,5,5,5,5,1).

In this way, we can make AA a palindrome in two operations, which is the minimum needed.

Sample Input 2

7
1 2 3 4 1 2 3

Sample Output 2

1

Sample Input 3

1
200000

Sample Output 3

0

AA may already be a palindrome in the beginning.

update @ 2024/3/10 09:19:31