#P14048. [SDCPC 2019] Heap
[SDCPC 2019] Heap
题目描述
DreamGrid is learning the insertion operation of a heap in the data structure course.
In the following description, we denote to be the maximum integer that . Recall that
- A heap of size is an array which satisfies one of the following two conditions:
- For all , . This is called a min heap.
- For all , . This is called a max heap.
- The insertion operation can be described by the following pseudo-code:
:::align{center}
:::
DreamGrid has prepared an initially empty array as the heap array and integers . He is just about to insert these integers into the heap array in order when his cellphone rings, so he leaves this work to his roommate BaoBao.
Unfortunately, BaoBao doesn't understand what the argument means in the insertion function (but for our dear contestants, we hope that you've understood the meaning of this argument), so he generates a binary string (a string which only contains 0
and 1
) of length , where indicates the -th character in the string, and decides the value of according to the string. When inserting into , if equals to 0
, then will be false; otherwise if equals to 1
, then will be true.
When DreamGrid comes back, he finds with dismay that the final heap
array does not seem to be a valid heap! Given the inserted integers , the final array and given that BaoBao has inserted in order, please help DreamGrid restore the binary string BaoBao generates.
输入格式
There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:
The first line contains an integer (), indicating the size of the final array.
The second line contains integers (), indicating the integers in the order they are inserted.
The third line contains integers , which is a permutation of , indicating the final heap
array.
It's guaranteed that the sum of of all test cases will not exceed .
输出格式
For each test case output one line containing one binary string, indicating the string BaoBao generates for inserting the integers. If there are multiple valid answers, output the one with the smallest lexicographic order. If the binary string does not exist, output Impossible
(without quotes) instead.
Recall that, for two binary strings and of length , we say is lexicographically smaller than , if there exists an integer satisfying all the following constraints:
- .
- For all , .
- and .
3
4
2 3 1 4
4 1 3 2
5
4 5 1 2 3
3 4 1 5 2
3
1 1 2
2 1 1
0101
Impossible
001
提示
We now explain the first sample test case.
$$\begin{array}{|c|c|c|c|} \hline \textbf{$i$} & \textbf{$v_i$} & \textbf{$b_i$} & \textbf{``Heap'' Array after Insertion} \\ \hline 1 & 2 & 0 & \{2\} \\ \hline 2 & 3 & 1 & \{3, 2\} \\ \hline 3 & 1 & 0 & \{1, 2, 3\} \\ \hline 4 & 4 & 1 & \{4, 1, 3, 2\} \\ \hline \end{array}$$