CF2059B.Cost of the Array

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an array aa of length nn and an even integer kk (2≤k≤n2 \le k \le n). You need to split the array aa into exactly kk non-empty subarrays†^{\dagger} such that each element of the array aa belongs to exactly one subarray.

Next, all subarrays with even indices (second, fourth, …\ldots, kk-th) are concatenated into a single array bb. After that, 00 is added to the end of the array bb.

The cost of the array bb is defined as the minimum index ii such that bi≠ib_i \neq i. For example, the cost of the array b=[1,2,4,5,0]b = [1, 2, 4, 5, 0] is 33, since b1=1b_1 = 1, b2=2b_2 = 2, and b3≠3b_3 \neq 3. Determine the minimum cost of the array bb that can be obtained with an optimal partitioning of the array aa into subarrays.

†^{\dagger}An array xx is a subarray of an array yy if xx can be obtained from yy by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

给你一个长度为 nn 的数组 aa 和一个偶数 kk(2≤k≤n2 \le k \le n)。你需要将数组 aa 恰好划分为 kk 个非空子数组†^{\dagger},使得数组 aa 中的每个元素恰好属于其中一个子数组。

接下来,将所有下标为偶数的子数组(即第 22 个、第 44 个、……、第 kk 个子数组)按顺序拼接成一个新数组 bb。然后,在数组 bb 的末尾添加一个 00。

定义数组 bb 的代价为满足 bi≠ib_i \neq i 的最小下标 ii。例如,若 b=[1,2,4,5,0]b = [1, 2, 4, 5, 0],则其代价为 33,因为 b1=1b_1 = 1,b2=2b_2 = 2,而 b3≠3b_3 \neq 3。请确定:在对数组 aa 进行最优划分的前提下,所能得到的数组 bb 的最小可能代价。

†^{\dagger} 若数组 xx 可通过从数组 yy 的开头删除若干(可能为零或全部)元素、并从结尾删除若干(可能为零或全部)元素而得到,则称 xx 是 yy 的一个子数组。

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn and kk (2≤k≤n≤2⋅1052 \le k \le n \le 2 \cdot 10^5, kk is even) — the length of the array aa and the number of subarrays.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \le a_i \le 10^9) — the elements of the array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 kk(2≤k≤n≤2⋅1052 \le k \le n \le 2 \cdot 10^5,且 kk 为偶数),分别表示数组 aa 的长度以及子数组的数量。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1091 \le a_i \le 10^9),表示数组 aa 的元素。

保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output a single integer — the minimum cost of the array bb that can be obtained.

对于每个测试用例,输出一个整数——可得到的数组 bb 的最小代价。

输入输出样例

  • 输入#1

    4
    3 2
    1 1 1
    8 8
    1 1 2 2 3 3 4 4
    5 4
    1 1 1 2 2
    5 4
    1 1 1000000000 2 2

    输出#1

    2
    5
    2
    1

说明/提示

In the first test case, there are only two possible partitionings: [[1],[1,1]][[1], [1, 1]] and [[1,1],[1]][[1, 1], [1]]. In either case, b1=1b_1 = 1, and b2≠2b_2 \ne 2, so the cost is 22.

In the second test case, there is only one possible partitioning, where b=[1,2,3,4,0]b = [1, 2, 3, 4, 0], so the cost is 55 (b5=0≠5b_5 = 0 \ne 5).

In the third test case, the following partitioning works: [[1],[1,1],[2],[2]][[1], [1, 1], [2], [2]]. Then b=[1,1,2,0]b = [1, 1, 2, 0], and the cost is 22.

在第一个测试用例中,仅有两种可能的划分方式:[[1],[1,1]][[1], [1, 1]] 和 [[1,1],[1]][[1, 1], [1]]。在这两种情况下,均有 b1=1b_1 = 1,且 b2≠2b_2 \ne 2,因此代价为 22。

在第二个测试用例中,仅存在一种可能的划分方式,此时 b=[1,2,3,4,0]b = [1, 2, 3, 4, 0],故代价为 55(因为 b5=0≠5b_5 = 0 \ne 5)。

在第三个测试用例中,如下划分方式可行:[[1],[1,1],[2],[2]][[1], [1, 1], [2], [2]]。此时 b=[1,1,2,0]b = [1, 1, 2, 0],代价为 22。

输入解题思路,AI测评打分。不知道怎么写?

首页