#P16303. [蓝桥杯 2026 省 Java C 组] 小蓝的序列

[蓝桥杯 2026 省 Java C 组] 小蓝的序列

Problem Description

Xiaolan believes that a sequence is “good” if and only if it satisfies the following conditions:

  1. Exactly two different integers appear in the sequence.
  2. Any pair of adjacent elements are different.
  3. For all valid indices ii (1≤i≤n−21 \le i \le n - 2), we have ai=ai+2a_i = a_{i+2}.

Equivalently, a good sequence must look like

x,y,x,y,x,y,…x, y, x, y, x, y, \dots

or

y,x,y,x,y,x,…y, x, y, x, y, x, \dots

where x≠yx \ne y, and only these two numbers appear in the entire sequence.

Now Xiaolan has a sequence of length nn. He can perform any number of modification operations. In each operation, he can change one element in the sequence to any positive integer.

Xiaolan wants to know: what is the minimum number of elements that must be modified to turn the current sequence into a good sequence?

Input Format

The input consists of two lines.

The first line contains a positive integer nn, representing the length of the sequence.

The second line contains nn positive integers a1,a2,⋯ ,ana_1, a_2, \cdots, a_n, representing Xiaolan's current sequence.

Output Format

Output one line containing a non-negative integer cc, representing the minimum number of elements that must be modified to make the sequence a good sequence.

5
1 1 1 1 2
2

Hint

Sample Explanation

One optimal plan is to change the 11st and the 33rd numbers to 22. Then the sequence becomes:

2,1,2,1,22, 1, 2, 1, 2

This is a good sequence. It can be verified that there is no way to satisfy the conditions by modifying only 11 element, so the answer is 22.

Constraints

  • For 30%30\% of the testdata, n≤8n \le 8.
  • Another 20%20\% of the testdata satisfy: for all 1≤i,j≤n1 \le i, j \le n, we have ai=aja_i = a_j.
  • For all testdata, 2≤n≤1062 \le n \le 10^6, and 1≤ai≤1061 \le a_i \le 10^6.

Translated by ChatGPT 5