[Codeforces1239B]The World Is Just a Programming Task (Hard Version)

题意

给你一个长度为 n n 的括号序列 s s

定义一个能够完全匹配的括号序列为合法序列

定义 f ( s ) f(s) 表示有多少位置 i i 使得 s i . . . n s 1... i 1 s_{i...n}s_{1...i-1} 为合法序列

交换 s s 的任意两个字符,使得 f ( s ) f(s) 最大


题解

首先判断是否左括号数量是否与右括号相等

将原序列转变成一个合法的括号序列

找到一个累计右括号最多的地方,前后交换一下即可

记录每个左括号的位置和对应右括号的位置并给它们编号

C n t i Cnt_i 表示编号为 i i 的那一对括号里面有多少合法括号子列

特别的,记录 C n t 0 Cnt_0 表示不交换时有多少位置符合条件

考虑交换对答案的影响

①:交换编号为 p p 的括号对且这对括号不被任何其他括号包含

s 1 . . . s i ( t 1 . . . t j ) c 1 . . . c k s 1 . . . s i ) t 1 . . . t j ( c 1 . . . c k s_1...s_i(t_1...t_j)c_1...c_k\Rightarrow s_1...s_i)t_1...t_j(c_1...c_k

合法的位置有 ( c 1 . . . c k s 1 . . . s i ) t 1 . . . t j ; t j ( c 1 . . . c k s 1 . . . s i ) t 1 . . . t j 1 ; . . . ; t 1 . . . t j ( c 1 . . . c k s 1 . . . s i ) (c_1...c_ks_1...s_i)t_1...t_j;t_j(c_1...c_ks_1...s_i)t_1...t_{j-1};...;t_1...t_j(c_1...c_ks_1...s_i)

答案就是 C n t p + 1 Cnt_p+1 (其中 s , t , c s,t,c 均为不可分割的合法括号子序列)

②:交换编号为 p p 的括号对且这对括号只被包含一层

s 1 . . . s i ( t 1 . . . t a ( t a + 1 . . . t b ) t b + 1 . . . t j ) c 1 . . . c k s 1 . . . s i ( t 1 . . . t a ) t a + 1 . . . t b ( t b + 1 . . . t j ) c 1 . . . c k s_1...s_i(t_1...t_a(t_{a+1}...t_b)t_{b+1}...t_j)c_1...c_k\Rightarrow s_1...s_i(t_1...t_a)t_{a+1}...t_b(t_{b+1}...t_j)c_1...c_k

那么答案就是 C n t 0 + C n t p + 1 Cnt_0+Cnt_p+1

③:交换编号为 p p 的括号对且这对括号被包含的层数大于 1 1

一种简单的情况即 ( ( . . . ( t a . . . t b ) . . . ) ) ( ( . . . ) t a . . . t b ( . . . ) ) = ( t 1 t a . . . t b t k ) ((...(t_a...t_b)...))\Rightarrow ((...)t_a...t_b(...))=(t_1t_a...t_bt_k)

可以发现这种交换答案是不会改变的

④:交换不是相对应的括号

如果是交换最外层的括号即 ( . . . ) t 1 . . . t j ( . . . ) ( . . . ( t 1 . . . t j ) . . . ) (...)t_1...t_j(...)\Rightarrow(...(t_1...t_j)...) ,答案会减小;甚至变为 1 1 ,如 s ( t ) c s ) t ( c s(t)c\Rightarrow s)t(c

如果跨越包含自己的括号交换,一种简单情况即 ( ( a ) ) ( b ) ( ) a ) ) ( b ( ((a))(b)\Rightarrow()a))(b( ,答案会变成 1 1 ;其他情况也同理

如果是交换同一层(不是最外层)之间的括号,则答案不会改变,因为不能改变最外层的括号匹配

综上可以分析发现,只有操作①②可能会使得答案变大的,所以只需要考虑这两种情况即可

时间复杂度 O ( n ) O(n)

#include<bits/stdc++.h>
#define fp(i,a,b) for(register int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(register int i=a,I=b-1;i>I;--i)
#define go(u) for(int i=fi[u],v=e[i].to;i;v=e[i=e[i].nx].to)
#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
const int N=3e5+5;
typedef int arr[N];
int n,sL=1,sR=1,Mid,Min,Num,Top,Ans;
arr L,R,fa,stk,Cnt;
char c[N],s[N];
int main(){
	#ifndef ONLINE_JUDGE
		file("s");
	#endif
	scanf("%d\n",&n);gets(c+1);
	fp(i,1,n){
		c[i]=='('?++Top:--Top;
		if(Top<Min)Min=Top,Mid=i;
	}
	if(Top)return puts("0\n1 1"),0;
	fp(i,Mid+1,n)s[i-Mid]=c[i];
	fp(i,1,Mid-1)s[i+n-Mid]=c[i];
	fp(i,1,n)
		if(s[i]=='(')stk[++Top]=++Num,L[Num]=i;
		else R[stk[Top]]=i,fa[stk[Top]]=stk[Top-1],++Cnt[stk[--Top]];
	Ans=Cnt[0];
	fp(i,1,Num)
		if(Cnt[i]+1>Ans&&!fa[i])Ans=Cnt[i]+1,sL=L[i],sR=R[i];
		else if(Cnt[0]+Cnt[i]+1>Ans&&fa[i]&&!fa[fa[i]])Ans=Cnt[0]+Cnt[i]+1,sL=L[i],sR=R[i];
	(sL+=Mid)>n?sL-=n:0,(sR+=Mid)>n?sR-=n:0;
	printf("%d\n%d %d",Ans,sL,sR);
return 0;
}

猜你喜欢

转载自blog.csdn.net/BeNoble_/article/details/102672972
今日推荐