• D. Bicolored RBS.


    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之后进行修改答案操作,那么无疑会导致我们多算了一个右括号

    1. #include
    2. using namespace std;
    3. int ans;
    4. string s;
    5. bool book[200000+10];
    6. int main()
    7. {
    8. int n;
    9. cin>>n;
    10. cin>>s;
    11. s=" "+s;
    12. int now=0;
    13. for(int i=1; i<=n; i++)
    14. {
    15. if(s[i]=='(')
    16. {
    17. now++;
    18. ans=max(ans,now);
    19. }
    20. else
    21. {
    22. ans=max(ans,now);
    23. now--;
    24. }
    25. }
    26. ans/=2;
    27. now=0;
    28. for(int i=1; i<=n; i++)
    29. {
    30. if(s[i]=='(')
    31. {
    32. now++;
    33. if(now<=ans)
    34. book[i]=1;
    35. }
    36. else
    37. {
    38. if(now<=ans)
    39. book[i]=1;
    40. now--;
    41. }
    42. }
    43. for(int i=1; i<=n; i++)
    44. {
    45. cout<
    46. }
    47. return 0;
    48. }

  • 相关阅读:
    Go缓冲通道和工作池
    如何搭建下载下来的机器学习深度学习的项目的环境?
    解决pyspark环境下GraphFrames报错问题
    系统集成|第十九章(笔记)
    计算机视觉40例之案例02人脸打码与解码
    解读AXI协议,做一个不被AXI折磨的芯片设计工程师
    Java 数组转集合
    less笔记
    LangChain 6根据图片生成推广文案HuggingFace中的image-caption模型
    2.5w字 + 36 张图爆肝操作系统面试题 学不吐你
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126332240