qscqesze
Published on 2017-09-02 11:31 in 暂未分类 with qscqesze

Codeforces Round #594 (Div. 1) D2. The World Is Just a Programming Task (Hard Version) 括号序列 思维

D2. The World Is Just a Programming Task (Hard Version)

This is a harder version of the problem. In this version, 𝑛≤300000.

Vasya is an experienced developer of programming competitions' problems. As all great minds at some time, Vasya faced a creative crisis. To improve the situation, Petya gifted him a string consisting of opening and closing brackets only. Petya believes, that the beauty of the bracket string is a number of its cyclical shifts, which form a correct bracket sequence.

To digress from his problems, Vasya decided to select two positions of the string (not necessarily distinct) and swap characters located at this positions with each other. Vasya will apply this operation exactly once. He is curious what is the maximum possible beauty he can achieve this way. Please help him.

We remind that bracket sequence 𝑠 is called correct if:

𝑠 is empty;
𝑠 is equal to "(𝑡)", where 𝑡 is correct bracket sequence;
𝑠 is equal to 𝑡1𝑡2, i.e. concatenation of 𝑡1 and 𝑡2, where 𝑡1 and 𝑡2 are correct bracket sequences.
For example, "(()())", "()" are correct, while ")(" and "())" are not.

The cyclical shift of the string 𝑠 of length 𝑛 by 𝑘 (0≤𝑘<𝑛) is a string formed by a concatenation of the last 𝑘 symbols of the string 𝑠 with the first 𝑛−𝑘 symbols of string 𝑠. For example, the cyclical shift of string "(())()" by 2 equals "()(())".

Cyclical shifts 𝑖 and 𝑗 are considered different, if 𝑖≠𝑗.

Input

The first line contains an integer 𝑛 (1≤𝑛≤300000), the length of the string.

The second line contains a string, consisting of exactly 𝑛 characters, where each of the characters is either "(" or ")".

Output

The first line should contain a single integer — the largest beauty of the string, which can be achieved by swapping some two characters.

The second line should contain integers 𝑙 and 𝑟 (1≤𝑙,𝑟≤𝑛) — the indices of two characters, which should be swapped in order to maximize the string's beauty.

In case there are several possible swaps, print any of them.

Examples

input
10
()()())(()
output
5
8 7
input
12
)(()(()())()
output
4
5 10
input
6
)))(()
output
0
1 1

Note

In the first example, we can swap 7-th and 8-th character, obtaining a string "()()()()()". The cyclical shifts by 0,2,4,6,8 of this string form a correct bracket sequence.

In the second example, after swapping 5-th and 10-th character, we obtain a string ")(())()()(()". The cyclical shifts by 11,7,5,3 of this string form a correct bracket sequence.

In the third example, swap of any two brackets results in 0 cyclical shifts being correct bracket sequences.

题意

给你一个长度为n的括号序列,然后你可以选择交换两个位置,你需要使得能够变成 合法括号序列的起点最多。

题解

这道题我看题解看了一下午才看懂是怎么做的,这道题真的是一点代码都不考察,只考察智商。。。。

你需要依次理解下面的东西:

  1. 人尽皆知的东西:合法的括号序列是,令'('为1,')'为-1,那么前缀和需要>=0,且最后的总和应该为0.

  2. 假设现在已经是交换好的序列了,那么答案个数,就是前缀和的最小值的个数。

  3. 如果交换'('和')',会使得这个区间的每个数的前缀和-=2,不包括交换点。如果交换')'和'(',你可以改变交换为a[i]和a[j+n],将其变成交换'('和')'

  4. 因为我们使得这个区间里面的每个数都减去了2,那么最小值只可能是 区间内的2+区间外的0,或者区间内的1。

所以我们看两种情况,哪种最小即可,另外一个比较好思考的是,区间一定不会出现交叉的情况。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
int n;
string s;
int a[maxn],ps[maxn];
int main(){
	cin>>n>>s;
	for(int i=0;i<s.size();i++){
		int p = i+1;
		if(s[i]=='('){
			a[p]=a[p+n]=1;
		}else{
			a[p]=a[p+n]=-1;
		}
	}
	for(int i=1;i<=2*n;i++){
		ps[i]=ps[i-1]+a[i];
	}
	if(ps[n]){
		cout<<"0"<<endl;
		cout<<"1 1"<<endl;
		return 0;
	}
	int beg = min_element(ps+1,ps+1+n)-ps;
	int minv = ps[beg];
	for(int i=1;i<=2*n;i++){
		ps[i]-=minv;
	}
	int cl = -1, cc = 0;
	int mx = 0,ansl = 1,ansr = 1;
	int cl2 = 0, cc2 = 0;
	int mx2 = 0, ans2l = 1, ans2r = 1;
	for(int i=1;i<=n;i++){
		if(ps[i+beg]==1){
			if(cc>mx){
				mx=cc;
				ansl=cl,ansr=i;
			}
			cl=i+1;
			cc=0;
		}else if(ps[i+beg]==2)++cc;
		if(ps[i+beg]==0){
			if(cc2>mx2){
				mx2=cc2;
				ans2l=cl2+1,ans2r=i;
			}
			cl2=i;
			cc2=0;
		}else if(ps[i+beg]==1)++cc2;
	}
	for(int i=1;i<=n;i++){
		if(ps[i+beg]==0)++mx;
	}
	if(mx2>mx){
		mx=mx2;
		ansl=ans2l;
		ansr=ans2r;
	}
	cout<<mx<<endl;
	cout<<(ansl+beg-1+n)%n+1<<" "<<(ansr+beg-1+n)%n+1<<endl;
}
posted @ 2019-11-12 18:15  qscqesze  阅读(153)  评论(1编辑  收藏