Meximum Array

处理数组剩余有用数字(有重复数字出现)。寻找连续数的最大值

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 aa of nn non-negative integers, Mihai wants to create a new array bb that is formed in the following way:

While aa is not empty:

  • Choose an integer kk (1≤k≤|a|1≤k≤|a|).
  • Append the MEX of the first kk numbers of the array aa to the end of array bb and erase them from the array aa, shifting the positions of the remaining numbers in aa.

But, since Mihai loves big arrays as much as the MEX concept, he wants the new array bb to be the lexicographically maximum. So, Mihai asks you to tell him what the maximum array bb that can be created by constructing the array optimally is.

An array xx is lexicographically greater than an array yy if in the first position where xx and yy differ xi>yixi>yi or if |x|>|y||x|>|y| and yy is a prefix of xx (where |x||x| denotes the size of the array xx).

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,31,2,3}) =0=0 and MEX({0,1,2,4,50,1,2,4,5}) =3=3.

Input

The first line of the input contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of elements in the array aa.

The second line of each test case contains nn non-negative integers a1,…,ana1,…,an (0≤ai≤n0≤ai≤n), where aiai is the ii-th integer from the array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case print mm — the length of the maximum array bb Mihai can create, followed by mm integers denoting the elements of the array bb.

Example

input

Copy

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

output

Copy

1
4 
2
5 1 
1
0 
1
5 
2
2 2 
4
3 2 2 0 

Note

In the first test case, the lexicographically maximum array bb is obtained by selecting k=5k=5, resulting in the MEXMEX of the whole array aa. It is lexicographically maximum because an array starting with a smaller number than 44 is lexicographically smaller, and choosing a k<5k<5 would result in an array starting with a number smaller than 44.

In the second test case, there are two ways to obtain the maximum array: first selecting k=6k=6, then k=2k=2, or first selecting k=7k=7 and then k=1k=1.

思路:先找到数组中最大值(是连续的,从0开始找的)加一,将答案存起来,去掉之前用过的数,再从剩余的数中找到最大的,加一。重复处理即可。

找连续最大值:(与*max_element()不同是,*max_element()只会找最大值,但不连续。ps:它们实际用起来的时间复杂度会有可能小于O(n))

k=0;//是从0开始连续的
while(st[k]) k++;

处理数组剩余数“字”(出现有相同数):数组中可能存在相同的数,而有部分用过,有部分没有用过。我们只需要记录该数在整个数组中出现过的次数,记录用掉的次数:总记录-用过次数=剩余没用的。将总记录更新,也就是更新成剩余的没用的,将用过的次数清零,又能开启一次新的循环。(k-1是最大的数)我们不需要将每个数都设为真假,因为我们不知道循环到何时停止。我们只需要“哈希”记录次数即可,不需要“哈希”真假,因为有重复数。“哈希”真假是剩余重复的数无用;“哈希”次数是处理剩余的数有用“哈希”次数需要记录剩余出现的总次数,与用过的次数

“哈希”次数模板:

for(int i=0; i<=k-1; i++)
    st[i]-=s[i],s[i]=0;

完整代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cstdio>

using namespace std;

#define sf(x) scanf("%d",&x)

const int N=2e5+10;
int st[N];
int a[N];
int s[N];

int main()
{
    int t;
    sf(t);
    while(t--)
    {
        int n;
        sf(n);
        for(int i=0; i<=n; i++) st[i]=0,s[i]=0;
        for(int i=1; i<=n; i++) sf(a[i]),st[a[i]]++;
        vector<int>v;
        int k=0;
        while(st[k])
            k++;
        int sum=0;
        for(int i=1; i<=n; i++)
        {
            if(a[i]<=k-1)
                if(!s[a[i]])
                {
                    s[a[i]]++;
                    sum++;
                }
                else
                    s[a[i]]++;
            if(sum==k)
            {
                v.push_back(k);
                for(int i=0; i<=k-1; i++)
                    st[i]-=s[i],s[i]=0;
                k=0;
                while(st[k])
                    k++;
                sum=0;
            }
        }
        printf("%d\n",v.size());
        for(auto x:v)
        {
            printf("%d ",x);
        }printf("\n");
    }
    return 0;
}

版权声明:本文为博主睡不饱的老笨蛋了原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/qq_51690312/article/details/122648972

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
xiaoxingxing的头像xiaoxingxing管理团队
上一篇 2022年1月23日 下午12:21
下一篇 2022年1月23日 下午1:13

相关推荐