Advertisement
senb1

krsu 3405 (55 points)

Mar 25th, 2023
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. /*
  2. by: senb1 aka amigo
  3. */
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. typedef unsigned long long ull;
  10. typedef long long ll;
  11. #define all(x) x.begin(), x.end()
  12. #define endl '\n'
  13.  
  14. const ll maxn = 50;
  15. const ll inf = 5e5 + 6;
  16. const ll mod = 2021;
  17.  
  18. int main() {
  19.     std::ios::sync_with_stdio(0);
  20.     std::cin.tie(0);
  21.  
  22.     string s;
  23.     cin >> s;
  24.     int b = 0, ans = 0, ind = 0;
  25.     for (int i = 0; i < s.size(); i++) {
  26.         if (s[i] == '(') {
  27.             b++;
  28.             // cout << "B++!! " << b << endl;
  29.         } else if (s[i] == ')') {
  30.             b--;
  31.             ind = i;
  32.         } else if (s[i] == 'X')
  33.             continue;
  34.         else
  35.             continue;
  36.         if (b < 0) {
  37.             // cout << "b < 0 " << b << ' ' << ind << endl;
  38.             int jj = 0;
  39.             for (int j = ind; j < s.size(); j++)
  40.                 if (s[j] == '(') {
  41.                     jj = j;
  42.                     break;
  43.                 }
  44.             while (jj > ind) {
  45.                 swap(s[jj], s[jj - 1]);
  46.                 jj--;
  47.                 ans++;
  48.             }
  49.             swap(s[jj], s[jj + 1]);
  50.         }
  51.         // cout << i << ' ' << b << ' ' << s << ' ' << ans << endl;
  52.     }
  53.     cout << ans << endl;
  54. }
  55. /*
  56. )()(
  57. )))(((
  58. ))()((
  59. )())((
  60. ()))((
  61. ())()(
  62. ()())(
  63. ()()()
  64. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement