#CF2237F. 粉刷数组 / F. Paint the Array
粉刷数组 / F. Paint the Array
F. Paint the Array
Source: Codeforces 2237F
Contest: Order Capital Round 2 (Codeforces Round 1104, Div. 1 + Div. 2)
Time limit: 2 seconds
Memory limit: 256 megabytes
Statement
Consider an array of length , and a fixed integer . A painting operation is defined as follows:
- Choose an interval of length and paint it with the values from left to right. Formally, choose an integer such that . Then, for every , position is painted with value .
If a position is painted multiple times, only the value painted most recently remains.
An array is called valid if it can be obtained by performing some painting operations such that every position is painted at least once.
Given an array with , find the minimum number of modifications needed to make it valid. One modification changes one element to any integer between and .
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains two integers and () — the length of the array and the length of each painted interval.
The second line contains integers ().
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output a single integer — the minimum number of modifications needed to make it valid.
Note
In the transformations below, the underlined positions are exactly the positions painted in the latest operation.
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 .
In the second test case, since and , every valid array must be obtained by painting both intervals and . For example, $$ [-,-,-,-]\to[-,\underline{1},\underline{2},\underline{3}]\to[\underline{1},\underline{2},\underline{3},3]. $$ This gives the valid array . The given array can be changed into it by modifying only the third element, so the answer is .
In the third test case, one closest valid array is , 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 differs from in two positions. It can be proven that one modification is not enough, so the answer is .
Examples
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
0
1
2
3
2
2
1
4
2
4
0
4
1
3
4