QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 1024 MB 満点: 100 ハック可能 ✓

#15443. 循环移位 II

統計

给你两个长度分别为 $2n$ 和 $2m$ 的整数序列 $A$ 和 $B$。所有元素均为非零整数。

序列 $A = (a_1, a_2, \dots, a_{2n})$ 满足以下条件:对于每个满足 $1 \le i \le n$ 的整数 $i$,恰好有两个下标 $j$ 满足 $|a_j| = i$。特别地,$\{|a_j| : 1 \le j \le 2n\} = \{1, 2, \dots, n\}$,且如果不考虑符号,每个绝对值恰好出现两次。

序列 $B = (b_1, b_2, \dots, b_{2m})$ 满足类似的条件:对于每个满足 $1 \le i \le m$ 的整数 $i$,恰好有两个下标 $j$ 满足 $|b_j| = i$。

你可以对序列 $A$ 进行以下操作,操作可以以任意顺序执行任意多次:

  1. 循环移位(Cyclic shift):将当前序列视为一个环,并进行旋转: $$(a_1, a_2, \dots, a_{2k}) \longrightarrow (a_{r+1}, a_{r+2}, \dots, a_{2k}, a_1, \dots, a_r)$$ 其中 $0 \le r < 2k$。
  2. 删除前缀中的一对相反数:如果当前序列的形式为 $A = (x, -x, S_1)$,其中 $x \neq 0$ 且 $S_1$ 可以为空,你可以将 $A$ 替换为 $S_1$;即删除前两个元素 $x$ 和 $-x$。
  3. 在最前面插入一对新的相反数:如果当前序列为 $A = (S_1)$(可以为空),你可以将其替换为 $(x, -x, S_1)$,其中 $x \neq 0$,且 $x$ 和 $-x$ 均未在 $S_1$ 中出现过。
  4. 翻转并取反:如果当前序列为 $$A = (a_1, a_2, \dots, a_{2k})$$ 你可以将其替换为 $$\text{nRev}(A) = (-a_{2k}, -a_{2k-1}, \dots, -a_1)$$
  5. 跨切口重排:选择一个切口,将当前序列分割为一个前缀和一个后缀。假设序列可以写为 $$A = (S_1, x, S_2, S_3, y, S_4)$$ 其中 $x > 0$,$y \in {x, -x}$,且切口位于 $S_2$ 和 $S_3$ 之间。这里 $S_1, S_2, S_3, S_4$ 可以为空序列。然后你可以应用以下变换之一:

    • 如果 $y = x$(即你选择了一对 $x$ 和 $x$),你可以将 $A$ 替换为 $$(S_1, \text{nRev}(S_3), x, \text{nRev}(S_4), S_2, y)$$
    • 如果 $y = -x$(即你选择了一对 $x$ 和 $-x$),你可以将 $A$ 替换为 $$(S_1, S_4, x, S_3, S_2, y)$$

你需要判断是否可以通过应用有限次这些操作,将序列 $A$ 转换为一个与序列 $B$ 等价的序列。

设 $C = (c_1, \dots, c_{2k})$ 和 $D = (d_1, \dots, d_{2k})$ 是两个长度相同的整数序列。我们称 $C$ 和 $D$ 等价当且仅当存在一个映射 $f : \mathbb{Z} \to \mathbb{Z}$ 满足:

  • 对于每个整数 $x$,$f(-x) = -f(x)$,
  • 只要 $f(x) \neq 0$,整数 $x$ 和 $f(x)$ 就具有相同的符号(同为正或同为负),
  • 对于所有 $1 \le i \le 2k$,都有 $d_i = f(c_i)$。

换句话说,如果我们用 $f(c_i)$ 替换序列 $C$ 中的每个元素 $c_i$,就能得到序列 $D$。对于未在序列中出现的整数,其 $f$ 的值可以任意选择。

输入格式

第一行包含一个整数 $t$($1 \le t \le 10^5$),表示测试用例的数量。对于每个测试用例:

第一行包含一个整数 $n$($1 \le n \le 5 \cdot 10^5$),表示序列 $A$ 的长度为 $2n$.

第二行包含 $2n$ 个整数,表示 $a_1, a_2, \dots, a_{2n}$。

第三行包含一个整数 $m$($1 \le m \le 5 \cdot 10^5$),表示序列 $B$ 的长度为 $2m$。

第四行包含 $2m$ 个整数,表示 $b_1, b_2, \dots, b_{2m}$。

序列 $A$ 和 $B$ 满足题目描述中的要求。

所有测试用例的总大小受限于 $\sum n + \sum m \le 10^6$。

输出格式

对于每个测试用例,如果可以将 $A$ 转换为与 $B$ 等价的序列,输出 "YES";否则输出 "NO"。

样例

输入格式 1

4
1
1 -1
2
-1 -2 2 1
1
1 1
1
-1 1
4
1 2 -1 3 -4 2 3 -4
4
1 2 -1 -2 4 3 4 3
4
4 3 -1 -2 -3 2 1 -4
3
-1 1 3 -3 2 2

输出格式 1

YES
NO
YES
NO

输入格式 2

5
4
1 2 -3 -2 4 -1 3 -4
6
-3 5 2 3 -2 -5 1 -6 -4 -1 4 6
5
-2 4 -4 3 2 -1 -5 1 -3 5
5
3 -4 1 -5 4 -1 2 -2 -3 5
4
-2 1 4 2 -4 3 -1 -3
5
-1 -5 -2 2 1 -4 -3 3 4 5
3
2 -1 -1 -3 -2 3
4
-4 -2 -1 4 1 -2 3 -3
5
3 5 -5 -2 2 -1 -3 -4 1 4
5
3 5 -2 -5 -1 2 -4 1 4 -3

输出格式 2

YES
YES
NO
YES
NO

说明

在第一个样例中:

在第一个测试用例中,$(1, -1) \xrightarrow{\text{op 3}} (-2, 2, 1, -1) \xrightarrow{\text{op 1}} (-1, -2, 2, 1)$。

在第三个测试用例中,假设 $x = 3$,$y = 3$,$S_1 = (1, 2, -1)$,$S_3 = (-4, 2)$,$S_4 = (-4)$,且 $S_2$ 为空。 那么 $A = (S_1, x, S_2, S_3, y, S_4)$,且 $A \xrightarrow{\text{op 5}} (1, 2, -1, -2, 4, 3, 4, 3)$。

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.