#P9653. 『GROI-R2』 不空白的画布

『GROI-R2』 不空白的画布

Problem Description

As we all know, after Alice hid away, Tenniel sat in front of a blank canvas, picked up a piece of charcoal, and began to draw.

But now the canvas is no longer blank, because the current scenery is already on it. Suppose the canvas length is nn. The color at each unit position can be represented by a positive integer in the range [1,k][1,k].

Tenniel still wants to draw the teacup he has knocked over. Each time he draws, he may choose any position on the canvas and repaint the color at that position into any positive integer in [1,k][1,k].

In the end, we know this painting has memory. Define the number of memory fragments left on the painting as the number of consecutive blocks of the same color. Now Tenniel wants to know: given an upper limit on the number of times he can repaint, what is the maximum possible number of memory fragments on the painting?

Formal statement

You have nn consecutive cells. Each cell has an initial color cic_i, and it is guaranteed that 1cik1 \le c_i \le k.

You may perform at most mm operations. Each operation changes the color of one cell, and the new color must still be in [1,k][1,k].

We call a maximal consecutive segment of the same color one block. You need to find the maximum number of blocks after at most mm operations.

Input Format

This problem has multiple test cases.

The first line contains a positive integer TT, denoting the number of test cases.

For each test case, the first line contains three positive integers n,m,kn,m,k, representing the canvas length, the upper limit of Tenniel’s painting operations, and the range of colors.

The second line contains an integer sequence cc of length nn, representing the initial color at each position on the canvas.

Output Format

For each test case, output one positive integer per line, representing the maximum possible number of memory fragments.

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

Hint

Sample explanation

For the first test case, Tenniel can repaint the second position from left to right to color 11, obtaining {cn}={2,1,2}\{c_n\}=\{2,1,2\}. The number of blocks is 33.

For the second test case, Tenniel can repaint the second position from left to right to color 11, and repaint the third position from left to right to color 33, obtaining {cn}={2,1,3,2,3}\{c_n\}=\{2,1,3,2,3\}. The number of blocks is 55.

Constraints

This problem uses bundled testdata.

Subtask\text{Subtask} n\sum n\le mm\le kk\le Score
11 1010 33 1010
22 5×1055\times 10^5 11 5×1055\times 10^5
33 10310^3 1515
44 5×1055\times 10^5 33 2525
55 5×1055\times 10^5 4040

For 100%100\% of the testdata, 1n5×1051\le n\le 5\times 10^5, 1n5×1051\le \sum n\le 5\times 10^5, 1mn1\le m\le n, 3k5×1053\le k \le 5\times 10^5, and 1cik1\le c_i\le k.

Translated by ChatGPT 5