D. Bicolored RBS
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
A string is called bracket sequence if it does not contain any characters other than "(" and ")". A bracket sequence is called regular (shortly, RBS) if it is possible to obtain correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, "", "(())" and "()()" are RBS and ")(" and "(()" are not.
We can see that each opening bracket in RBS is paired with some closing bracket, and, using this fact, we can define nesting depth of the RBS as maximum number of bracket pairs, such that the 22 -nd pair lies inside the 11 -st one, the 33 -rd one — inside the 22 -nd one and so on. For example, nesting depth of "" is 00 , "()()()" is 11 and "()((())())" is 33 .
Now, you are given RBS ss of even length nn . You should color each bracket of ss into one of two colors: red or blue. Bracket sequence rr , consisting only of red brackets, should be RBS, and bracket sequence, consisting only of blue brackets bb , should be RBS. Any of them can be empty. You are not allowed to reorder characters in ss , rr or bb . No brackets can be left uncolored.
Among all possible variants you should choose one that minimizes maximum of rr 's and bb 's nesting depth. If there are multiple solutions you can print any of them.
Input
The first line contains an even integer nn (2≤n≤2⋅1052≤n≤2⋅105 ) — the length of RBS ss .
The second line contains regular bracket sequence ss (|s|=n|s|=n , si∈{si∈{ "(", ")"}} ).
Output
Print single string tt of length nn consisting of "0"-s and "1"-s. If titi is equal to 0 then character sisi belongs to RBS rr , otherwise sisi belongs to bb .
Examples
Input
Copy
2 ()
Output
Copy
11
Input
Copy
4 (())
Output
Copy
0101
Input
Copy
10 ((()())())
Output
Copy
0110001111
Note
In the first example one of optimal solutions is s=s= "()() ". rr is empty and b=b= "()() ". The answer is max(0,1)=1max(0,1)=1 .
In the second example it's optimal to make s=s= "(())(()) ". r=b=r=b= "()() " and the answer is 11 .
In the third example we can make s=s= "((()())())((()())()) ". r=r= "()()()() " and b=b= "(()())(()()) " and the answer is 22 .
=========================================================================
题目所说的“最大纵深长度”一直没有理解明白,最后发现递推方式竟然是遇见左就加,否则就减,然后取最大值。
然后为了使两段的最大纵深的最大值最小,就应该让两者更接近最大纵深的一半。
我们把depth/2作为第一端的极限,遇见左括号我们加,如果这时候dep<=depth/2,我们继续加进去就行,如果是右括号,则需要把dep-1,而减1操作必须在更新答案之后,否则一个样例也过不了。原因是我们会遇到一段连续的左括号,这让我们目前的纵深大于depth/2,我们遇到右括号会有一个减纵深的过程,当我们减完这次才到达depth/2以内的时候,实际上我们这一个是多出来的
例如 ( ( ) )
depth/2=1
遍历第一个 ,加进去
第二个不加
第三个,-1,如果在减1之后进行修改答案操作,那么无疑会导致我们多算了一个右括号
#include <bits/stdc++.h>
using namespace std;
int ans;
string s;
bool book[200000+10];
int main()
{
int n;
cin>>n;
cin>>s;
s=" "+s;
int now=0;
for(int i=1; i<=n; i++)
{
if(s[i]=='(')
{
now++;
ans=max(ans,now);
}
else
{
ans=max(ans,now);
now--;
}
}
ans/=2;
now=0;
for(int i=1; i<=n; i++)
{
if(s[i]=='(')
{
now++;
if(now<=ans)
book[i]=1;
}
else
{
if(now<=ans)
book[i]=1;
now--;
}
}
for(int i=1; i<=n; i++)
{
cout<<book[i];
}
return 0;
}