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 = 't = int(input())\r\n\r\ns = ""\r\ng = []\r\n\r\ng = list(map(int, input().split()))\r\nfor ele in g:\r\n\tif ele == 1:\r\n\t\ts += \'(\'\r\n\telif ele == 2:\r\n\t\ts += \')\'\r\n\telif ele == 3:\r\n\t\ts += \'[\'\r\n\telse :\r\n\t\ts += \']\'\r\n\r\n#s = input()\r\n#t = len(s)\r\nstack = []\r\n#print(s)\r\ndeptho = 0\r\nif len(s) > 0:\r\n\tdeptho = 1\r\n\r\n\r\nd = {}\r\nd[0]  = deptho\r\nd[1]  = 0\r\nd[2] = 0\r\n\r\n#vis = [0]*n\r\nstack = []\r\ndepth = []\r\n\r\n\r\nfor i in range(t):\r\n\tif len(stack) == 0 and (s[i] == \'(\' or s[i] == \'[\'):\r\n\t\tstack.append(s[i])\r\n\t\tdepth.append(1)\r\n\telif (s[i] == \'(\' and stack[-1] == \'[\') or (s[i] == \'[\' and stack[-1] == \'(\'):\r\n\t\tstack.append(s[i])\r\n\t\tdepth.append(depth[-1]+1)\r\n\telif (s[i] == \'(\') or (s[i] == \'[\'):\r\n\t\tstack.append(s[i])\r\n\t\tdepth.append(depth[-1])\r\n\r\n\tdeptho = max(deptho, depth[-1])\r\n\r\n\tif s[i] == \')\' or s[i] == \']\':\r\n\t\tstack.pop()\r\n\t\tdepth.pop()\r\n\r\nd[0] = deptho\r\n\r\n\r\np_char = 0\r\nsq_char = 0\r\nmax_sq_char = 0\r\nmax_p_char = 0\r\np_depth = 0\r\nsq_depth = 0\r\n\r\nfor i in range(t):\r\n\tif(s[i] == \'(\'):\r\n\t\tp_depth += 1\r\n\tif(s[i] == \'[\'):\r\n\t\tsq_depth+=1\r\n\r\n\tif(p_depth):\r\n\t\tp_char+=1\r\n\tif(sq_depth):\r\n\t\tsq_char+=1\r\n\r\n\tmax_sq_char = max(max_sq_char, sq_char)\r\n\tmax_p_char = max(max_p_char, p_char)\r\n\r\n\tif(s[i] == \')\'):\r\n\t\tp_depth-=1\r\n\tif(s[i] == \']\'):\r\n\t\tsq_depth-=1\r\n\r\n\tif(p_depth == 0):\tp_char = 0\r\n\tif(sq_depth == 0):\tsq_char = 0\r\n\r\nd[1] = max_p_char\r\nd[2] = max_sq_char\r\nprint(d[0],d[1],d[2])\r\n\r\n\r\n\r\n'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 14175