CF1262C Messy

题意简述:

给定一个长度为$n$的括号序列 保证一半左括号一半右括号 每次可以指定一个区间反转 假设进过若干次反转以后 对于括号序列的每一个前缀 一共用$k$个合法(包含序列本身)那么符合题意 求一个长度任意的操作序列(长度小于等于$n$)在经过操作后使得该括号序列符合题意

题解:

现在就是后悔 非常后悔 首先因为不需要保证长度最小 只需要合法就行可以想到构造 其实构造也是比较好想的 首先类似$((()))$的序列$k$肯定是1 然后假如$k$大于1 我们可以每次在后面多放一组括号 比如$k=3$ 就可以构造$((()))()()$ 其实这时候题目基本已经做出来了 但是我emmm...被翻转字符串搞蒙了然后遗 憾 离 场 其实翻转字符串就是一个伪装 因为题保证有解所以一定一半左括号一半右括号 我们先构造一个答案序列 每次发现不相等我们就从原串后面找一个相等的然后把这一段暴力反转 然后继续判断就行 不需要考虑过多翻转的性质

AC代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#define ll long long
#define F(i,a,b) for(int i=(a);i<=(b);i++)
#define mst(a,b) memset((a),(b),sizeof(a))
#define PII pair<int,int>
using namespace std;
template<class T>inline void read(T &x) {
    x=0; int ch=getchar(),f=0;
    while(ch<'0'||ch>'9'){if (ch=='-') f=1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    if(f)x=-x;
}
const int inf=0x3f3f3f3f;
const int maxn=2050;
int T;
int n,k;
char s[maxn],ans[maxn],temp[maxn];
vector<PII> res;
void calc(int x,int y)
{
	for(int i=x;i<=y;i++) temp[i]=s[i];
	for(int i=x;i<=y;i++) s[i]=temp[y-i+x];
}
int main()
{
	cin>>T;
	while(T--)
	{
		int cnt=0;
		res.clear();
		cin>>n>>k;
		cin>>(s+1);
		k--;
		for(int i=1;i<=n-2*k;i++)
		{
			if(i<=(n-2*k)/2) ans[i]='(';
			else ans[i]=')';
		}
		int pan=1;
		for(int i=n-2*k+1;i<=n;i++)
		{
			if(pan)
			{
				ans[i]='(';
				pan=0;
			}
			else
			{
				ans[i]=')';
				pan=1;
			}
		}
		for(int i=1;i<=n;i++)
		{
			if(s[i]!=ans[i])
			{
				for(int j=i+1;j<=n;j++)
				{
					if(s[j]==ans[i]) 
					{
						cnt++;
						calc(i,j);
						res.push_back(make_pair(i,j));
						break;
					}
				}
			}
		}
		cout<<cnt<<endl;
		for(int i=0;i<res.size();i++) cout<<res[i].first<<" "<<res[i].second<<endl;
	}
	return 0;
}