Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C. Messy 构造
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 构造的更多相关文章
- 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) 思路:我们想一下如果题目说的是最 ...
- 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 ...
- 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 ...
- 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, ...
- 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 ...
- 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 ...
- Codeforces Round #602 (Div. 2, based on Technocup 2020 Elimination Round 3) C Messy
//因为可以反转n次 所以可以得到任何可以构成的序列 #include<iostream> #include<string> #include<vector> us ...
- 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 ...
- 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 ...
随机推荐
- 关于js的for in循环,慎用
参考:http://www.cftea.com/c/2014/08/6290.asp作者:vkvi 如题我看到也有点诧异,测试了真的有这个问题,上代码 Array.prototype.a = func ...
- echarts 双Y轴图表
直接代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- react-router刷新页面Cannot GET 问题
最近在做项目的时候遇到了如下错误 并在控制台看到了如下的报错 我先是按照控制台的错误搜索,得出的结果都是对meta头部进行设置,允许资源请求,但是问题依然没有解决,偶然间改变了想法,会不会是路由的问题 ...
- 编译原理之DFA最小化,语法分析初步
1.将DFA最小化: 状态转换图: 识别语言:b*ac*(da)*bb* 2.构造以下文法相应的最小的DFA S→ 0A|1B A→ 1S|1 B→0S|0 (1)正规式: S -> 0(1S+ ...
- centos7在线yum安装mysql时官方镜像下载过慢的解决方案
帮客户调试数据库,搭建一测试环境,centos7最小化安装后,在线安装mysql. 步骤: 1. wget -i http://dev.mysql.com/get/mysql57-community- ...
- Ubuntu系统修改资源为阿里云镜像
一般都会推荐使用国内的镜像源,比如163或者阿里云的镜像服务器将下列文本添加到/etc/apt/sources.list文件里 deb http://mirrors.aliyun.com/ubuntu ...
- jQuery-跨域问题的处理
调用登录接口时,后端一般会在调用登录接口成功后,在response中设置cookie,之后前端的每次请求都会自动地在请求头上加上后端设置好的cookie,这对前端来说是透明的. 当登录接口与登录后调用 ...
- ArrayList和LinkedList介绍
java.util.ArrayList集合的数据存储结构是数组,且是多线程,元素增删慢,查找快, 由于日常使用开发大多数为查询数据,遍历数据,所以ArrayList是最常用的集合.上一节已写了. ja ...
- emojy表情的小问题
报错内容:在emojy表情入库或者更新库的时候,会报这个错:java.sql.SQLException:Incorrect string value: '\xF0\x9F\x99\x8F\..' fo ...
- MVC过滤器:过滤器执行顺序
如果某个Action过滤器运用了多种过滤器,那么过滤器的执行顺序是如何呢? 规则一:不同类型的过滤器有一个先后顺序 即执行顺序是:授权过滤器->动作过滤器->结果过滤器->异常过滤器 ...