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

/**
 * author: BueVonHun 🐼🎋
 * More Info: https://github.com/Bue-von-hon
 **/

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <vector>

typedef long long ll;
using namespace std;

#define fast_cin()                                                             \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n'
#define m_p make_pair
#define pii pair<int, int>
#define piii pair<int, pii>
#define vt vector
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()

template <class T> T _min(T a, T b) { return (a < b ? a : b); }
template <class T> T _max(T a, T b) { return (a > b ? a : b); }
template <class T> void read(T &x) { cin >> x; }

int test_case;
int main(void) {
  fast_cin();

  read(test_case);
  deque<char> v;
  int Rcount = 0;
  for (int i = 0; i < test_case; i++) {
    char ch;
    cin >> ch;
    v.push_back(ch);
    if (ch == 'R')
      Rcount++;
  }
  int size = v.size();
  int count = 0;
  int pos = 0, rear = size - 1;
  for (int i = size - 1; i >= 0; i--) {
    if (v[i] == 'R') {
      rear = i;
      break;
    }
  }
  while (true) {
    if (pos >= rear || Rcount == 0)
      break;
    if (v[pos] == 'W') {
      swap(v[pos], v[rear]);
      pos++;
      count++;
      Rcount--;
      for (int i = rear; i >= 0; i--) {
        if (v[i] == 'R') {
          rear = i;
          break;
        }
      }
    } else
      pos++;
  }
  cout << count << endl;
  return 0;
}


Error: TLE

Fixed submission:

/**
 * author: BueVonHun 🐼🎋
 * More Info: https://github.com/Bue-von-hon
 **/

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <vector>

typedef long long ll;
using namespace std;

#define fast_cin()                                                             \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define endl '\\n'
#define m_p make_pair
#define pii pair<int, int>
#define piii pair<int, pii>
#define vt vector
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()

template <class T> T _min(T a, T b) { return (a < b ? a : b); }
template <class T> T _max(T a, T b) { return (a > b ? a : b); }
template <class T> void read(T &x) { cin >> x; }

int test_case;
int main(void) {
  fast_cin();

  read(test_case);
  deque<char> v;
  for (int i = 0; i < test_case; i++) {
    char ch;
    cin >> ch;
    v.push_back(ch);
  }
  int size = v.size();
  int count = 0;
  int pos = 0, rear = size - 1;
  for (int i = size - 1; i >= 0; i--) {
    if (v[i] == 'R') {
      rear = i;
      break;
    }
  }
  while (true) {
    if (pos >= rear)
      break;
    if (v[pos] == 'W') {
      swap(v[pos], v[rear]);
      pos++;
      count++;
      for (int i = rear; i >= 0; i--) {
        if (v[i] == 'R') {
          rear = i;
          break;
        }
      }
    } else
      pos++;
  }
  cout << count << endl;
  return 0;
}