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 ...
 
随机推荐
- FCC---CSS Flexbox: Apply the flex-direction Property to Create Rows in the Tweet Embed
			
The header and footer in the tweet embed example have child items that could be arranged as rows usi ...
 - C lang: The caracter reverse
			
Ax_Code #include<stdio.h> int main(void) { int i; char string[7] = {"mrsoft"}; char ...
 - django之跨站请求伪造csrf
			
目录 跨站请求伪造 csrf 钓鱼网站 模拟实现 针对form表单 ajax请求 csrf相关的两个装饰器 跨站请求伪造 csrf 钓鱼网站 就类似于你搭建了一个跟银行一模一样的web页面 , 用户在 ...
 - java全栈项目
			
文档地址:https://course.7yue.pro/lin/sleeve/ http://talelin.unna.com.cn/ 1.小程序里,我把结构分为三部分:wxml(view).pag ...
 - CCPC 2019 秦皇岛 Angle Beats
			
题目 给出P个点,然后给出Q个询问,问从P中选出两个点和给的点能组成直角三角形的方法个数.-O2,时间限制5秒. \[2\leqslant P\leqslant 2000,\qquad 1\leqsl ...
 - shadertoy使用教程
			
shadertoy使用教程 /** *常量定义 */ uniform vec3 iResolution; // 窗口分辨率,单位像素 uniform float iTime; // 程序运行的时间,单 ...
 - Linux系统目录结构知识
			
1.Linux目录结构: 逻辑上所有的目录只有一个顶点 /(根),所有目录的起点.根下面是一个类似倒挂的树的层次结构. 目录的结构和分区设备是没有关系的,也就是不同的目录可以跨越不同的磁盘设备或分区. ...
 - Dynamics CRM 2015/2016新特性之三十四:有了插件日志,调试插件so easy!
			
关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复217或者20160330可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...
 - 微信小程序——表单验证插件WxValidate的二次封装(二)
			
在上一篇博客<微信小程序——仿jqueryValidate表单验证插件WxValidate的二次封装>中,我将WxValidate做了再次封装,简化了初始规则数据的构造,但是当有错误时页面 ...
 - Linux Shell之监测磁盘空间
			
Linux Shell之监测磁盘空间 系统管理员的另一个重要任务就是监测系统磁盘的使用情况.不管运行的是简单Linux台式机还是大型的Linux服务器,我们都要知道还有多少空间可以留给应用程序.事实上 ...