Codeforces Round 1063 (Div. 2)
B
Siga ta Kymata
Codeforces
1700
原题
← AC →
翻译中…

You are given a permutation$^{\text{∗}}$ $p$ of every integer from $1$ to $n$. You also own a binary$^{\text{†}}$ string $s$ of size $n$ where $s_i = \mathtt{0}$ for all $1 \le i \le n$. You may do the following operation at most $5$ times:

翻译中…
  • Choose any two integers $l$ and $r$ such that $1 \le l \le r \le n$. Then, for every $i$ such that $l \lt i \lt r$ and $\min(p_l, p_r) \lt p_i \lt \max(p_l, p_r)$ hold at the same time, you will set $s_i$ to $\mathtt{1}$.
翻译中…

You are also given a binary string $x$ of size $n$. After performing operations, it must hold for every $1 \le i \le n$ that if $x_i = \mathtt{1}$, then $s_i = \mathtt{1}$. Note that if $x_i = \mathtt{0}$, then $s_i$ can have any value.

翻译中…

Figure out any sequence of at most $5$ operations such that the aforementioned condition is satisfied, or report that it is impossible to do so. Note that you do not have to minimize the number of operations you make.

翻译中…

$^{\text{∗}}$A permutation $p$ of every integer from $1$ to $n$ is a sequence of elements from $1$ to $n$ such that every element appears exactly once.$^{\text{†}}$A string $b$ of size $m$ is considered binary if and only if $b_i = \mathtt{0}$ or $b_i = \mathtt{1}$ for all $1 \le i \le m$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the size of the array.

翻译中…

The second line contains exactly $n$ integers $p_1, p_2, \ldots, p_n$ ($1 \le p_i \le n$, the elements of $p$ are pairwise distinct) — where $p_i$ is the $i$-th element of the permutation.

翻译中…

The third line contains a single binary string $x$ of size $n$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, if it is impossible to perform operations such that the constraint is satisfied, output $-1$.

翻译中…

Otherwise, output an integer $0 \le k \le 5$, the number of operations. On the $i$-th of the next $k$ lines, output two integers $1 \le l_i \le r_i \le n$, the bounds of the $i$-th operation that is performed. If there are multiple correct solutions, output any of them.

翻译中…

Note

翻译中…

In the first example, $p = [1, 2, 3]$, and $x = \mathtt{010}$. We can perform a single operation, with $l = 1$ and $r = 3$. After the operation, we set $s_2$ to $\mathtt{1}$ since $l \lt 2 \lt r$ and $\min(p_l, p_r) \lt p_2 = 2 \lt \max(p_l, p_r)$ hold at the same time. As a result, $s = \mathtt{010}$.

翻译中…

In the second example, it can be shown that there does not exist a correct sequence of at most $5$ operations, so we output $-1$.

翻译中…

In the third example, $p = [1, 3, 2, 4, 6, 5]$ and $x = \mathtt{001100}$. After performing an operation for $l = 1$ and $r = 5$, then $s = \mathtt{011100}$. If we also perform an operation for $l = 2$ and $r = 6$, then $s$ will remain the same. The string $s = \mathtt{011100}$ is valid, because for every position where $x$ has a $\mathtt{1}$, then $s$ also has a $\mathtt{1}$ at that position.

翻译中…

样例

Input #1
6
3
1 2 3
010
5
3 4 2 1 5
11111
6
1 3 2 4 6 5
001100
6
6 2 3 4 5 1
110110
5
2 1 4 3 5
00000
5
2 5 3 1 4
00100
Output #1
1
1 3
-1
2
1 5
2 6
-1
0
1
2 4
InputCopy OutputCopy
题目信息
题号
2163B
时间限制
1.5 s
内存限制
256 MB
标签
构造
贪心
模拟
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。