[Solution] Bring Balance Codeforces Solution | Codeforces Problem Solution 2022
Alina has a bracket sequence of length , consisting of opening brackets '(' and closing brackets ')'. As she likes balance, she wants to turn this bracket sequence into a balanced bracket sequence.
In one operation, she can reverse any substring of .
What's the smallest number of operations that she needs to turn into a balanced bracket sequence? It can be shown that it's always possible in at most operations.
As a reminder, a sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters + and 1. For example, sequences (())(), (), and (()(())) are balanced, while )(, ((), and (()))( are not.
The first line of the input contains a single integer () — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer ().
The second line of each test case contains a string of length , consisting of opening and closing brackets.
The sum of over all test cases doesn't exceed .
For each test case, in the first line output a single integer — the smallest number of operations required.
The -th of the next lines should contain two integers (), indicating that in the -th operation, Alina will reverse the substring . Here the numeration starts from .
If there are multiple sequences of operations with the smallest length which transform the sequence into a balanced one, you can output any of them.
No comments:
Post a Comment