Swapping Brackets

Time limit: 0.5s Memory limit: 64MB Input: Output:

Task

Do you remember Baby Bob? His older brother, Balanced Benjamin would like to give a gift to his brother. He has a string SS of length NN, where NN is even. The string consists of N2\frac{N}{2} opening brackets ( and N2\frac{N}{2} closing brackets ).

As Bob likes valid bracket sequences, Benjamin would like to turn SS into a valid bracket sequence by swapping (not necessarily adjacent) characters in SS.

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 SS into a balanced bracket sequence by swapping characters in SS 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 NN, the length of SS.
  • a line containing string SS, a bracket sequence of length NN.

Output data

The output file consists of:

  • a line containing integer RR, the minimum number of swaps necessary.
  • RR lines, each line contains the indices (00-based) of the two characters to be swapped.

Constraints and clarifications

  • 1N1061 \leq N \leq 10^6.
  • NN is even.
  • SS has length NN and only contains characters ( and ).
# Points Constraints
1 0 Examples.
2 33 N16N \le 16
3 44 N5678N \le 5678
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 SS the following way:
)))((()( \rightarrow ())(())( \rightarrow (()(()))

Example 3

stdin

10
()(()(()))

stdout

0

Explanation

In the third sample case the bracket sequence is already valid.

Log in or sign up to be able to send submissions!