codeforces-scrape

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

1097C.md (2139B)


      1 Source: https://codeforces.com/problemset/problem/1097/C
      2 
      3 C. Yuhao and a Parenthesis
      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 One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences.
     22 
     23 A bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it's possible to obtain a correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, the sequences "(())()", "()" and "(()(()))" are correct, while the bracket sequences ")(", "(()" and "(()))(" are not correct.
     24 
     25 Yuhao found this problem too simple for him so he decided to make the problem harder. You are given many (not necessarily correct) bracket sequences. The task is to connect some of them into ordered pairs so that each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence. The goal is to create as many pairs as possible.
     26 
     27 This problem unfortunately turned out to be too difficult for Yuhao. Can you help him and solve it?
     28 
     29 Input
     30 
     31 The first line contains one integer $n$ ($1 \leq n \leq 10^5$) — the number of bracket sequences.
     32 
     33 Each of the following $n$ lines contains one bracket sequence — a non-empty string which consists only of characters "(" and ")".
     34 
     35 The sum of lengths of all bracket sequences in the input is at most $5 \cdot 10^5$.
     36 
     37 Note that a bracket sequence may appear in the input multiple times. In this case, you can use each copy of the sequence separately. Also note that the order in which strings appear in the input doesn't matter.
     38 
     39 Output
     40 
     41 Print a single integer — the maximum number of pairs which can be made, adhering to the conditions in the statement.
     42 
     43 Examples
     44 
     45 Input
     46 
     47 ```
     48 7
     49 )())
     50 )
     51 ((
     52 ((
     53 (
     54 )
     55 )
     56 ```
     57 
     58 Output
     59 
     60 ```
     61 2
     62 ```
     63 
     64 Input
     65 
     66 ```
     67 4
     68 (
     69 ((
     70 (((
     71 (())
     72 ```
     73 
     74 Output
     75 
     76 ```
     77 0
     78 ```
     79 
     80 Input
     81 
     82 ```
     83 2
     84 (())
     85 ()
     86 ```
     87 
     88 Output
     89 
     90 ```
     91 1
     92 ```
     93 
     94 Note
     95 
     96 In the first example, it's optimal to construct two pairs: "((     )())" and "(     )".