#P16821. [蓝桥杯 2026 国 Python B] 小球消除

    ID: 19162 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>区间 DP2026蓝桥杯国赛

[蓝桥杯 2026 国 Python B] 小球消除

Problem Description

There are NN balls in a row, and each ball has a color. The color of the ii-th ball is given by a positive integer cic_i, where the color index satisfies 1≤ci≤C1 \le c_i \le C.

During the game, you may insert any number of balls at any positions in the current sequence. The colors of inserted balls must also be between 11 and CC. You may also repeatedly perform the following elimination operation: choose three consecutive balls in the current sequence; if the first ball and the third ball have the same color, you can delete these three balls at the same time. After deletion, the remaining balls on the left and right become adjacent again. Insertion operations and elimination operations can be alternated in any order.

For example, for the color sequence 1 2 1 31\ 2\ 1\ 3, you can choose the first three balls 1 2 11\ 2\ 1 and delete them, leaving the sequence 33.

Please compute the minimum number of balls that need to be inserted so that the entire sequence can eventually be completely eliminated.

Input Format

The first line contains an integer TT, representing the number of test cases.

For each test case:

  • The first line contains two integers N,CN, C, representing the initial number of balls and the number of color types.
  • The second line contains NN integers c1,c2,…,cNc_1, c_2, \dots, c_N, representing the color of each ball in the initial sequence.

Output Format

For each test case, output one line with one integer, representing the minimum number of balls that need to be inserted.

3
4 2
1 2 1 2
5 3
1 2 3 2 1
3 3
1 2 3
2
1
3

Hint

Sample Explanation

In the first test case, you can insert two balls of color 22, making the sequence 2 1 2 2 1 22\ 1\ 2\ 2\ 1\ 2. First delete the first three balls 2 1 22\ 1\ 2, then delete the remaining 2 1 22\ 1\ 2, and the whole sequence can be eliminated. Therefore, the answer is 22.

In the second test case, you can first delete the middle 2 3 22\ 3\ 2, leaving 1 11\ 1. Then insert one ball of color 22 to get 1 2 11\ 2\ 1 and delete it. Therefore, the answer is 11.

In the third test case, at least 33 balls need to be inserted. For example, first insert two balls of color 11 to form 1 1 11\ 1\ 1 and delete it, leaving 2 32\ 3; then insert one ball of color 22 to form 2 3 22\ 3\ 2 and delete it.

Constraints

For 30%30\% of the testdata, N≤15N \le 15.

For all testdata, T≤20T \le 20, 1≤N≤3001 \le N \le 300, 1≤C≤101 \le C \le 10, 1≤ci≤C1 \le c_i \le C.

Translated by ChatGPT 5