2059B.md (2437B)
1 Source: https://codeforces.com/problemset/problem/2059/B 2 3 B. Cost of the Array 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 You are given an array $a$ of length $n$ and an even integer $k$ ($2 \le k \le n$). You need to split the array $a$ into exactly $k$ non-empty subarrays$^{\dagger}$ such that each element of the array $a$ belongs to exactly one subarray. 22 23 Next, all subarrays with even indices (second, fourth, $\ldots$, $k$-th) are concatenated into a single array $b$. After that, $0$ is added to the end of the array $b$. 24 25 The cost of the array $b$ is defined as the minimum index $i$ such that $b_i \neq i$. For example, the cost of the array $b = [1, 2, 4, 5, 0]$ is $3$, since $b_1 = 1$, $b_2 = 2$, and $b_3 \neq 3$. Determine the minimum cost of the array $b$ that can be obtained with an optimal partitioning of the array $a$ into subarrays. 26 27 $^{\dagger}$An array $x$ is a subarray of an array $y$ if $x$ can be obtained from $y$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. 28 29 Input 30 31 Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows. 32 33 The first line of each test case contains two integers $n$ and $k$ ($2 \le k \le n \le 2 \cdot 10^5$, $k$ is even) — the length of the array $a$ and the number of subarrays. 34 35 The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of the array $a$. 36 37 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. 38 39 Output 40 41 For each test case, output a single integer — the minimum cost of the array $b$ that can be obtained. 42 43 Example 44 45 Input 46 47 ``` 48 4 49 50 3 2 51 52 1 1 1 53 54 8 8 55 56 1 1 2 2 3 3 4 4 57 58 5 4 59 60 1 1 1 2 2 61 62 5 4 63 64 1 1 1000000000 2 2 65 ``` 66 67 Output 68 69 ``` 70 2 71 5 72 2 73 1 74 ``` 75 76 Note 77 78 In the first test case, there are only two possible partitionings: $[[1], [1, 1]]$ and $[[1, 1], [1]]$. In either case, $b_1 = 1$, and $b_2 \ne 2$, so the cost is $2$. 79 80 In the second test case, there is only one possible partitioning, where $b = [1, 2, 3, 4, 0]$, so the cost is $5$ ($b_5 = 0 \ne 5$). 81 82 In the third test case, the following partitioning works: $[[1], [1, 1], [2], [2]]$. Then $b = [1, 1, 2, 0]$, and the cost is $2$.