Advertisement
savrasov

Untitled

Apr 10th, 2017
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. char c[200000];
  7. int b[200000];
  8.  
  9. int main()
  10. {
  11. int n, mn = 1e9, ans = 0;
  12. char t;
  13. cin >> n;
  14. for (int i = 0; i < n; i++)
  15. {
  16. cin >> c[i];
  17. if (c[i] == '(') b[i] = b[max(0, i - 1)] + 1;
  18. else b[i] = b[max(0, i - 1)] - 1;
  19. mn = min(mn, b[i]);
  20. }
  21. if (mn < -1 || b[n - 1] > 1 || !b[n - 1] || (b[n - 1] ==1 && mn <= 0)) return cout << 0, 0;
  22. if (b[n - 1] == 1) t = ')';
  23. else t = '(';
  24. if (t == '(')
  25. {
  26. for (int i = 0; i < n; i++)
  27. {
  28. if (c[i] != t) ans++;
  29. if (b[i] == -1) break;
  30. }
  31. }
  32. else
  33. {
  34. for (int i = n - 1; i > -1; i--)
  35. {
  36. if (!b[i]) break;
  37. if (c[i] != t) ans++;
  38.  
  39. }
  40. }
  41. cout << ans;
  42. return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement