CF1227C.Messy
普及+/提高
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are fed up with your messy room, so you decided to clean it up.
Your room is a bracket sequence s=s1s2…sn of length n. Each character of this string is either an opening bracket '(' or a closing bracket ')'.
In one operation you can choose any consecutive substring of s and reverse it. In other words, you can choose any substring s[l…r]=sl,sl+1,…,sr and change the order of elements in it into sr,sr−1,…,sl.
For example, if you will decide to reverse substring s[2…4] of string s="((()))" it will be equal to s="()(())".
A regular (aka balanced) 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 "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not.
A prefix of a string s is a substring that starts at position 1. For example, for s="(())()" there are 6 prefixes: "(", "((", "(()", "(())", "(())(" and "(())()".
In your opinion, a neat and clean room s is a bracket sequence that:
- the whole string s is a regular bracket sequence;
- and there are exactly k prefixes of this sequence which are regular (including whole s itself).
For example, if k=2, then "(())()" is a neat and clean room.
You want to use at most n operations to make your room neat and clean. Operations are applied one after another sequentially.
It is guaranteed that the answer exists. Note that you do not need to minimize the number of operations: find any way to achieve the desired configuration in n or less operations.
你厌倦了自己凌乱的房间,于是决定把它收拾干净。
你的房间是一个长度为 n 的括号序列 s=s1s2…sn。该字符串的每个字符要么是左括号 '(',要么是右括号 ')'。
在一次操作中,你可以任选 s 的一个连续子串并将其反转。换言之,你可以任选子串 s[l…r]=sl,sl+1,…,sr,并将其中元素顺序变为 sr,sr−1,…,sl。
例如,若对字符串 s="((()))" 的子串 s[2…4] 进行反转,则结果为 s="()(())"。
正则(即平衡)括号序列 是指:通过在原序列的字符之间插入字符 '1' 和 '+',可将其转化为一个合法的算术表达式。例如,括号序列 "()()"、"(())" 是正则的(对应表达式分别为 "(1)+(1)"、"((1+1)+1)"),而 ")(" 和 "(" 则不是。
字符串 s 的一个前缀是指从位置 1 开始的子串。例如,对 s="(())()",共有 6 个前缀:"(", "((", "(()", "(())", "(())(" 和 "(())()"。
在你看来,一个整洁干净的房间 s 是满足以下条件的括号序列:
- 整个字符串 s 是一个正则括号序列;
- 该序列恰好有 k 个前缀是正则的(包含 s 本身)。
例如,当 k=2 时,"(())()" 就是一个整洁干净的房间。
你希望至多使用 n 次操作,使你的房间变得整洁干净。这些操作按顺序依次执行。
题目保证答案存在。注意:你无需最小化操作次数;只需找出一种在 n 次或更少操作内达成目标状态的方法即可。
输入格式
The first line contains integer number t (1≤t≤100) — the number of test cases in the input. Then t test cases follow.
The first line of a test case contains two integers n and k (1≤k≤2n,2≤n≤2000, n is even) — length of s and required number of regular prefixes.
The second line of a test case contains s of length n — the given bracket sequence. It contains only '(' and ')'.
It is guaranteed that there are exactly 2n characters '(' and exactly 2n characters ')' in the given string.
The sum of all values n over all the test cases in the input doesn't exceed 2000.
第一行包含一个整数 t(1≤t≤100)—— 输入中测试用例的数量。随后是 t 个测试用例。
每个测试用例的第一行包含两个整数 n 和 k(1≤k≤2n,2≤n≤2000,且 n 为偶数)—— 分别表示字符串 s 的长度以及所要求的“合法前缀”的数量。
每个测试用例的第二行包含一个长度为 n 的字符串 s —— 给定的括号序列,其中仅包含字符 '(' 和 ')'。
保证给定字符串中恰好有 2n 个字符 '(' 和 2n 个字符 ')'。
输入中所有测试用例的 n 值之和不超过 2000。
输出格式
For each test case print an answer.
In the first line print integer m (0≤m≤n) — the number of operations. You do not need to minimize m, any value is suitable.
In the following m lines print description of the operations, each line should contain two integers l,r (1≤l≤r≤n), representing single reverse operation of s[l…r]=slsl+1…sr. Operations are applied one after another sequentially.
The final s after all operations should be a regular, also it should be exactly k prefixes (including s) which are regular.
It is guaranteed that the answer exists. If there are several possible answers you can print any.
对每个测试用例输出一个答案。
第一行输出一个整数 m(0≤m≤n)—— 表示操作次数。你无需最小化 m,任意合法值均可。
接下来的 m 行描述这些操作,每行包含两个整数 l,r(1≤l≤r≤n),表示对子串 s[l…r]=slsl+1…sr 执行一次翻转操作。操作按顺序依次应用。
所有操作完成后,最终的字符串 s 必须是正规的(regular),且其恰好有 k 个前缀(包括 s 自身)是正规的。
题目保证答案存在。若存在多个可能的答案,输出任意一个即可。
输入输出样例
输入#1
4 8 2 ()(())() 10 3 ))()()()(( 2 1 () 2 1 )(
输出#1
4 3 4 1 1 5 8 2 2 3 4 10 1 4 6 7 0 1 1 2
说明/提示
In the first example, the final sequence is "()(()())", where two prefixes are regular, "()" and "()(()())". Note, that all the operations except "5 8" in the example output are useless (they do not change s).
在第一个例子中,最终序列为 "()(()())",其中有两个前缀是正规的,即 "()" 和 "()(()())"。注意,示例输出中除 "5 8" 外的所有操作都是无用的(它们不会改变 s)。
输入解题思路,AI测评打分。不知道怎么写?