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

题意

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

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

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

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


题解

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

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

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

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

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

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

考虑交换对答案的影响

①:交换编号为 p 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 s1...si(t1...tj)c1...cks1...si)t1...tj(c1...ck

合法的位置有 ( 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) (c1...cks1...si)t1...tj;tj(c1...cks1...si)t1...tj1;...;t1...tj(c1...cks1...si)

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

②:交换编号为 p 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 s1...si(t1...ta(ta+1...tb)tb+1...tj)c1...cks1...si(t1...ta)ta+1...tb(tb+1...tj)c1...ck

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

③:交换编号为 p p p的括号对且这对括号被包含的层数大于 1 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) ((...(ta...tb)...))((...)ta...tb(...))=(t1ta...tbtk)

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

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

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

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

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

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

时间复杂度 O ( n ) 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值