CF1685C.Bring Balance

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Alina has a bracket sequence ss of length 2n2n, consisting of nn opening brackets '(' and nn closing brackets ')'. As she likes balance, she wants to turn this bracket sequence into a balanced bracket sequence.

In one operation, she can reverse any substring of ss.

What's the smallest number of operations that she needs to turn ss into a balanced bracket sequence? It can be shown that it's always possible in at most nn operations.

As a reminder, a sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters + and 1. For example, sequences (())(), (), and (()(())) are balanced, while )(, ((), and (()))( are not.

阿丽娜有一个长度为 2n2n 的括号序列 ss,其中包含 nn 个左括号 '(' 和 nn 个右括号 ')'。由于她喜欢“平衡”,她希望将该括号序列变为一个平衡括号序列。

在一次操作中,她可以翻转 ss 的任意一个子串。

请问:将 ss 变为平衡括号序列所需的最少操作次数是多少?可以证明,至多只需 nn 次操作即可完成。

作为提示:若一个括号序列可通过添加字符 + 和 1 转化为一个合法的数学表达式,则称其为平衡括号序列。例如,序列 (())()、() 和 (()(())) 是平衡的,而 )(、(() 和 (()))( 则不是。

输入格式

The first line of the input contains a single integer tt (1≤t≤2⋅1041 \le t \le 2 \cdot 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1051 \le n \le 10^5).

The second line of each test case contains a string ss of length 2n2n, consisting of nn opening and nn closing brackets.

The sum of nn over all test cases doesn't exceed 2⋅1052\cdot 10^5.

输入的第一行包含一个整数 tt(1≤t≤2⋅1041 \le t \le 2 \cdot 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5)。

每个测试用例的第二行包含一个长度为 2n2n 的字符串 ss,该字符串由 nn 个左括号和 nn 个右括号组成。

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

输出格式

For each test case, in the first line output a single integer kk (0≤k≤n)(0 \le k \le n) — the smallest number of operations required.

The ii-th of the next kk lines should contain two integers li,ril_i, r_i (1≤li≤ri≤2n1 \le l_i \le r_i \le 2n), indicating that in the ii-th operation, Alina will reverse the substring slsl+1…sr−1srs_ls_{l+1} \ldots s_{r-1}s_r. Here the numeration starts from 11.

If there are multiple sequences of operations with the smallest length which transform the sequence into a balanced one, you can output any of them.

对于每个测试用例,在第一行输出一个整数 kk(0≤k≤n0 \le k \le n)—— 即将序列变为平衡序列所需的最少操作次数。

接下来的 kk 行中,第 ii 行应包含两个整数 li,ril_i, r_i(1≤li≤ri≤2n1 \le l_i \le r_i \le 2n),表示在第 ii 次操作中,Alina 将反转子串 slsl+1…sr−1srs_ls_{l+1} \ldots s_{r-1}s_r。此处下标从 11 开始计数。

若存在多个长度最短的操作序列可将序列变为平衡序列,你可以输出其中任意一个。

输入输出样例

  • 输入#1

    3
    2
    (())
    5
    ())((()))(
    6
    ())((()))(()

    输出#1

    0
    2
    3 4
    9 10
    1
    2 11

说明/提示

In the first test case, the string is already balanced.

In the second test case, the string will be transformed as follows: ())((()))( →\to ()()(()))( →\to ()()(())(), where the last string is balanced.

In the third test case, the string will be transformed to ((()))((())), which is balanced.

在第一个测试用例中,该字符串已经是平衡的。

在第二个测试用例中,该字符串将按如下方式变换:())((()))( →\to ()()(()))( →\to ()()(())(),其中最终的字符串是平衡的。

在第三个测试用例中,该字符串将变换为 ((()))((())),它是平衡的。

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

首页