CF2190B1.Sub-RBS (Easy Version)

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

This is the easy version of the problem. The difference between the versions is that in this version, you only need to evaluate for whole string ss, ss is regular, and the constraints on nn are higher.

We say that a bracket sequence aa is better than a bracket sequence bb if one of the following holds:

  • bb is a prefix of aa, but a≠ba \ne b; or
  • let ii be the first position (if it exists) where ai≠bia_i \neq b_i, then ai=(\color{red}{a_i = \texttt{(}} and bi=)\color{red}{b_i = \texttt{)}}.

You are given a regular bracket sequence∗^{\text{∗}} ss of even length nn.

Among all non-empty subsequences †^{\text{†}} tt of ss that are regular bracket sequences, find the maximum possible length of tt such that tt is better than ss. If no such tt exists, report it.

∗^{\text{∗}}A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting the characters 1\texttt{1} and +\texttt{+} between the original characters of the sequence. For example:

  • bracket sequences ()()\texttt{()()} and (())\texttt{(())} are regular (the resulting expressions are (1)+(1)\texttt{(1)+(1)} and ((1+1)+1)\texttt{((1+1)+1)});
  • bracket sequences )(\texttt{)(}, (\texttt{(}, and )\texttt{)} are not.

†^{\text{†}}A sequence aa is a subsequence of a sequence bb if aa can be obtained from bb by the deletion of several (possibly, zero or all) element from arbitrary positions.

这是该问题的简单版本。两个版本的区别在于:在本版本中,你只需对整个字符串 ss 进行求解,ss 是正则括号序列,且 nn 的约束更大。

我们称括号序列 aa 优于 括号序列 bb,当且仅当满足以下任一条件:

  • bb 是 aa 的前缀,但 a≠ba \ne b;或者
  • 设 ii 为第一个满足 ai≠bia_i \neq b_i 的位置(若存在),则 ai=(\color{red}{a_i = \texttt{(}} 且 bi=)\color{red}{b_i = \texttt{)}}。

给定一个长度为偶数 nn 的正则括号序列∗^{\text{∗}} ss。

在 ss 的所有非空子序列†^{\text{†}} tt(其中 tt 本身也是正则括号序列)中,找出满足 tt 优于 ss 的 tt 的最大可能长度。若不存在这样的 tt,请报告这一点。

∗^{\text{∗}} 正则括号序列是指:可通过在原序列字符之间插入字符 1\texttt{1} 和 +\texttt{+},将其转化为合法算术表达式的括号序列。例如:

  • 括号序列 ()()\texttt{()()} 和 (())\texttt{(())} 是正则的(对应表达式分别为 (1)+(1)\texttt{(1)+(1)} 和 ((1+1)+1)\texttt{((1+1)+1)});
  • 括号序列 )(\texttt{)(}、(\texttt{(} 和 )\texttt{)} 不是正则的。

†^{\text{†}} 序列 aa 是序列 bb 的子序列,当且仅当 aa 可通过从 bb 中任意位置删除若干(可能为零个或全部)元素而得到。

输入格式

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

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

The second line of each test case contains a sequence ss of length nn consisting only of characters (\texttt{(} and )\texttt{)}.

It is guaranteed that the given sequence ss is a regular bracket sequence.

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

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

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5,且 nn 为偶数)—— 字符串 ss 的长度。

每个测试用例的第二行包含一个长度为 nn 的序列 ss,其中仅包含字符 (\texttt{(} 和 )\texttt{)}。

保证给定的序列 ss 是一个合法括号序列(regular bracket sequence)。

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

输出格式

For each test case, print a single integer — the maximum possible length of a non-empty subsequence tt of ss that is a regular bracket sequence and is better than ss. If no such tt exists, print −1-1.

对于每个测试用例,输出一个整数——字符串 ss 的非空子序列 tt 的最大可能长度,使得 tt 是一个合法括号序列,且优于 ss。若不存在这样的 tt,则输出 −1-1。

输入输出样例

  • 输入#1

    3
    2
    ()
    8
    (()(()))
    6
    (())()

    输出#1

    -1
    6
    -1

说明/提示

In the first example, the only non-empty regular bracket subsequence of ss is t=s=()t = s = \texttt{()}. Since tt is not better than ss, we output −1-1.

In the second example, we can choose t=((()))t = \texttt{((()))}. The first index where tt and ss differ is i=3i = 3. Since t3=(t_3 = \texttt{(} and s3=)s_3 = \texttt{)}, tt is better than ss. We cannot choose a longer subsequence because the only longer regular bracket subsequence is ss itself, which is not better than ss. Thus, we output 66.

在第一个例子中,ss 的唯一非空正则括号子序列是 t=s=()t = s = \texttt{()}。由于 tt 并不优于 ss,我们输出 −1-1。

在第二个例子中,我们可以选择 t=((()))t = \texttt{((()))}。tt 与 ss 首次不同的下标是 i=3i = 3。由于 t3=(t_3 = \texttt{(} 而 s3=)s_3 = \texttt{)},因此 tt 优于 ss。我们无法选择更长的子序列,因为唯一更长的正则括号子序列就是 ss 本身,而它并不优于 ss。因此,我们输出 66。

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

首页