Order Capital Round 2 (Codeforces Round 1104, Div. 1 + Div. 2)
F
给数组涂色
Paint the Array
Codeforces
2400
原题
← EG →

给数组涂色AI (CI翻译)

Consider an array of length $n$, and a fixed integer $m$. A painting operation is defined as follows:

考虑一个长度为 $n$ 的数组,以及一个固定整数 $m$。一次涂色操作定义如下:AI (CI翻译)

  • Choose an interval of length $m$ and paint it with the values $1,2,\ldots,m$ from left to right.Formally, choose an integer $l$ such that $1\le l\le n-m+1$. Then, for every $1\le i\le m$, position $l+i-1$ is painted with value $i$.
  • 选择一个长度为 $m$ 的区间,并从左到右将其涂成 $1,2,\ldots,m$。形式化地,选择一个整数 $l$,满足 $1\le l\le n-m+1$。然后,对于所有 $1\le i\le m$,将位置 $l+i-1$ 涂成数值 $i$。
AI (CI翻译)

If a position is painted multiple times, only the value painted most recently remains.

如果某个位置被涂色多次,则只保留最后一次涂上的数值。AI (CI翻译)

An array is called valid if it can be obtained by performing some painting operations such that every position is painted at least once.

如果一个数组可以通过执行若干次涂色操作得到,并且每个位置都至少被涂色一次,则称该数组是合法的。AI (CI翻译)

Given an array $a_1,a_2,\ldots,a_n$ with $1\le a_i\le m$, find the minimum number of modifications needed to make it valid. One modification changes one element to any integer between $1$ and $m$.

给定一个数组 $a_1,a_2,\ldots,a_n$,其中 $1\le a_i\le m$,求将其变为合法的最少修改次数。一次修改可以将一个元素改为 $1$ 到 $m$ 之间的任意整数。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

每个测试包含多组测试用例。第一行包含测试用例数量 $t$($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)

The first line of each test case contains two integers $n$ and $m$ ($1\le m\le n\le 5\cdot 10^5$) — the length of the array and the length of each painted interval.

每组测试用例的第一行包含两个整数 $n$ 和 $m$($1\le m\le n\le 5\cdot 10^5$),分别表示数组长度和每个涂色区间的长度。AI (CI翻译)

The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le m$).

第二行包含 $n$ 个整数 $a_1,a_2,\ldots,a_n$($1\le a_i\le m$)。AI (CI翻译)

It is guaranteed that the sum of $n$ over all test cases does not exceed $5\cdot 10^5$.

保证所有测试用例的 $n$ 之和不超过 $5\cdot 10^5$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, output a single integer — the minimum number of modifications needed to make it valid.

对于每组测试用例,输出一个整数,表示将数组变为合法的所需的最少修改次数。AI (CI翻译)

Note

说明

AI (CI翻译)

In the transformations below, the underlined positions are exactly the positions painted in the latest operation.

在下面的变换过程中,带下划线的位置恰好是最后一次操作所涂色的位置。AI (CI翻译)

In the first test case, the array is already valid. It can be obtained by the following painting operations: $$ [-,-,-,-,-]\to[-,-,\underline{1},\underline{2},\underline{3}]\to[\underline{1},\underline{2},\underline{3},2,3]. $$ Therefore no modification is needed, and the answer is $0$.

在第一个测试用例中,数组已经是合法的。它可以通过以下涂色操作得到: $$ [-,-,-,-,-]\to[-,-,\underline{1},\underline{2},\underline{3}]\to[\underline{1},\underline{2},\underline{3},2,3]. $$ 因此不需要进行修改,答案为 $0$。AI (CI翻译)

In the second test case, since $n=4$ and $m=3$, every valid array must be obtained by painting both intervals $[1,3]$ and $[2,4]$. For example, $$ [-,-,-,-]\to[-,\underline{1},\underline{2},\underline{3}]\to[\underline{1},\underline{2},\underline{3},3]. $$ This gives the valid array $[1,2,3,3]$. The given array $[1,2,2,3]$ can be changed into it by modifying only the third element, so the answer is $1$.

在第二个测试用例中,由于 $n=4$ 且 $m=3$,每个合法的数组都必须通过对区间 $[1,3]$ 和 $[2,4]$ 都进行涂色得到。例如: $$ [-,-,-,-]\to[-,\underline{1},\underline{2},\underline{3}]\to[\underline{1},\underline{2},\underline{3},3]. $$ 这样得到的合法数组为 $[1,2,3,3]$。给定数组 $[1,2,2,3]$ 只需修改第三个元素即可变成该数组,因此答案为 $1$。AI (CI翻译)

In the third test case, one closest valid array is $[1,1,2,3,3]$, which can be obtained as follows: $$ [-,-,-,-,-]\to[\underline{1},\underline{2},\underline{3},-,-]\to[1,2,\underline{1},\underline{2},\underline{3}]\to[1,\underline{1},\underline{2},\underline{3},3]. $$ The given array $[2,1,2,3,2]$ differs from $[1,1,2,3,3]$ in two positions. It can be proven that one modification is not enough, so the answer is $2$.

在第三个测试用例中,一个距离最近的合法数组是 $[1,1,2,3,3]$,可以通过以下方式得到: $$ [-,-,-,-,-]\to[\underline{1},\underline{2},\underline{3},-,-]\to[1,2,\underline{1},\underline{2},\underline{3}]\to[1,\underline{1},\underline{2},\underline{3},3]. $$ 给定数组 $[2,1,2,3,2]$ 与 $[1,1,2,3,3]$ 有两个位置不同。可以证明,一次修改是不够的,因此答案为 $2$。AI (CI翻译)

样例

Input #1
15
5 3
1 2 3 2 3
4 3
1 2 2 3
5 3
2 1 2 3 2
5 3
2 2 2 2 2
5 4
1 1 3 4 1
6 3
1 1 1 2 1 1
8 5
1 2 1 2 3 4 5 1
5 3
2 3 1 1 2
8 4
1 2 3 2 3 2 3 4
4 4
4 3 2 1
5 1
1 1 1 1 1
7 3
3 3 3 2 1 1 1
10 3
1 2 3 1 2 2 3 1 2 3
7 3
1 3 2 3 2 1 2
10 4
1 4 3 3 2 3 4 4 2 2
Output #1
0
1
2
3
2
2
1
4
2
4
0
4
1
3
4
InputCopy OutputCopy
题目信息
题号
2237F
时间限制
2 s
内存限制
256 MB
标签
数据结构
动态规划
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。