Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vlli vector<ll>
#define vi vector<int>
#define vs vector<string>
#define vplli vector<pair<ll, ll>>
#define plli pair<ll, ll>
#define vps vector<pair<string, string>>
#define vpi vector<pair<int, int>>
#define all(x) x.begin(), x.end()
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define forn(i, a, n) for (ll i = a; i < n; i++)
#define scan(arr, a, n)                                                        \\
  for (ll i = (a); i < (n); i++)                                               \\
    cin >> (arr)[i];
#define print(arr, a, n)                                                       \\
  for (ll i = (a); i < (n); i++)                                               \\
    cout << (arr)[i] << " ";

ll add(ll x, ll y) {
  ll res = x + y;
  return (res >= mod ? res - mod : res);
}
ll mul(ll x, ll y) {
  ll res = x * y;
  return (res >= mod ? res % mod : res);
}
ll sub(ll x, ll y) {
  ll res = x - y;
  return (res < 0 ? res + mod : res);
}
ll power(ll x, ll y) {
  ll res = 1;
  x %= mod;
  while (y) {
    if (y & 1)
      res = mul(res, x);
    y >>= 1;
    x = mul(x, x);
  }
  return res;
}
ll mod_inv(ll x) { return power(x, mod - 2); }

int main() {
  fast;
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll ans = 0, x = 0, y = n, f = 0, r = 0, w = 0;
  forn(i, 0, n) {
    if (s[i] == 'R')
      r++;
    else
      w++;
  }
  if (r == 0 || w == 0) {
    cout << "0" << endl;
    return 0;
  }

  forn(i, 0, n) {
    if (i >= y) {
      break;
    }
    if (s[i] == 'W') {
      for (ll j = y - 1; j >= 0; j--) {
        if (s[j] == 'R') {
          if (j > i) {
            // cout<<i<<" "<<j<<endl;
            y = j;
            // swap(s[i], s[j]);
            ans++;
            break;

          } else {
            f = 1;
            break;
          }
        }
      }
      if (f) {
        break;
      }
    }
  }
  // cout<<s<<endl;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vlli vector<ll>
#define vi vector<int>
#define vs vector<string>
#define vplli vector<pair<ll, ll>>
#define plli pair<ll, ll>
#define vps vector<pair<string, string>>
#define vpi vector<pair<int, int>>
#define all(x) x.begin(), x.end()
#define fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define forn(i, a, n) for (ll i = a; i < n; i++)
#define scan(arr, a, n)                                                        \\
  for (ll i = (a); i < (n); i++)                                               \\
    cin >> (arr)[i];
#define print(arr, a, n)                                                       \\
  for (ll i = (a); i < (n); i++)                                               \\
    cout << (arr)[i] << " ";

ll add(ll x, ll y) {
  ll res = x + y;
  return (res >= mod ? res - mod : res);
}
ll mul(ll x, ll y) {
  ll res = x * y;
  return (res >= mod ? res % mod : res);
}
ll sub(ll x, ll y) {
  ll res = x - y;
  return (res < 0 ? res + mod : res);
}
ll power(ll x, ll y) {
  ll res = 1;
  x %= mod;
  while (y) {
    if (y & 1)
      res = mul(res, x);
    y >>= 1;
    x = mul(x, x);
  }
  return res;
}
ll mod_inv(ll x) { return power(x, mod - 2); }

int main() {
  fast;
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll ans = 0, x = 0, y = n, f = 0;
  forn(i, 0, n) {
    if (i >= y) {
      break;
    }
    if (s[i] == 'W') {
      for (ll j = y - 1; j >= 0; j--) {
        if (s[j] == 'R') {
          if (j > i) {
            // cout<<i<<" "<<j<<endl;
            y = j;
            // swap(s[i], s[j]);
            ans++;
            break;

          } else {
            f = 1;
            break;
          }
        }
      }
      if (f) {
        break;
      }
    }
  }
  // cout<<s<<endl;
  cout << ans << endl;
}