CF BUDDY
← Problems·

1659D · Reverse Sort Sum

1900 · constructive algorithms, data structures, greedy

Problem: Suppose you had an array AA of nn elements, each of which is 00 or 11.

Let us define a function f(k,A)f(k,A) which returns another array BB, the result of sorting the first kk elements of AA in non-decreasing order. For example, f(4,[0,1,1,0,0,1,0])=[0,0,1,1,0,1,0]f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]. Note that the first 44 elements were sorted.

Now consider the arrays B1,B2,,BnB_1, B_2,\ldots, B_n generated by f(1,A),f(2,A),,f(n,A)f(1,A), f(2,A),\ldots,f(n,A). Let CC be the array obtained by taking the element-wise sum of B1,B2,,BnB_1, B_2,\ldots, B_n.

For example, let A=[0,1,0,1]A=[0,1,0,1]. Then we have B1=[0,1,0,1]B_1=[0,1,0,1], B2=[0,1,0,1]B_2=[0,1,0,1], B3=[0,0,1,1]B_3=[0,0,1,1], B4=[0,0,1,1]B_4=[0,0,1,1]. Then C=B1+B2+B3+B4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4]C=B_1+B_2+B_3+B_4=[0,1,0,1]+[0,1,0,1]+[0,0,1,1]+[0,0,1,1]=[0,2,2,4].

You are given CC. Determine a binary array AA that would give CC when processed as above. It is guaranteed that an array AA exists for given CC in the input.

Input Format: The first line contains a single integer tt (1t10001 \leq t \leq 1000)  — the number of test cases.

Each test case has two lines. The first line contains a single integer nn (1n21051 \leq n \leq 2 \cdot 10^5).

The second line contains nn integers c1,c2,,cnc_1, c_2, \ldots, c_n (0cin0 \leq c_i \leq n). It is guaranteed that a valid array AA exists for the given CC.

The sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output Format: For each test case, output a single line containing nn integers a1,a2,,ana_1, a_2, \ldots, a_n (aia_i is 00 or 11). If there are multiple answers, you may output any of them.

Note: Here's the explanation for the first test case. Given that A=[1,1,0,1]A=[1,1,0,1], we can construct each BiB_i:

  • B1=[1,1,0,1]B_1=[\color{blue}{1},1,0,1];
  • B2=[1,1,0,1]B_2=[\color{blue}{1},\color{blue}{1},0,1];
  • B3=[0,1,1,1]B_3=[\color{blue}{0},\color{blue}{1},\color{blue}{1},1];
  • B4=[0,1,1,1]B_4=[\color{blue}{0},\color{blue}{1},\color{blue}{1},\color{blue}{1}]

Sample Cases

Case 1

Input

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

Output

1 1 0 1 
0 1 1 0 0 0 1 
0 0 0 
0 0 0 1 
1 0 1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback