311317: CF1970A3. Balanced Unshuffle (Hard)
Description
The only difference with the medium version is the maximum length of the input.
A parentheses sequence is a string consisting of characters "(" and ")", for example "(()((".
A balanced parentheses sequence is a parentheses sequence which can become a valid mathematical expression after inserting numbers and operations into it, for example "(()(()))".
The balance of a parentheses sequence is defined as the number of opening parentheses "(" minus the number of closing parentheses ")". For example, the balance of the sequence "(()((" is 3.
A balanced parentheses sequence can also be defined as a parentheses sequence with balance 0 such that each of its prefixes has a non-negative balance.
We define the balanced shuffle operation that takes a parentheses sequence and returns a parentheses sequence as follows: first, for every character of the input sequence, we compute the balance of the prefix of the sequence before that character and write those down in a table together with the positions of the characters in the input sequence, for example:
Prefix balance | 0 | 1 | 2 | 1 | 2 | 3 | 2 | 1 |
Position | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
Character | ( | ( | ) | ( | ( | ) | ) | ) |
Then, we sort the columns of this table in increasing order of prefix balance, breaking ties in decreasing order of position. In the above example, we get:
Prefix balance | 0 | 1 | 1 | 1 | 2 | 2 | 2 | 3 |
Position | 1 | 8 | 4 | 2 | 7 | 5 | 3 | 6 |
Character | ( | ) | ( | ( | ) | ( | ) | ) |
The last row of this table forms another parentheses sequence, in this case "()(()())". This sequence is called the result of applying the balanced shuffle operation to the input sequence, or in short just the balanced shuffle of the input sequence.
Surprisingly, it turns out that the balanced shuffle of any balanced parentheses sequence is always another balanced parentheses sequence (we will omit the proof for brevity). Even more surprisingly, the balanced shuffles of two different balanced parentheses sequences are always different, therefore the balanced shuffle operation is a bijection on the set of balanced parentheses sequences of any given length (we will omit this proof, too).
You are given a balanced parentheses sequence. Find its preimage: the balanced parentheses sequence the balanced shuffle of which is equal to the given sequence.
InputThe only line of input contains a string $s$ consisting only of characters "(" and ")". This string is guaranteed to be a non-empty balanced parentheses sequence with its length not exceeding $500\,000$.
OutputPrint the balanced parentheses sequence $t$ such that the balanced shuffle of $t$ is equal to $s$. It is guaranteed that the answer always exists and is unique.
ExampleInput()(()())Output
(()(()))
Output
这个题目是关于平衡括号序列的一个操作,称为“平衡洗牌”(Balanced Unshuffle)。平衡括号序列指的是可以通过插入数字和运算符变成一个有效的数学表达式的括号序列。例如,"(()(()))" 就是一个平衡括号序列。
平衡洗牌操作定义如下:对于输入的括号序列,首先计算每个字符之前的序列的平衡值(开括号数量减去闭括号数量),并将这些平衡值和字符位置记录在表格中。然后,按照平衡值递增的顺序排序表格的列,如果平衡值相同,则按照位置递减的顺序排序。最后,表格的最后一行形成的新序列就是平衡洗牌的结果。
题目要求:给定一个平衡括号序列,找出它的前像——即通过平衡洗牌操作可以得到给定序列的平衡括号序列。
输入输出数据格式:
- 输入:一个平衡括号序列,仅由字符 "(" 和 ")" 组成,序列长度不超过 500,000。
- 输出:一个平衡括号序列,其平衡洗牌结果等于给定的序列。
示例:
输入:
```
()(()())
```
输出:
```
(()(()))
```
请注意,这个翻译是基于网页内容的理解,并且以中文重新表述了题目的要求。题目大意: 这个题目是关于平衡括号序列的一个操作,称为“平衡洗牌”(Balanced Unshuffle)。平衡括号序列指的是可以通过插入数字和运算符变成一个有效的数学表达式的括号序列。例如,"(()(()))" 就是一个平衡括号序列。 平衡洗牌操作定义如下:对于输入的括号序列,首先计算每个字符之前的序列的平衡值(开括号数量减去闭括号数量),并将这些平衡值和字符位置记录在表格中。然后,按照平衡值递增的顺序排序表格的列,如果平衡值相同,则按照位置递减的顺序排序。最后,表格的最后一行形成的新序列就是平衡洗牌的结果。 题目要求:给定一个平衡括号序列,找出它的前像——即通过平衡洗牌操作可以得到给定序列的平衡括号序列。 输入输出数据格式: - 输入:一个平衡括号序列,仅由字符 "(" 和 ")" 组成,序列长度不超过 500,000。 - 输出:一个平衡括号序列,其平衡洗牌结果等于给定的序列。 示例: 输入: ``` ()(()()) ``` 输出: ``` (()(())) ``` 请注意,这个翻译是基于网页内容的理解,并且以中文重新表述了题目的要求。