欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1997C.Even Positions
Even Positions
Monocarp had a regular bracket sequence of length ( is even). He even came up with his own way to calculate its cost.
He knows that in a regular bracket sequence (RBS), each opening bracket is paired up with the corresponding closing bracket. So he decided to calculate the cost of RBS as the sum of distances between pairs of corresponding bracket pairs.
For example, let's look at RBS (())(). It has three pairs of brackets:
(\_\_)\_\_: the distance between brackets at position and at is ;\_()\_\_\_: the distance is ;\_\_\_\_(): the distance is .
So the cost of (())() is .
Unfortunately, due to data corruption, Monocarp lost all characters on odd positions . Only characters on even positions () remain. For example, (())() turned to \_(\_)\_).
Monocarp wants to restore his RBS by placing brackets on the odd positions. But since the restored RBS may not be unique, he wants to choose one with minimum cost. It's too hard to do for Monocarp alone, so can you help him?
Reminder: A regular bracket sequence is a string consisting of only brackets, such that this sequence, when inserted 1-s and +-s, gives a valid mathematical expression. For example, (), (()) or (()())() are RBS, while ), ()( or ())(() are not.
Input
The first line contains a single integer () — the number of test cases. Next cases follow.
The first line of each test case contains a single integer (; is even) — the length of string .
The second line of each test case contains a string of length , where all characters on the odd positions are '\_' and all characters on the even positions are either '(' or ')'.
Additional constraints:
- can be restored to at least one regular bracket sequence;
- the total sum of over all test cases doesn't exceed .
Output
For each test case, print one integer — the minimum cost of the regular bracket sequence that can be obtained from by replacing '\_'-s with brackets.
Note
In the first test case, it's optimal to make equal to (())(). The cost of will be equal to .
In the second test case, the only option is to make equal to () with cost .
In the third test case, the only possible RBS is ()()()() with cost .
In the fourth test case, it's optimal to make equal to (())(()) with cost .
Samples
4
6
_(_)_)
2
_)
8
_)_)_)_)
8
_(_)_(_)
5
1
4
8
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |