codeforces-scrape

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

1837D.md (2400B)


      1 Source: https://codeforces.com/problemset/problem/1837/D
      2 
      3 D. Bracket Coloring
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 512 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example:
     22 
     23 * the bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)");
     24 * the bracket sequences ")(", "(" and ")" are not.
     25 
     26 A bracket sequence is called beautiful if one of the following conditions is satisfied:
     27 
     28 * it is a regular bracket sequence;
     29 * if the order of the characters in this sequence is reversed, it becomes a regular bracket sequence.
     30 
     31 For example, the bracket sequences "()()", "(())", ")))(((", "))()((" are beautiful.
     32 
     33 You are given a bracket sequence $s$. You have to color it in such a way that:
     34 
     35 * every bracket is colored into one color;
     36 * for every color, there is at least one bracket colored into that color;
     37 * for every color, if you write down the sequence of brackets having that color in the order they appear, you will get a beautiful bracket sequence.
     38 
     39 Color the given bracket sequence $s$ into the minimum number of colors according to these constraints, or report that it is impossible.
     40 
     41 Input
     42 
     43 The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
     44 
     45 Each test case consists of two lines. The first line contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of characters in $s$. The second line contains $s$ — a string of $n$ characters, where each character is either "(" or ")".
     46 
     47 Additional constraint on the input: the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
     48 
     49 Output
     50 
     51 For each test case, print the answer as follows:
     52 
     53 * if it is impossible to color the brackets according to the problem statement, print $-1$;
     54 * otherwise, print two lines. In the first line, print one integer $k$ ($1 \le k \le n$) — the minimum number of colors. In the second line, print $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le k$), where $c_i$ is the color of the $i$-th bracket. If there are multiple answers, print any of them.
     55 
     56 Example
     57 
     58 Input
     59 
     60 ```
     61 4
     62 
     63 8
     64 
     65 ((())))(
     66 
     67 4
     68 
     69 (())
     70 
     71 4
     72 
     73 ))((
     74 
     75 3
     76 
     77 (()
     78 ```
     79 
     80 Output
     81 
     82 ```
     83 2
     84 2 2 2 1 2 2 2 1
     85 1
     86 1 1 1 1
     87 1
     88 1 1 1 1
     89 -1
     90 ```