CF1750E.Bracket Cost

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Daemon Targaryen decided to stop looking like a Metin2 character. He turned himself into the most beautiful thing, a bracket sequence.

For a bracket sequence, we can do two kind of operations:

  • Select one of its substrings†^\dagger and cyclic shift it to the right. For example, after a cyclic shift to the right, "(())" will become ")(()";
  • Insert any bracket, opening '(' or closing ')', wherever you want in the sequence.

We define the cost of a bracket sequence as the minimum number of such operations to make it balanced‡^\ddagger.

Given a bracket sequence ss of length nn, find the sum of costs across all its n(n+1)2\frac{n(n+1)}{2} non-empty substrings. Note that for each substring we calculate the cost independently.

†^\dagger A string aa is a substring of a string bb if aa can be obtained from bb by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

‡^\ddagger 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.

戴蒙·坦格利安决定不再看起来像《冒险岛》(Metin2)中的角色。他将自己变成世间最美丽之物——一个括号序列。

对于一个括号序列,我们可以执行以下两种操作:

  • 选取其任意一个子串†^\dagger,并将其向右循环移位。例如,对 "(())" 向右循环移位后,得到 ")(()";
  • 在序列中任意位置插入任意一个括号,即左括号 '(' 或右括号 ')'。

我们定义一个括号序列的代价为:使其变为平衡序列‡^\ddagger 所需的上述操作的最少次数。

给定一个长度为 nn 的括号序列 ss,求其全部 n(n+1)2\frac{n(n+1)}{2} 个非空子串的代价之和。注意:对每个子串,我们独立地计算其代价。

†^\dagger 字符串 aa 是字符串 bb 的子串,当且仅当 aa 可通过从 bb 的开头删除若干(可能为零或全部)字符、并从 bb 的末尾删除若干(可能为零或全部)字符而得到。

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

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1051 \leq t \leq 10^5) — the number of test cases. The description of 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) — the length of the bracket sequence.

The second line of each test case contains a string ss, consisting only of characters '(' and ')', of length nn — the bracket sequence.

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

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1051 \leq t \leq 10^5),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5),表示括号序列的长度。

每个测试用例的第二行包含一个长度为 nn 的字符串 ss,该字符串仅由字符 '(' 和 ')' 组成,即所给的括号序列。

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

输出格式

For each test case, print a single integer — the sum of costs of all substrings of ss.

对于每个测试用例,输出一个整数——字符串 ss 的所有子串的代价之和。

输入输出样例

  • 输入#1

    5
    1
    )
    4
    )()(
    3
    ())
    5
    (((((
    10
    )(())))())

    输出#1

    1
    9
    6
    35
    112

说明/提示

In the first test case, there is the only substring ")". Its cost is 11 because we can insert '(' to the beginning of this substring and get a string "()", that is a balanced string.

In the second test case, the cost of each substring of length one is 11. The cost of a substring ")(" is 11 because we can cyclically shift it to right and get a string "()". The cost of strings ")()" and "()(" is 11 because its enough to insert one bracket to each of them. The cost of substring ")()(" is 11 because we can cyclically shift it to right and get a string "()()". So there are 4+2+2+1=94 + 2 + 2 + 1 = 9 substring of cost 11 and 11 substring of cost 00. So the sum of the costs is 99.

In the third test case,

  • "(", the cost is 11;
  • "()", the cost is 00;
  • "())", the cost is 11;
  • ")", the cost is 11;
  • "))", the cost is 22;
  • ")", the cost is 11.

So the sum of the costs is 66.

在第一个测试用例中,唯一的子串是 ")"。其代价为 11,因为我们可以在此子串开头插入 '(',从而得到字符串 "()",这是一个平衡字符串。

在第二个测试用例中,每个长度为一的子串的代价均为 11。子串 ")(" 的代价为 11,因为我们可将其向右循环移位,得到字符串 "()"。子串 ")()" 和 "()(" 的代价均为 11,因为各自只需插入一个括号即可使其平衡。子串 ")()(" 的代价为 11,因为我们可将其向右循环移位,得到字符串 "()()"。因此,代价为 11 的子串共有 4+2+2+1=94 + 2 + 2 + 1 = 9 个,代价为 00 的子串有 11 个。故所有子串的代价之和为 99。

在第三个测试用例中,

  • "(" 的代价为 11;
  • "()" 的代价为 00;
  • "())" 的代价为 11;
  • ")" 的代价为 11;
  • "))" 的代价为 22;
  • ")" 的代价为 11。

因此,所有子串的代价之和为 66。

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

首页