D. Harder Problem
Given a sequence of positive integers, a positive integer is called a mode of the sequence if it occurs the maximum number of times that any positive integer occurs. For example, the mode of [ 2 , 2 , 3 ] [2,2,3] [2,2,3] is 2 2 2. Any of 9 9 9, 8 8 8, or 7 7 7 can be considered to be a mode of the sequence [ 9 , 9 , 8 , 8 , 7 , 7 ] [9,9,8,8,7,7] [9,9,8,8,7,7].
You gave UFO an array a a a of length n n n. To thank you, UFO decides to construct another array b b b of length n n n such that a i a_i ai is a mode of the sequence [ b 1 , b 2 , … , b i ] [b_1, b_2, \ldots, b_i] [b1,b2,…,bi] for all 1 ≤ i ≤ n 1 \leq i \leq n 1≤i≤n.
However, UFO doesn’t know how to construct array b b b, so you must help her. Note that 1 ≤ b i ≤ n 1 \leq b_i \leq n 1≤bi≤n must hold for your array for all 1 ≤ i ≤ n 1 \leq i \leq n 1≤i≤n.
Input
The first line contains t t t ( 1 ≤ t ≤ 1 0 4 1 \leq t \leq 10^4 1≤t≤104) — the number of test cases.
The first line of each test case contains an integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \leq n \leq 2 \cdot 10^5 1≤n≤2⋅105) — the length of a a a.
The following line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an ( 1 ≤ a i ≤ n 1 \leq a_i \leq n 1≤ai≤n).
It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2⋅105.
Output
For each test case, output n n n numbers b 1 , b 2 , … , b n b_1, b_2, \ldots, b_n b1,b2,…,bn ( 1 ≤ b i ≤ n 1 \leq b_i \leq n 1≤bi≤n) on a new line. It can be shown that b b b can always be constructed. If there are multiple possible arrays, you may print any.
Example
input
4
2
1 2
4
1 1 1 2
8
4 5 5 5 1 1 2 1
10
1 1 2 2 1 1 3 3 1 1
out
1 2
1 1 2 2
4 5 5 1 1 2 2 3
1 8 2 2 1 3 3 9 1 1
Note
Let’s verify the correctness for our sample output in test case 2 2 2.
- At i = 1 i = 1 i=1, 1 1 1 is the only possible mode of [ 1 ] [1] [1].
- At i = 2 i = 2 i=2, 1 1 1 is the only possible mode of [ 1 , 1 ] [1, 1] [1,1].
- At i = 3 i = 3 i=3, 1 1 1 is the only possible mode of [ 1 , 1 , 2 ] [1, 1, 2] [1,1,2].
- At i = 4 i = 4 i=4, 1 1 1 or 2 2 2 are both modes of [ 1 , 1 , 2 , 2 ] [1, 1, 2, 2] [1,1,2,2]. Since a i = 2 a_i = 2 ai=2, this array is valid.
题目大意:要构造出一个b数组,令其满足每个a[i] 在[b1,b2,b3,…,bi]中可以做模,那么得到的这个b数组则是正确的。
思路:我们有n个位置放数,那我们就按照123…n这种情况放数,这样每个数只出现一次,这样每一个数都可以作为模数,但是,要注意数出现的顺序,所以我们出现一个新的就输出当前这个数,然后遍历1~n看哪个数还没出现过,没出现的则输出。
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;const int N = 2e5+10;
int a[N];
int vis[N];void solve(){memset(vis,0,sizeof(vis));int n;scanf("%d", &n);for (int i = 1; i <= n; i++) {scanf("%d", &a[i]);if(vis[a[i]] == 0){vis[a[i]] = 1;printf("%d ", a[i]);}}for (int i = 1; i <= n; i++){if(vis[i] == 0){printf("%d ",i);}}return ;
}int main(){int t;scanf("%d", &t);while(t--){solve();}
}