Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- char c[200000];
- int b[200000];
- int main()
- {
- int n, mn = 1e9, ans = 0;
- char t;
- cin >> n;
- for (int i = 0; i < n; i++)
- {
- cin >> c[i];
- if (c[i] == '(') b[i] = b[max(0, i - 1)] + 1;
- else b[i] = b[max(0, i - 1)] - 1;
- mn = min(mn, b[i]);
- }
- if (mn < -1 || b[n - 1] > 1 || !b[n - 1] || (b[n - 1] ==1 && mn < 0)) return cout << 0, 0;
- if (b[n - 1] == 1) t = ')';
- else t = '(';
- if (t == '(')
- {
- for (int i = 0; i < n; i++)
- {
- if (c[i] != t) ans++;
- if (b[i] == -1) break;
- }
- }
- else
- {
- for (int i = n - 1; i > -1; i--)
- {
- if (!b[i]) break;
- if (c[i] != t) ans++;
- }
- }
- cout << ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement