CF1896F.Bracket Xoring

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a binary string ss of length 2n2n where each element is 0\mathtt{0} or 1\mathtt{1}. You can do the following operation:

  1. Choose a balanced bracket sequence†^\dagger bb of length 2n2n.
  2. For every index ii from 11 to 2n2n in order, where bib_i is an open bracket, let pip_i denote the minimum index such that b[i,pi]b[i,p_i] is a balanced bracket sequence. Then, we perform a range toggle operation‡^\ddagger from ii to pip_i on ss. Note that since a balanced bracket sequence of length 2n2n will have nn open brackets, we will do nn range toggle operations on ss.

Your task is to find a sequence of no more than 1010 operations that changes all elements of ss to 0\mathtt{0}, or determine that it is impossible to do so. Note that you do not have to minimize the number of operations.

Under the given constraints, it can be proven that if it is possible to change all elements of ss to 0\mathtt{0}, there exists a way that requires no more than 1010 operations.

†^\dagger A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters ++ and 11. For example, sequences "(())()", "()", and "(()(()))" are balanced, while ")(", "(()", and "(()))(" are not.

‡^\ddagger If we perform a range toggle operation from ll to rr on a binary string ss, then we toggle all values of sis_i such that l≤i≤rl \leq i \leq r. If sis_i is toggled, we will set si:=0s_i := \mathtt{0} if si=1s_i = \mathtt{1} or vice versa. For example, if s=1000101s=\mathtt{1000101} and we perform a range toggle operation from 33 to 55, ss will be changed to s=1011001s=\mathtt{1011001}.

给你一个长度为 2n2n 的二进制字符串 ss,其中每个元素为 0\mathtt{0} 或 1\mathtt{1}。你可以执行以下操作:

  1. 选择一个长度为 2n2n 的平衡括号序列†^\dagger bb。
  2. 按照从 11 到 2n2n 的顺序,对每个满足 bib_i 是左括号的下标 ii,令 pip_i 表示满足子串 b[i,pi]b[i,p_i] 构成平衡括号序列的最小下标。接着,我们在 ss 上执行一次从 ii 到 pip_i 的区间翻转操作‡^\ddagger。注意:由于长度为 2n2n 的平衡括号序列恰好包含 nn 个左括号,因此该操作总共会对 ss 执行 nn 次区间翻转。

你的任务是:找到至多 1010 次操作的序列,使得 ss 的所有元素均变为 0\mathtt{0};若无法实现,则判定其不可能。注意:你无需最小化操作次数。

在本题给定的约束条件下,可以证明:若能将 ss 全部变为 0\mathtt{0},则一定存在一种方案,其操作次数不超过 1010 次。

†^\dagger 若一个括号序列可通过添加字符 ++ 和 11 变为合法的数学表达式,则称其为平衡括号序列。例如,"(())()"、"()" 和 "(()(()))" 是平衡的,而 ")("、"(()" 和 "(()))(" 不是。

‡^\ddagger 对二进制字符串 ss 执行从 ll 到 rr 的区间翻转操作,指将所有满足 l≤i≤rl \leq i \leq r 的 sis_i 进行翻转:若 si=1s_i = \mathtt{1},则设为 0\mathtt{0};若 si=0s_i = \mathtt{0},则设为 1\mathtt{1}。例如,若 s=1000101s=\mathtt{1000101},执行从 33 到 55 的区间翻转后,ss 将变为 1011001\mathtt{1011001}。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤10001 \le t \le 1000). The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051 \le n \le 2\cdot 10^5) — where 2n2n is the length of string ss.

The second line of each test case contains a binary string ss of length 2n2n (si=0s_i = \mathtt{0} or si=1s_i = \mathtt{1}).

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤10001 \le t \le 1000)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2\cdot 10^5),其中 2n2n 是字符串 ss 的长度。

每个测试用例的第二行包含一个长度为 2n2n 的二进制字符串 ss(即对所有 ii,有 si=0s_i = \mathtt{0} 或 si=1s_i = \mathtt{1})。

保证所有测试用例的 nn 值之和不超过 2⋅1052\cdot 10^5。

输出格式

For each test case, output −1-1 in a single line if it is impossible to change all elements of ss to 0\mathtt{0}.

Otherwise, output a single integer kk (0≤k≤100 \le k \le 10) representing the number of operations needed to change all elements of ss to 0\mathtt{0}. Then, on each of the next kk lines, output a balanced bracket sequence of length 2n2n representing the operations needed to change all elements of ss to 00s.

If there are multiple ways to change all elements of ss to 0\mathtt{0} that require not more than 1010 operations, you can output any of them.

对于每个测试用例,如果无法将字符串 ss 的所有元素都变为 0\mathtt{0},则在一行中输出 −1-1。

否则,输出一个整数 kk(0≤k≤100 \le k \le 10),表示将 ss 的所有元素变为 0\mathtt{0} 所需的操作次数。随后的 kk 行中,每行输出一个长度为 2n2n 的平衡括号序列,表示将 ss 的所有元素变为 0\mathtt{0} 所需的操作。

若存在多种方案可在不超过 1010 次操作内将 ss 的所有元素变为 0\mathtt{0},则输出任意一种即可。

输入输出样例

  • 输入#1

    4
    1
    01
    2
    0000
    3
    100111
    4
    01011100

    输出#1

    -1
    2
    ()()
    ()()
    1
    (())()
    2
    (((())))
    ()()(())

说明/提示

In the first test case, it can be proven that it is impossible to change all elements of ss to 0\mathtt{0}.

In the second test case, the first operation using the bracket sequence b=()()b = \mathtt{()()} will convert the binary string s=0000s=\mathtt{0000} to s=1111s=\mathtt{1111}. Then, the second operation using the same bracket sequence b=()()b = \mathtt{()()} will convert the binary string s=1111s=\mathtt{1111} back to s=0000s=\mathtt{0000}. Note that since all elements of ss is already 0\mathtt{0} initially, using 00 operations is also a valid answer.

In the third test case, a single operation using the bracket sequence b=(())()b = \mathtt{(())()} will change all elements of ss to 0\mathtt{0}. The operation will happen as follows.

  1. b1b_1 is an open bracket and p1=4p_1 = 4 since b[1,4]=(())b[1,4]=\mathtt{(())} is a balanced bracket sequence. Hence, we do a range toggle operation from 11 to 44 on the binary string s=100111s = \mathtt{100111} to obtain s=011011s = \mathtt{011011}.
  2. b2b_2 is an open bracket and p2=3p_2 = 3 since b[2,3]=()b[2,3]=\mathtt{()} is a balanced bracket sequence. Hence, we do a range toggle operation from 22 to 33 on the binary string s=011011s = \mathtt{011011} to obtain s=000011s = \mathtt{000011}.
  3. b3b_3 is not an open bracket, so no range toggle operation is done at this step.
  4. b4b_4 is not an open bracket, so no range toggle operation is done at this step.
  5. b5b_5 is an open bracket and p5=6p_5 = 6 since b[5,6]=()b[5,6]=\mathtt{()} is a balanced bracket sequence. Hence, we do a range toggle operation from 55 to 66 on the binary string s=000011s = \mathtt{000011} to obtain s=000000s = \mathtt{000000}.
  6. b6b_6 is not an open bracket, so no range toggle operation is done at this step.

In the fourth test case, the first operation using the bracket sequence b=(((())))b = \mathtt{(((())))} will convert the binary string s=01011100s = \mathtt{01011100} to s=11111001s = \mathtt{11111001}. Then, the second operation using the bracket sequence b=()()(())b = \mathtt{()()(())} will convert the binary string s=11111001s = \mathtt{11111001} to s=00000000s=\mathtt{00000000}.

在第一个测试用例中,可以证明无法将 ss 的所有元素都变为 0\mathtt{0}。

在第二个测试用例中,第一次操作使用括号序列 b=()()b = \mathtt{()()},将二进制字符串 s=0000s=\mathtt{0000} 转换为 s=1111s=\mathtt{1111};随后,第二次操作再次使用相同的括号序列 b=()()b = \mathtt{()()},将二进制字符串 s=1111s=\mathtt{1111} 恢复为 s=0000s=\mathtt{0000}。注意:由于 ss 初始时所有元素已是 0\mathtt{0},因此执行 00 次操作也是一个合法答案。

在第三个测试用例中,仅需一次操作,使用括号序列 b=(())()b = \mathtt{(())()} 即可将 ss 的所有元素变为 0\mathtt{0}。该操作过程如下:

  1. b1b_1 是左括号,且 p1=4p_1 = 4(因为子串 b[1,4]=(())b[1,4]=\mathtt{(())} 是一个平衡括号序列),因此对二进制字符串 s=100111s = \mathtt{100111} 执行区间翻转操作 [1,4][1,4],得到 s=011011s = \mathtt{011011}。
  2. b2b_2 是左括号,且 p2=3p_2 = 3(因为子串 b[2,3]=()b[2,3]=\mathtt{()} 是一个平衡括号序列),因此对二进制字符串 s=011011s = \mathtt{011011} 执行区间翻转操作 [2,3][2,3],得到 s=000011s = \mathtt{000011}。
  3. b3b_3 不是左括号,因此此步不执行任何区间翻转操作。
  4. b4b_4 不是左括号,因此此步不执行任何区间翻转操作。
  5. b5b_5 是左括号,且 p5=6p_5 = 6(因为子串 b[5,6]=()b[5,6]=\mathtt{()} 是一个平衡括号序列),因此对二进制字符串 s=000011s = \mathtt{000011} 执行区间翻转操作 [5,6][5,6],得到 s=000000s = \mathtt{000000}。
  6. b6b_6 不是左括号,因此此步不执行任何区间翻转操作。

在第四个测试用例中,第一次操作使用括号序列 b=(((())))b = \mathtt{(((())))},将二进制字符串 s=01011100s = \mathtt{01011100} 转换为 s=11111001s = \mathtt{11111001};随后,第二次操作使用括号序列 b=()()(())b = \mathtt{()()(())},将二进制字符串 s=11111001s = \mathtt{11111001} 转换为 s=00000000s=\mathtt{00000000}。

输入解题思路,AI测评打分。不知道怎么写?

首页