Problem p02597 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int(i) = (n); (i) >= (0); (i)--)
#define SIZE(x) ((int)(x).size())
#define GCD(a, b)                                                              \
  int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
#define QP(a, b)                                                               \
  int qp(int a, ll b) {                                                        \
    int ans = 1;                                                               \
    do {                                                                       \
      if (b & 1)                                                               \
        ans = 1ll * ans * a % mo;                                              \
      a = 1ll * a * a % mo;                                                    \
    } while (b >>= 1);                                                         \
    return ans;                                                                \
  }
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define all(v) (v).begin(), (v).end()

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
template <typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

typedef vector<int, int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1LL << 60;
const ll MOD = 1000000007LL;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int sum = 0;
  string s;
  cin >> s;
  int r = s.rfind("R");
  int w = s.find("W");
  while (r != -1 && w != -1 && r > w) {
    sum++;
    int tmp = s[r];
    s[r] = s[w];
    s[w] = tmp;
    r = s.rfind("R");
    w = s.find("W");
  }
  cout << sum;
  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int(i) = (n); (i) >= (0); (i)--)
#define SIZE(x) ((int)(x).size())
#define GCD(a, b)                                                              \
  int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
#define QP(a, b)                                                               \
  int qp(int a, ll b) {                                                        \
    int ans = 1;                                                               \
    do {                                                                       \
      if (b & 1)                                                               \
        ans = 1ll * ans * a % mo;                                              \
      a = 1ll * a * a % mo;                                                    \
    } while (b >>= 1);                                                         \
    return ans;                                                                \
  }
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define all(v) (v).begin(), (v).end()

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
template <typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

typedef vector<int, int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1LL << 60;
const ll MOD = 1000000007LL;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int sum = 0;
  string s;
  cin >> s;
  int num_r = count(all(s), 'R');
  for (int i = 0; i < num_r; i++) {
    if (s[i] == 'W')
      sum++;
  }
  cout << sum;
  cout << endl;
  return 0;
}