codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1374C.md (2321B)


      1 Source: https://codeforces.com/problemset/problem/1374/C
      2 
      3 C. Move Brackets
      4 
      5 time limit per test
      6 
      7 1 second
      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 You are given a bracket sequence $s$ of length $n$, where $n$ is even (divisible by two). The string $s$ consists of $\frac{n}{2}$ opening brackets '(' and $\frac{n}{2}$ closing brackets ')'.
     22 
     23 In one move, you can choose exactly one bracket and move it to the beginning of the string or to the end of the string (i.e. you choose some index $i$, remove the $i$-th character of $s$ and insert it before or after all remaining characters of $s$).
     24 
     25 Your task is to find the minimum number of moves required to obtain regular bracket sequence from $s$. It can be proved that the answer always exists under the given constraints.
     26 
     27 Recall what the regular bracket sequence is:
     28 
     29 * "()" is regular bracket sequence;
     30 * if $s$ is regular bracket sequence then "(" + $s$ + ")" is regular bracket sequence;
     31 * if $s$ and $t$ are regular bracket sequences then $s$ + $t$ is regular bracket sequence.
     32 
     33 For example, "()()", "(())()", "(())" and "()" are regular bracket sequences, but ")(", "()(" and ")))" are not.
     34 
     35 You have to answer $t$ independent test cases.
     36 
     37 Input
     38 
     39 The first line of the input contains one integer $t$ ($1 \le t \le 2000$) — the number of test cases. Then $t$ test cases follow.
     40 
     41 The first line of the test case contains one integer $n$ ($2 \le n \le 50$) — the length of $s$. It is guaranteed that $n$ is even. The second line of the test case containg the string $s$ consisting of $\frac{n}{2}$ opening and $\frac{n}{2}$ closing brackets.
     42 
     43 Output
     44 
     45 For each test case, print the answer — the minimum number of moves required to obtain regular bracket sequence from $s$. It can be proved that the answer always exists under the given constraints.
     46 
     47 Example
     48 
     49 Input
     50 
     51 ```
     52 4
     53 2
     54 )(
     55 4
     56 ()()
     57 8
     58 ())()()(
     59 10
     60 )))((((())
     61 ```
     62 
     63 Output
     64 
     65 ```
     66 1
     67 0
     68 1
     69 3
     70 ```
     71 
     72 Note
     73 
     74 In the first test case of the example, it is sufficient to move the first bracket to the end of the string.
     75 
     76 In the third test case of the example, it is sufficient to move the last bracket to the beginning of the string.
     77 
     78 In the fourth test case of the example, we can choose last three openning brackets, move them to the beginning of the string and obtain "((()))(())".