contest: Codeforces Round #653 (Div. 3), problem: (C) Move Brackets, Accepted

 

  1. import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
  2. # import time,random,resource
  3.  
  4. sys.setrecursionlimit(10**7)
  5. inf = 10**20
  6. eps = 1.0 / 10**10
  7. mod = 10**9+7
  8. mod2 = 998244353
  9. dd = [(-1,0),(0,1),(1,0),(0,-1)]
  10. ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
  11.  
  12. def LI(): return list(map(int, sys.stdin.readline().split()))
  13. def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
  14. def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
  15. def LF(): return [float(x) for x in sys.stdin.readline().split()]
  16. def LS(): return sys.stdin.readline().split()
  17. def I(): return int(sys.stdin.readline())
  18. def F(): return float(sys.stdin.readline())
  19. def S(): return input()
  20. def pf(s): return print(s, flush=True)
  21. def pe(s): return print(str(s), file=sys.stderr)
  22. def JA(a, sep): return sep.join(map(str, a))
  23. def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)
  24.  
  25.  
  26. def main():
  27. t = I()
  28.  
  29. rr = []
  30. for _ in range(t):
  31. n = I()
  32. s = S()
  33.  
  34. t = 0
  35. r = 0
  36. for c in s:
  37. if c == "(":
  38. t += 1
  39. else:
  40. t -= 1
  41. if t < 0:
  42. r += 1
  43. t = 0
  44.  
  45. rr.append(r)
  46.  
  47.  
  48. return JA(rr, "\n")
  49.  
  50.  
  51. print(main())
  52.  
  53.  

Comments

Popular Posts