1997C.md (2805B)
1 Source: https://codeforces.com/problemset/problem/1997/C 2 3 C. Even Positions 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 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. 22 23 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. 24 25 For example, let's look at RBS (())(). It has three pairs of brackets: 26 27 * (__)__: the distance between brackets at position $1$ and at $4$ is $4 - 1 = 3$; 28 * _()___: the distance is $3 - 2 = 1$; 29 * ____(): the distance is $6 - 5 = 1$. 30 31 So the cost of (())() is $3 + 1 + 1 = 5$. 32 33 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 _(_)_). 34 35 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? 36 37 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. 38 39 Input 40 41 The first line contains a single integer $t$ ($1 \le t \le 5000$) — the number of test cases. Next $t$ cases follow. 42 43 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$. 44 45 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 ')'. 46 47 Additional constraints: 48 49 * $s$ can be restored to at least one regular bracket sequence; 50 * the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. 51 52 Output 53 54 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. 55 56 Example 57 58 Input 59 60 ``` 61 4 62 63 6 64 65 _(_)_) 66 67 2 68 69 _) 70 71 8 72 73 _)_)_)_) 74 75 8 76 77 _(_)_(_) 78 ``` 79 80 Output 81 82 ``` 83 5 84 1 85 4 86 8 87 ``` 88 89 Note 90 91 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$. 92 93 In the second test case, the only option is to make $s$ equal to () with cost $1$. 94 95 In the third test case, the only possible RBS is ()()()() with cost $1 + 1 + 1 + 1 = 4$. 96 97 In the fourth test case, it's optimal to make $s$ equal to (())(()) with cost $3 + 1 + 3 + 1 = 8$.