CF1628A.Meximum Array
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Mihai has just learned about the MEX concept and since he liked it so much, he decided to use it right away.
Given an array a of n non-negative integers, Mihai wants to create a new array b that is formed in the following way:
While a is not empty:
- Choose an integer k (1≤k≤∣a∣).
- Append the MEX of the first k numbers of the array a to the end of array b and erase them from the array a, shifting the positions of the remaining numbers in a.
But, since Mihai loves big arrays as much as the MEX concept, he wants the new array b to be the lexicographically maximum. So, Mihai asks you to tell him what the maximum array b that can be created by constructing the array optimally is.
An array x is lexicographically greater than an array y if in the first position where x and y differ xi>yi or if ∣x∣>∣y∣ and y is a prefix of x (where ∣x∣ denotes the size of the array x).
The MEX of a set of non-negative integers is the minimal non-negative integer such that it is not in the set. For example, MEX({1,2,3}) =0 and MEX({0,1,2,4,5}) =3.
米海刚刚学习了MEX的概念,由于他非常喜欢这个概念,决定立刻加以应用。
给定一个长度为 n 的非负整数数组 a,米海希望构造一个新数组 b,其构造方式如下:
当数组 a 非空时,重复执行以下操作:
- 选择一个整数 k(满足 1≤k≤∣a∣);
- 将数组 a 的前 k 个数的 MEX 追加到数组 b 的末尾,并将这 k 个数从 a 中删除(a 中剩余元素的位置相应前移)。
然而,由于米海既热爱 MEX 概念,也热爱“大”数组,他希望新数组 b 是字典序最大的。因此,米海请你告诉他:通过最优地构造数组,所能得到的字典序最大的数组 b 是什么?
数组 x 字典序大于数组 y,当且仅当:在 x 和 y 第一次出现不同值的位置 i 上,满足 xi>yi;或者 ∣x∣>∣y∣ 且 y 是 x 的前缀(其中 ∣x∣ 表示数组 x 的长度)。
一个非负整数集合的 MEX(最小不存在值)是指不在该集合中的最小非负整数。例如,MEX({1,2,3}) =0,而 MEX({0,1,2,4,5}) =3。
输入格式
The first line of the input contains a single integer t (1≤t≤100) — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer n (1≤n≤2⋅105) — the number of elements in the array a.
The second line of each test case contains n non-negative integers a1,…,an (0≤ai≤n), where ai is the i-th integer from the array a.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
输入的第一行包含一个整数 t(1≤t≤100),表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105),表示数组 a 中的元素个数。
每个测试用例的第二行包含 n 个非负整数 a1,…,an(0≤ai≤n),其中 ai 是数组 a 中的第 i 个整数。
保证所有测试用例的 n 之和不超过 2⋅105。
输出格式
For each test case print m — the length of the maximum array b Mihai can create, followed by m integers denoting the elements of the array b.
对于每个测试用例,输出 m —— Mihai 能够构造的最长数组 b 的长度,然后输出 m 个整数,表示数组 b 的元素。
输入输出样例
输入#1
6 5 1 0 2 0 3 8 2 2 3 4 0 1 2 0 1 1 5 0 1 2 3 4 4 0 1 1 0 10 0 0 2 1 1 1 0 0 1 1
输出#1
1 4 2 5 1 1 0 1 5 2 2 2 4 3 2 2 0
说明/提示
In the first test case, the lexicographically maximum array b is obtained by selecting k=5, resulting in the MEX of the whole array a. It is lexicographically maximum because an array starting with a smaller number than 4 is lexicographically smaller, and choosing a k<5 would result in an array starting with a number smaller than 4.
In the second test case, there are two ways to obtain the maximum array: first selecting k=6, then k=2, or first selecting k=7 and then k=1.
在第一个测试用例中,字典序最大的数组 b 是通过选择 k=5 得到的,此时得到的是整个数组 a 的 MEX。该数组是字典序最大的,因为以比 4 更小的数开头的数组字典序更小,而若选择 k<5,则所得数组将以小于 4 的数开头。
在第二个测试用例中,存在两种方式可得到字典序最大的数组:先选择 k=6、再选择 k=2;或先选择 k=7、再选择 k=1。
输入解题思路,AI测评打分。不知道怎么写?