senb1

krsu 3405 (not my code)

Mar 30th, 2023
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3.  
  4. #define ar array
  5. typedef long long ll;
  6.  
  7. signed main() {
  8.     ios::sync_with_stdio(0);
  9.     cin.tie(0);
  10.  
  11.     string s;
  12.     cin >> s;
  13.     int n = s.size();
  14.     queue<int> p;
  15.     for (int i = 0; i < n; i++) {
  16.         if (s[i] == '(')
  17.             p.push(i);
  18.     }
  19.  
  20.     int sum = 0, res = 0;
  21.     for (int i = 0; i < n; i++) {
  22.         if (s[i] == '(')
  23.             sum++, p.pop();
  24.         if (s[i] == ')')
  25.             sum--;
  26.         if (sum < 0) {
  27.             sum += 2;
  28.             for (int j = p.front() - 1; j >= i; j--) {
  29.                 swap(s[j], s[j + 1]);
  30.                 res++;
  31.             }
  32.             p.pop();
  33.         }
  34.     }
  35.  
  36.     cout << res << "\n";
  37. }
  38.  
  39. /*
  40.  
  41. */
  42.  
Advertisement
Add Comment
Please, Sign In to add comment