Even Positions
题面翻译
对于一个长度为 2 m 2m 2m 的括号序列 s s s,设其每对匹配的括号中左括号位置为 l i l_i li,右括号位置为 r i r_i ri,定义该括号序列的花费为:
cost s = ∑ i = 1 m r i − l i \text{cost}_s = \sum_{i = 1}^m r_i - l_i costs=i=1∑mri−li
t t t 组数据,每组数据给定一个长度为 n n n 的序列 s s s( n n n 为偶数),但奇数位上的字符未知,你需要填充奇数位上的字符,使得:
-
只能填充
(
和)
; -
填入后的括号序列合法。
你需要最小化填入后的括号序列的花费,输出最小花费。
1 ≤ t ≤ 5000 , 1 ≤ ∑ n ≤ 2 × 1 0 5 1 \le t \le 5000, 1 \le \sum n \le 2 \times 10^5 1≤t≤5000,1≤∑n≤2×105
translated by Ray662。
题目描述
Monocarp had a regular bracket sequence $ s $ of length $ n $ ( $ n $ 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 $ 1 $ and at $ 4 $ is $ 4 - 1 = 3 $ ;
- _()___: the distance is $ 3 - 2 = 1 $ ;
- ____(): the distance is $ 6 - 5 = 1 $ .
So the cost of (())() is $ 3 + 1 + 1 = 5 $ .Unfortunately, due to data corruption, Monocarp lost all characters on odd positions $ s_1, s_3, \dots, s_{n-1} $ . Only characters on even positions ( $ s_2, s_4, \dots, s_{n} $ ) 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.
输入格式
The first line contains a single integer $ t $ ( $ 1 \le t \le 5000 $ ) — the number of test cases. Next $ t $ cases follow.
The first line of each test case contains a single integer $ n $ ( $ 2 \le n \le 2 \cdot 10^5 $ ; $ n $ is even) — the length of string $ s $ .
The second line of each test case contains a string $ s $ of length $ n $ , where all characters on the odd positions are ‘_’ and all characters on the even positions are either ‘(’ or ‘)’.
Additional constraints:
- $ s $ can be restored to at least one regular bracket sequence;
- the total sum of $ n $ over all test cases doesn’t exceed $ 2 \cdot 10^5 $ .
输出格式
For each test case, print one integer — the minimum cost of the regular bracket sequence that can be obtained from $ s $ by replacing ‘_’-s with brackets.
样例 #1
样例输入 #1
4
6
_(_)_)
2
_)
8
_)_)_)_)
8
_(_)_(_)
样例输出 #1
5
1
4
8
提示说明
In the first test case, it’s optimal to make $ s $ equal to (())(). The cost of $ s $ will be equal to $ 3 + 1 + 1 = 5 $ .
In the second test case, the only option is to make $ s $ equal to () with cost $ 1 $ .
In the third test case, the only possible RBS is ()()()() with cost $ 1 + 1 + 1 + 1 = 4 $ .
In the fourth test case, it’s optimal to make $ s $ equal to (())(()) with cost $ 3 + 1 + 3 + 1 = 8 $ .
代码内容
// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <stack>//栈
// #include <deque>//队列
// #include <queue>//堆/优先队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=2e5+10;
char a[N][2];
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n;
string s;
cin>>n>>s;
ll ans=0;
for(auto &t:s)
{
if(t=='(') ans++;
}
cout<<n/2+ans*2<<endl;
}
return 0;
}