Zonal Computing Olympiad 2012, 26 Nov 2011

We consider sequences of opening and closing brackets with two types of brackets, () and [].  A bracket sequence is well-bracketed if we can pair up each opening bracket with a matching closing bracket in the usual sense.  For instance, the sequences (), [] ([]) and []([]) are well-bracketed, while (, ()], (], )( and [(]) are not well-bracketed.  In the last case, each opening bracket has a matching closing bracket and vice versa, but the intervals spanned by the different types of brackets intersect each other instead of being contained one within the other.

The alternating depth of a well-bracketed sequence tells us the maximum number of times we switch between the two types of brackets when we have inner matched brackets enclosed within outer matched brackets.  For instance, the alternating depth of (), [[[]]] and ()[][] is 1, the alternating depth of [()] and ()([]) is 2, the alternating depth of ([()]) and [()][(([]))] is 3, and so on.

Given a well-bracketed sequence, we are interested in computing three quantities.
- The alternating depth of the sequence.
- The maximum number of symbols between any pair of matched brackets of the type ( and ), including both the outer brackets.
- The maximum number of symbols between any pair of matched brackets of the type [ and ], including both the outer brackets.

For instance, the alternating depth of (([]))[[[()]]] is 2, the maximum number of symbols between a matched pair () is 6 and the maximum number of symbols between a matched pair [] is 8.



-----Input format-----
The input consists of two lines.  The first line is a single integer N, the length of the bracket sequence. Positions in the sequence are numbered 1,2,…,N. The second line is a sequence of N space-separated integers that encode the bracket expression as follows: 1 denotes an opening bracket (, 2 denotes a closing bracket ), 3 denotes an opening bracket [ and 4 denotes a closing bracket ].  Nothing other than 1, 2, 3 or 4 appears in the second line of input and the corresponding expression is guaranteed to be well-bracketed.

-----Output format-----
Your program should print 3 space-separated integers in a line, denoting the three quantities asked for in the following order: alternating depth, length of the maximum sequence between matching () brackets and length of the maximum sequence between matching [] brackets.  

-----Testdata-----
You may assume that 2 ≤ N ≤ 105.  In 30% of the test cases, 2 ≤ N ≤ 103.

- Subtask 1 (30 marks)
- Subtask 2 (70 marks)

-----Sample Input-----
14
1 1 3 4 2 2 3 3 3 1 2 4 4 4

-----Sample Output-----
2 6 8

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

ns = {"__name__": "__main__"}
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = 'class Stack:\r\n\tdef __init__(self):\r\n\t\tself.items=[]\r\n\t\r\n\tdef empty(self):\r\n\t\treturn\tself.items==[]\r\n\t\r\n\tdef push(self,item):\r\n\t\tself.items.append(item)\r\n\t\r\n\tdef pop(self):\r\n\t\treturn self.items.pop()\r\n\t\r\n\tdef top(self):\r\n\t\treturn self.items[-1]\r\n\t\r\n\tdef size(self):\r\n\t\treturn len(self.items)\r\n\tdef __iter__(self):\r\n\t\treturn self\r\n\tdef __next__(self):\r\n\t\treturn _StackIterator(list(self.items()))\r\n\t\t\r\nclass _StackIterator:\r\n\tdef __init__(self,stack):\r\n\t\tself.stackRef=stack\r\n\t\tself.curInd=0\r\n\tdef __iter__(self):\r\n\t\treturn self\r\n\tdef __next__(self):\r\n\t\tif self.curInd<self.size():\r\n\t\t\tentry=self.stackRef[self.curInd]\r\n\t\t\tself.curInd+=1\r\n\t\t\treturn entry\r\n\t\telse:\r\n\t\t\traise StopIteration\r\ndef main():\r\n\t\r\n\tn=int(input())\r\n\tSymbol=Stack()\r\n\tIndex=Stack()\r\n\tFirstBracket=ThirdBracket=0\r\n\tarr=list(map(int,input().split()))\r\n\t\r\n\tfor i in range(n):\r\n\t\tif arr[i]==1 or arr[i]==3:\r\n\t\t\tSymbol.push(arr[i])\r\n\t\t\tIndex.push(i)\r\n\t\telse:\r\n\t\t\ttemp=Symbol.pop()\r\n\t\t\tif temp==1:\r\n\t\t\t\tFirstBracket=max(FirstBracket,i-Index.pop()+1)\r\n\t\t\telif temp==3:\r\n\t\t\t\tThirdBracket=max(ThirdBracket,i-Index.pop()+1)\r\n\tseq=Stack()\r\n\tAlterDepth=0\r\n\tcurDepth=Stack()\r\n\tfor i in range(n):\r\n\t\tif (arr[i]==1 or arr[i]==3) and seq.empty():\r\n\t\t\tcurDepth.push(1)\r\n\t\t\tseq.push(arr[i])\r\n\t\t\tAlterDepth=max(AlterDepth,curDepth.top())\r\n\t\t\t\r\n\t\telif arr[i]==1 and seq.top()==3:\r\n\t\t\tcurDepth.push(curDepth.top()+1)\r\n\t\t\tseq.push(arr[i])\r\n\t\t\tAlterDepth=max(AlterDepth,curDepth.top())\r\n\t\t\t\r\n\t\telif arr[i]==3 and seq.top()==1:\r\n\t\t\tcurDepth.push(curDepth.top()+1)\r\n\t\t\tseq.push(arr[i])\r\n\t\t\tAlterDepth=max(AlterDepth,curDepth.top())\r\n\t\t\r\n\t\telif (arr[i]==1 or arr[i]==3):\r\n\t\t\tcurDepth.push(curDepth.top())\r\n\t\t\tseq.push(arr[i])\r\n\t\t\tAlterDepth=max(curDepth.top(),AlterDepth)\r\n\t\t\r\n\t\telif arr[i]==2 or arr[i]==4:\r\n\t\t\tcurDepth.pop()\r\n\t\t\tseq.pop()\r\n\tprint(AlterDepth,FirstBracket,ThirdBracket)\r\n\t\t\t\t\r\n\t\r\ndef __starting_point():\r\n\t\r\n\tmain()\r\n\n__starting_point()'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 22471