A15937 | Rearrange Brackets
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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:
- bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)");
- bracket sequences ")(", "(" and ")" are not.
You are given a regular bracket sequence. In one move, you can remove a pair of adjacent brackets such that the left one is an opening bracket and the right one is a closing bracket. Then concatenate the resulting parts without changing the order. The cost of this move is the number of brackets to the right of the right bracket of this pair.
The cost of the regular bracket sequence is the smallest total cost of the moves required to make the sequence empty.
Actually, you are not removing any brackets. Instead, you are given a regular bracket sequence and an integer $k$ . You can perform the following operation at most $k$ times:
- extract some bracket from the sequence and insert it back at any position (between any two brackets, at the start or at the end; possibly, at the same place it was before).
After all operations are performed, the bracket sequence has to be regular. What is the smallest possible cost of the resulting regular bracket sequence?
- bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)");
- bracket sequences ")(", "(" and ")" are not.
You are given a regular bracket sequence. In one move, you can remove a pair of adjacent brackets such that the left one is an opening bracket and the right one is a closing bracket. Then concatenate the resulting parts without changing the order. The cost of this move is the number of brackets to the right of the right bracket of this pair.
The cost of the regular bracket sequence is the smallest total cost of the moves required to make the sequence empty.
Actually, you are not removing any brackets. Instead, you are given a regular bracket sequence and an integer $k$ . You can perform the following operation at most $k$ times:
- extract some bracket from the sequence and insert it back at any position (between any two brackets, at the start or at the end; possibly, at the same place it was before).
After all operations are performed, the bracket sequence has to be regular. What is the smallest possible cost of the resulting regular bracket sequence?
输入格式
The first line contains a single integer $t$ ( $1 \le t \le 10^4$ ) — the number of testcases.
The first line of each testcase contains a single integer $k$ ( $0 \le k \le 5$ ) — the maximum number of operations you can perform.
The second line contains a non-empty regular bracket sequence, it consists only of characters '(' and ')'.
The total length of the regular bracket sequences over all testcases doesn't exceed $2 \cdot 10^5$ .
The first line of each testcase contains a single integer $k$ ( $0 \le k \le 5$ ) — the maximum number of operations you can perform.
The second line contains a non-empty regular bracket sequence, it consists only of characters '(' and ')'.
The total length of the regular bracket sequences over all testcases doesn't exceed $2 \cdot 10^5$ .
输出格式
For each testcase, print a single integer — the smallest possible cost of the regular bracket sequence after you perform at most $k$ operations on it.
输入输出样例
输入 #1
7 0 () 0 (()) 1 (()) 5 () 1 (()()(())) 2 ((())()(()())((()))) 3 ((())()(()())((())))
输出 #1
0 1 0 0 1 4 2
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted