QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 256 MB 總分: 100 可 Hack ✓

#6680. 堆

统计

DreamGrid 正在数据结构课程中学习堆的插入操作。 在接下来的描述中,我们记 $i/2$ 为满足 $2x \le i$ 的最大整数 $x$。回想一下:

  • 大小为 $n$ 的堆是一个数组 $a_1, a_2, \dots, a_n$,满足以下两个条件之一:
    • 对于所有 $2 \le i \le n$,$a_{i/2} \le a_i$。这被称为最小堆。
    • 对于所有 $2 \le i \le n$,$a_{i/2} \ge a_i$。这被称为最大堆。
  • 插入操作可以通过以下伪代码描述:
procedure insert(
    v: value to insert,
    a: heap array,
    is_max: if a is a max heap)
{Let n be the length of the heap array after insertion}
an := v
i := n
while i > 1
    if is_max is false and ai/2 <= ai
        {The heap array now satisfies the condition to be a min heap}
        break
    else if is_max is true and ai/2 >= ai
        {The heap array now satisfies the condition to be a max heap}
        break
    swap ai/2 and ai
    i := i/2
{Insertion ends}

DreamGrid 准备了一个初始为空的数组 $a$ 作为堆,以及 $n$ 个整数 $v_1, v_2, \dots, v_n$。他正准备按顺序将这 $n$ 个整数插入堆中,这时他的手机响了,于是他把这项工作留给了他的室友 BaoBao。

不幸的是,BaoBao 不理解插入函数中参数 is_max 的含义(但对于我们亲爱的参赛者,我们希望你们已经理解了这个参数的含义),所以他生成了一个长度为 $n$ 的二进制字符串(只包含 '0' 和 '1' 的字符串)$b = b_1b_2 \dots b_n$,其中 $b_i$ 表示字符串中的第 $i$ 个字符,并根据该字符串决定 is_max 的值。当插入 $v_i$ 到 $a$ 时,如果 $b_i$ 等于 '0',则此次插入时的 is_maxfalse;否则如果 $b_i$ 等于 '1',则此次插入时的 is_maxtrue

当 DreamGrid 回来时,他沮丧地发现最终的“堆”数组 $a_1, a_2, \dots, a_n$ 看起来并不是一个有效的堆!给定 $n$ 个插入的整数 $v_1, v_2, \dots, v_n$ 以及最终的数组,且已知 BaoBao 是按顺序插入 $v_1, v_2, \dots, v_n$ 的,请帮助 DreamGrid 还原 BaoBao 生成的二进制字符串 $b$。

输入格式

输入包含多组测试数据。第一行包含一个整数 $T$,表示测试数据的组数。对于每组测试数据:

第一行包含一个整数 $n$ ($1 \le n \le 10^5$),表示最终数组的大小。

第二行包含 $n$ 个整数 $v_1, v_2, \dots, v_n$ ($1 \le v_i \le 10^9$),表示插入的顺序。

第三行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$,它是 $v_1, v_2, \dots, v_n$ 的一个排列,表示最终的“堆”数组。

保证所有测试数据的 $n$ 之和不超过 $10^6$。

输出格式

对于每组测试数据,输出一行包含一个二进制字符串,表示 BaoBao 生成的用于插入整数的字符串。如果存在多个有效的答案,输出字典序最小的那一个。如果二进制字符串不存在,则输出 “Impossible”(不含引号)。

回想一下,对于两个长度为 $n$ 的二进制字符串 $s$ 和 $t$,如果存在一个整数 $k$ 满足以下所有约束,则称 $s$ 的字典序小于 $t$:

  • $1 \le k \le n$。
  • 对于所有 $1 \le i < k$,$s_i = t_i$。
  • $s_k = \text{'0'}$ 且 $t_k = \text{'1'}$。

样例

样例输入 1

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

样例输出 1

0101
Impossible
001

说明

我们现在解释第一个样例测试数据。

$i$ $v_i$ $b_i$ “Heap” Array after Insertion
1 2 0 {2}
2 3 1 {3, 2}
3 1 0 {1, 2, 3}
4 4 1 {4, 1, 3, 2}

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.