Task
Do you remember Baby Bob? His older brother, Balanced Benjamin would like to give a gift to his brother. He has a string of length , where is even. The string consists of opening brackets (
and closing brackets )
.
As Bob likes valid bracket sequences, Benjamin would like to turn into a valid bracket sequence by swapping (not necessarily adjacent) characters in .
A bracket sequence is valid if 1
and +
characters can be inserted into it so that it becomes a valid mathematical expression. For example, ()(()(()))
is a valid bracket sequence.
Since he's less mathematically inclined than his younger brother, he asks for your help.
Write a program that turns into a balanced bracket sequence by swapping characters in using the minimum number of swaps.
If there are more than one way to do this, you can output any of them.
Input data
The input file consists of:
- a line containing integer , the length of .
- a line containing string , a bracket sequence of length .
Output data
The output file consists of:
- a line containing integer , the minimum number of swaps necessary.
- lines, each line contains the indices (-based) of the two characters to be swapped.
Constraints and clarifications
- .
- is even.
- has length and only contains characters
(
and)
.
# | Points | Constraints |
---|---|---|
1 | 0 | Examples. |
2 | 33 | |
3 | 44 | |
4 | 23 | No additional limitations. |
Example 1
stdin
4
)()(
stdout
1
0 3
Explanation
In the first sample case the string after the swap looks like this: (())
.
Example 2
stdin
8
)))((()(
stdout
2
0 5
7 1
Explanation
In the second sample case the swaps change string the following way:
)))((()(
())(())(
(()(()))
Example 3
stdin
10
()(()(()))
stdout
0
Explanation
In the third sample case the bracket sequence is already valid.