B. Bessie and MEX
time limit per test: 1.5 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
⠀
Farmer John has a permutation p 1 , p 2 , … , p n p_1, p_2, \ldots, p_n p1,p2,…,pn, where every integer from 0 0 0 to n − 1 n-1 n−1 occurs exactly once. He gives Bessie an array a a a of length n n n and challenges her to construct p p p based on a a a.
The array a a a is constructed so that a i a_i ai = MEX ( p 1 , p 2 , … , p i ) − p i \texttt{MEX}(p_1, p_2, \ldots, p_i) - p_i MEX(p1,p2,…,pi)−pi, where the MEX \texttt{MEX} MEX of an array is the minimum non-negative integer that does not appear in that array. For example, MEX ( 1 , 2 , 3 ) = 0 \texttt{MEX}(1, 2, 3) = 0 MEX(1,2,3)=0 and MEX ( 3 , 1 , 0 ) = 2 \texttt{MEX}(3, 1, 0) = 2 MEX(3,1,0)=2.
Help Bessie construct any valid permutation
p
p
p that satisfies
a
a
a. The input is given in such a way that at least one valid
p
p
p exists. If there are multiple possible
p
p
p, it is enough to print one of them.
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 lengths of p p p and a a a.
The second 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 ( − n ≤ a i ≤ n -n \leq a_i \leq n −n≤ai≤n) — the elements of array a a a.
It is guaranteed that there is at least one valid p p p for the given data.
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 integers on a new line, the elements of p p p.
If there are multiple solutions, print any of them.
Example
inputCopy
3
5
1 1 -2 1 2
5
1 1 1 1 1
3
-2 1 2
outputCopy
0 1 4 2 3
0 1 2 3 4
2 0 1
Note
In the first case, p = [ 0 , 1 , 4 , 2 , 3 ] p = [0, 1, 4, 2, 3] p=[0,1,4,2,3] is one possible output.
a a a will then be calculated as a 1 = MEX ( 0 ) − 0 = 1 a_1 = \texttt{MEX}(0) - 0 = 1 a1=MEX(0)−0=1, a 2 = MEX ( 0 , 1 ) − 1 = 1 a_2 = \texttt{MEX}(0, 1) - 1 = 1 a2=MEX(0,1)−1=1, a 3 = MEX ( 0 , 1 , 4 ) − 4 = − 2 a_3 = \texttt{MEX}(0, 1, 4) - 4 = -2 a3=MEX(0,1,4)−4=−2, a 4 = MEX ( 0 , 1 , 4 , 2 ) − 2 = 1 a_4 = \texttt{MEX}(0, 1, 4, 2) - 2 = 1 a4=MEX(0,1,4,2)−2=1, a 5 = MEX ( 0 , 1 , 4 , 2 , 3 ) − 3 = 2 a_5 = \texttt{MEX}(0, 1, 4, 2, 3) - 3 = 2 a5=MEX(0,1,4,2,3)−3=2.
So, as required, a a a will be [ 1 , 1 , − 2 , 1 , 2 ] [1, 1, -2, 1, 2] [1,1,−2,1,2].
AC代码:
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<numeric>
#include<iomanip>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
const int N=3e5+10;
const int MOD=1e9 + 7;
const int INF=0X3F3F3F3F;
const int dx[]={-1,1,0,0,-1,-1,+1,+1};
const int dy[]={0,0,-1,1,-1,+1,-1,+1};
const int M = 1e6 + 10;
int t;
int n;
int h[N];
int a[N];
int main()
{
cin >> t;
while(t --){
cin >> n;
memset(h, 0, sizeof h);
int flag = n;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
}
for(int i = n; i >= 1; i --)
{
int x = flag - a[i];
if(flag > x) flag = x;
h[i] = x;
}
for(int i = 1; i <= n; i ++)
{
cout << h[i] << " ";
}
cout << endl;
}
return 0;
}