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();
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2016-11-24 Codeforces Round #381 (Div. 1) A. Alyona and mex 构造
2016-11-24 BZOJ 2648: SJY摆棋子 kdtree
2015-11-24 线段树 模板
2015-11-24 Codeforces Round #115 B. Plane of Tanks: Pro 水题
2015-11-24 Codeforces Round #115 A. Robot Bicorn Attack 暴力