Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bits/stdc++.h"
- using namespace std;
- #define ar array
- typedef long long ll;
- signed main() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- string s;
- cin >> s;
- int n = s.size();
- queue<int> p;
- for (int i = 0; i < n; i++) {
- if (s[i] == '(')
- p.push(i);
- }
- int sum = 0, res = 0;
- for (int i = 0; i < n; i++) {
- if (s[i] == '(')
- sum++, p.pop();
- if (s[i] == ')')
- sum--;
- if (sum < 0) {
- sum += 2;
- for (int j = p.front() - 1; j >= i; j--) {
- swap(s[j], s[j + 1]);
- res++;
- }
- p.pop();
- }
- }
- cout << res << "\n";
- }
- /*
- */
Advertisement
Add Comment
Please, Sign In to add comment