C. Messy

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.

Input

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≤n2,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 n2 characters '(' and exactly n2 characters ')' in the given string.

The sum of all values n over all the test cases in the input doesn't exceed 2000.

Output

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.

Example

input

4

8 2

()(())()

10 3

))()()()((

2 1

()

2 1

)(

output

4

3 4

1 1

5 8

2 2

3

4 10

1 4

6 7

0

1

1 2

Note

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).

题意

给你一个长度为n的括号序列,恰好n/2个(,n/2个),你需要通过最多n次翻转操作,使得能够得到恰好k个合法括号前缀。

题解

首先因为能够翻转n次,所以任何序列我都能得到。

然后我只要构造出来就行。

假设要k个合法前缀,那么我前k-1个括号前缀通过()()()()()()构造,最后的一个为剩下的括号((((.....))))这样构造就可以了

代码

#include<bits/stdc++.h>
using namespace std;
int n,k;
string s;
void solve_swap(int x,int y){
while(x<y){
swap(s[x],s[y]);
x++,y--;
}
}
string get_str(int n,int k){
string tmp="";
for(int i=0;i<k-1;i++){
tmp+="(";
tmp+=")";
}
int len = n-tmp.size();
for(int i=0;i<len/2;i++){
tmp+="(";
}
for(int i=0;i<len/2;i++){
tmp+=")";
}
return tmp;
}
void solve(){
cin>>n>>k;
cin>>s;
vector<pair<int,int>>ans;
string final_str = get_str(n,k);
for(int i=0;i<s.size();i++){
if(s[i]!=final_str[i]){
for(int j=i+1;j<s.size();j++){
if(s[j]==final_str[i]){
solve_swap(i,j);
ans.push_back(make_pair(i+1,j+1));
break;
}
}
}
}
//cout<<s<<endl;
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++){
cout<<ans[i].first<<" "<<ans[i].second<<endl;
}
}
int main(){
int t;
scanf("%d",&t);
while(t--)solve();
}

Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C. Messy 构造的更多相关文章

  1. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3

    A,有多个线段,求一条最短的线段长度,能过覆盖到所又线段,例如(2,4)和(5,6) 那么我们需要4 5连起来,长度为1,例如(2,10)(3,11),用(3,10) 思路:我们想一下如果题目说的是最 ...

  2. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) F2. Wrong Answer on test 233 (Hard Version) dp 数学

    F2. Wrong Answer on test 233 (Hard Version) Your program fails again. This time it gets "Wrong ...

  3. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) E. Arson In Berland Forest 二分 前缀和

    E. Arson In Berland Forest The Berland Forest can be represented as an infinite cell plane. Every ce ...

  4. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) D2. Optimal Subsequences (Hard Version) 数据结构 贪心

    D2. Optimal Subsequences (Hard Version) This is the harder version of the problem. In this version, ...

  5. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) B. Box 贪心

    B. Box Permutation p is a sequence of integers p=[p1,p2,-,pn], consisting of n distinct (unique) pos ...

  6. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A. Math Problem 水题

    A. Math Problem Your math teacher gave you the following problem: There are n segments on the x-axis ...

  7. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C Messy

    //因为可以反转n次 所以可以得到任何可以构成的序列 #include<iostream> #include<string> #include<vector> us ...

  8. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) B Box

    #include<bits/stdc++.h> using namespace std; ]; ]; int main() { int total; cin>>total; w ...

  9. Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) A Math Problem

    //只要从所有区间右端点的最小值覆盖到所有区间左端点的最大值即可 #include<iostream> using namespace std ; int x,y; int n; int ...

随机推荐

  1. vue解惑之v-on(事件监听指令)

    一.v-on指令 vue中用v-on指令来监听DOM事件,并触发相应的代码.比如v-on:click,表示监听了点击事件. 二.事件修饰符 在事件处理函数中调用 event.preventDefaul ...

  2. OSG嵌入QT的简明总结

    目录 1.解决方案 2.存在问题 1) 警告提示 2) 多线程问题 3) 其他 1.解决方案 不得不说关于OSG的资料实在太零散了,搜索了很多关于OSG在QT下的解决方案,都是各有各的说法,有的说的不 ...

  3. Linux系统学习 九、日志、命令、身份鉴别、目录、文件查看、控制台终端、文件属性

    一.配置静态IP地址 输入ifconfig后没有配置IP地址,接下来进行手动配置. 输入以下命令进入IP配置文件进行配置   原始内容 进入vi后,输入i进入编辑状态,编辑完成后,按esc键退出编辑状 ...

  4. 如何通过 subprocess 持续获取输出内容

    在实际应用中会用到subprocess的Popen方法执行一些命令,而我们需要通过执行这个命令的来获取输出进行一些信息记录或者分析使用,如果是很快就可以执行完的那还好,有时需要持续跟踪内容的输出,比如 ...

  5. 解决Android Screen Monitor在android8.0及以上系统报错:"E/Screenshot: Unsupported protocol: 2"

    1.打开命令窗口,切换到 asm.jar 所在目录,执行 java -jar asm.jar,正常情况下打开后连接上设备会显示出画面 2.但是在android8.0以上系统该asm.jar包就无法正常 ...

  6. 【cf960G】G. Bandit Blues(第一类斯特林数)

    传送门 题意: 现在有一个人分别从\(1,n\)两点出发,包中有一个物品价值一开始为\(0\),每遇到一个价值比包中物品高的就交换两个物品. 现在已知这个人从左边出发交换了\(a\)次,从右边出发交换 ...

  7. String对象及正则表达式

    1,String和string还是有区别的,一个就是用双引号或单引号包括起来的数据就是字符串,另一个本质是数组多个字符串组成的只读字符数组: 2,你说string他是数组吧,他和数组还是有点区别的,他 ...

  8. JVM内存模型与类加载机制

    一. java虚拟机的内存模型如图: 补习一下jvm内存模型中的各个组成部分 堆: 我们new出来的对象全部放在堆中,他是jvm所能够动态分配的最大的一块空间 优点: 内存动态分配,生命周期不必事先告 ...

  9. ASP.NET Core部署系列二:发布到CentOS上

    前言: 在上一节中,通过一系列的步骤,已经将项目部署到IIS上,虽然遇到了一些问题,但最终解决并成功运行了.而在这一节中,将尝试通过linux系统的环境下,部署项目,实现Net Core跨平台的亮点. ...

  10. ASP.NET Core 2.2 WebApi 系列【四】集成Swagger

    Swagger 是一款自动生成在线接口文档+功能测试功能软件 一.安装程序包 通过管理 NuGet 程序包安装,搜索Swashbuckle.AspNetCore 二.配置 Swagger 将 Swag ...