CF1375D.Replace by MEX

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You're given an array of nn integers between 00 and nn inclusive.

In one operation, you can choose any element of the array and replace it by the MEX of the elements of the array (which may change after the operation).

For example, if the current array is [0,2,2,1,4][0, 2, 2, 1, 4], you can choose the second element and replace it by the MEX of the present elements — 33. Array will become [0,3,2,1,4][0, 3, 2, 1, 4].

You must make the array non-decreasing, using at most 2n2n operations.

It can be proven that it is always possible. Please note that you do not have to minimize the number of operations. If there are many solutions, you can print any of them.

–

An array b[1…n]b[1 \ldots n] is non-decreasing if and only if b1≤b2≤…≤bnb_1 \le b_2 \le \ldots \le b_n.

The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance:

  • The MEX of [2,2,1][2, 2, 1] is 00, because 00 does not belong to the array.
  • The MEX of [3,1,0,1][3, 1, 0, 1] is 22, because 00 and 11 belong to the array, but 22 does not.
  • The MEX of [0,3,1,2][0, 3, 1, 2] is 44 because 00, 11, 22 and 33 belong to the array, but 44 does not.

It's worth mentioning that the MEX of an array of length nn is always between 00 and nn inclusive.

给你一个包含 nn 个整数的数组,每个整数均在 00 到 nn(含)之间。

在一次操作中,你可以任选数组中的一个元素,并将其替换为当前数组所有元素的 MEX(该值可能在操作后发生变化)。

例如,若当前数组为 [0,2,2,1,4][0, 2, 2, 1, 4],你可以选择第二个元素,并将其替换为当前数组元素的 MEX —— 即 33。数组将变为 [0,3,2,1,4][0, 3, 2, 1, 4]。

你必须在至多 2n2n 次操作内,使数组变为非递减序列。

可以证明,这总是可行的。请注意:你无需最小化操作次数。若存在多种解法,输出任意一种即可。

—

数组 b[1…n]b[1 \ldots n] 是非递减的,当且仅当 b1≤b2≤…≤bnb_1 \le b_2 \le \ldots \le b_n。

一个数组的 MEX(minimum excluded value,最小未出现非负整数)是指不属于该数组的最小非负整数。例如:

  • [2,2,1][2, 2, 1] 的 MEX 是 00,因为 00 不在数组中;
  • [3,1,0,1][3, 1, 0, 1] 的 MEX 是 22,因为 00 和 11 在数组中,但 22 不在;
  • [0,3,1,2][0, 3, 1, 2] 的 MEX 是 44,因为 00、11、22 和 33 都在数组中,但 44 不在。

值得一提的是,长度为 nn 的数组的 MEX 值总是在 00 到 nn(含)之间。

输入格式

The first line contains a single integer tt (1≤t≤2001 \le t \le 200) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (3≤n≤10003 \le n \le 1000) — length of the array.

The second line of each test case contains nn integers a1,…,ana_1, \ldots, a_n (0≤ai≤n0 \le a_i \le n) — elements of the array. Note that they don't have to be distinct.

It is guaranteed that the sum of nn over all test cases doesn't exceed 10001000.

第一行包含一个整数 tt(1≤t≤2001 \le t \le 200)—— 表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(3≤n≤10003 \le n \le 1000)—— 表示数组的长度。

每个测试用例的第二行包含 nn 个整数 a1,…,ana_1, \ldots, a_n(0≤ai≤n0 \le a_i \le n)—— 表示数组的元素。注意:这些元素不一定互不相同。

保证所有测试用例的 nn 值之和不超过 10001000。

输出格式

For each test case, you must output two lines:

The first line must contain a single integer kk (0≤k≤2n0 \le k \le 2n) — the number of operations you perform.

The second line must contain kk integers x1,…,xkx_1, \ldots, x_k (1≤xi≤n1 \le x_i \le n), where xix_i is the index chosen for the ii-th operation.

If there are many solutions, you can find any of them. Please remember that it is not required to minimize kk.

对于每个测试用例,你必须输出两行:

第一行必须包含一个整数 kk(0≤k≤2n0 \le k \le 2n)—— 即你执行的操作次数。

第二行必须包含 kk 个整数 x1,…,xkx_1, \ldots, x_k(1≤xi≤n1 \le x_i \le n),其中 xix_i 表示第 ii 次操作所选择的下标。

若存在多种解法,你可以输出任意一种。请注意,不要求最小化 kk。

输入输出样例

  • 输入#1

    5
    3
    2 2 3
    3
    2 1 0
    7
    0 7 3 1 3 7 7
    9
    2 0 1 1 2 4 4 2 0
    9
    8 4 7 6 1 2 3 0 5

    输出#1

    0
    
    2
    3 1
    4
    2 5 5 4
    11
    3 8 9 7 8 5 9 6 4 1 2
    10
    1 8 1 9 5 2 4 6 3 7

说明/提示

In the first test case, the array is already non-decreasing (2≤2≤32 \le 2 \le 3).

Explanation of the second test case (the element modified by each operation is colored in red):

  • a=[2,1,0]a = [2, 1, 0] ; the initial MEX is 33.
  • a=[2,1,3]a = [2, 1, \color{red}{3}] ; the new MEX is 00.
  • a=[0,1,3]a = [\color{red}{0}, 1, 3] ; the new MEX is 22.
  • The final array is non-decreasing: 0≤1≤30 \le 1 \le 3.

Explanation of the third test case:

  • a=[0,7,3,1,3,7,7]a = [0, 7, 3, 1, 3, 7, 7] ; the initial MEX is 22.
  • a=[0,2,3,1,3,7,7]a = [0, \color{red}{2}, 3, 1, 3, 7, 7] ; the new MEX is 44.
  • a=[0,2,3,1,4,7,7]a = [0, 2, 3, 1, \color{red}{4}, 7, 7] ; the new MEX is 55.
  • a=[0,2,3,1,5,7,7]a = [0, 2, 3, 1, \color{red}{5}, 7, 7] ; the new MEX is 44.
  • a=[0,2,3,4,5,7,7]a = [0, 2, 3, \color{red}{4}, 5, 7, 7] ; the new MEX is 11.
  • The final array is non-decreasing: 0≤2≤3≤4≤5≤7≤70 \le 2 \le 3 \le 4 \le 5 \le 7 \le 7.

在第一个测试用例中,数组已经是非递减的(2≤2≤32 \le 2 \le 3)。

第二个测试用例的说明(每次操作所修改的元素以红色标出):

  • a=[2,1,0]a = [2, 1, 0];初始 MEX 为 33。
  • a=[2,1,3]a = [2, 1, \color{red}{3}];新的 MEX 为 00。
  • a=[0,1,3]a = [\color{red}{0}, 1, 3];新的 MEX 为 22。
  • 最终数组是非递减的:0≤1≤30 \le 1 \le 3。

第三个测试用例的说明:

  • a=[0,7,3,1,3,7,7]a = [0, 7, 3, 1, 3, 7, 7];初始 MEX 为 22。
  • a=[0,2,3,1,3,7,7]a = [0, \color{red}{2}, 3, 1, 3, 7, 7];新的 MEX 为 44。
  • a=[0,2,3,1,4,7,7]a = [0, 2, 3, 1, \color{red}{4}, 7, 7];新的 MEX 为 55。
  • a=[0,2,3,1,5,7,7]a = [0, 2, 3, 1, \color{red}{5}, 7, 7];新的 MEX 为 44。
  • a=[0,2,3,4,5,7,7]a = [0, 2, 3, \color{red}{4}, 5, 7, 7];新的 MEX 为 11。
  • 最终数组是非递减的:0≤2≤3≤4≤5≤7≤70 \le 2 \le 3 \le 4 \le 5 \le 7 \le 7。

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

首页