alexjercan/bugnet huggingface dataset:
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const int maxn = 45 + 10;
const int mod = 1e9 + 7;

using namespace std;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n, ans = 0;
  string s;

  cin >> n >> s;

  set<int> r, w;

  for (int i = 0; i < n; ++i) {
    if (s[i] == 'R')
      r.insert(i);
    else
      w.insert(i);
  }

  ans = min(w.size(), r.size());

  int moves = 0;

  while (!r.empty() && !w.empty() && *(w.begin()) < *(r.rbegin())) {
    ++moves;
    int x = *(w.begin());
    w.erase(x);
    r.insert(x);
    x = *(r.rbegin());
    r.erase(x);
    w.insert(x);
  }

  ans = min(ans, moves);

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const int maxn = 45 + 10;
const int mod = 1e9 + 7;

using namespace std;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n, ans = 0;
  string s;

  cin >> n >> s;

  set<int> r, w;

  for (int i = 0; i < n; ++i) {
    if (s[i] == 'R')
      r.insert(i);
    else
      w.insert(i);
  }

  ans = min(w.size(), r.size());

  int moves = 0;

  while (!w.empty() && *(w.begin()) < *(r.rbegin())) {
    ++moves;
    int x = *(w.begin());
    w.erase(x);
    r.insert(x);
    x = *(r.rbegin());
    r.erase(x);
    w.insert(x);
  }

  ans = min(ans, moves);

  cout << ans << endl;

  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int a;
  string s;
  cin >> a >> s;
  cout << count(s.begin(), s.begin() + count(s.begin(), s.end(), 'R'), 'W');
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int a;
  string s;
  scanf("%d %s", &a, &s);
  cout << count(s.begin(), s.begin() + count(s.begin(), s.end(), 'R'), 'W');
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repi(i, 0, n)
#define SZ(x) ((int)(x).size())
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
struct Vec {
  double x, y;
};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> T min(T a, T b) {
  if (a < b)
    return a;
  return b;
}
template <class T> T max(T a, T b) {
  if (a > b)
    return a;
  return b;
}
using namespace std;
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  // cout << fixed << setprecision(15);
  int N, rc = 0;
  string b;
  cin >> N;
  cin >> b;

  rep(i, N) {
    if (b[i] == 'R') {
      rc++;
    }
  }
  ll ans = 0;
  rep(i, rc) {
    if (b[i] == 'W')
      ans++;
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repi(i, 0, n)
#define SZ(x) ((int)(x).size())
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
struct Vec {
  double x, y;
};
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> T min(T a, T b) {
  if (a < b)
    return a;
  return b;
}
template <class T> T max(T a, T b) {
  if (a > b)
    return a;
  return b;
}
using namespace std;
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  // cout << fixed << setprecision(15);
  int N, rc = 0;
  string b;
  vector<int> R(N);
  cin >> N;
  cin >> b;

  rep(i, N) {
    if (b[N - i] == 'R') {
      R.push_back(N - i);
      rc++;
    }
  }
  ll ans = 0;
  rep(i, N) {
    if (rc < 0 || R.empty())
      break;
    if (b[i] == 'W') {
      R.pop_back();
      ans++;
    }
    rc--;
  }
  cout << ans << endl;

  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

int main() {
  fast_io;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n;
  string str;
  cin >> n >> str;

  int x = 0, y = 0, c = 0;
  for (int i = 0; i < n; i++) {
    if (str[i] == 'W')
      y++;
    else
      x++;
  }

  int res = min(x, y);

  for (int i = 0; i < x; i++) {
    if (str[i] == 'W')
      c++;
  }

  res = min(res, c);
  cout << res << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
typedef long long ll;
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

int main() {
  fast_io;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n;
  string str;
  cin >> n >> str;

  int x = 0, y = 0, c = 0;
  for (int i = 0; i < n; ++i) {
    if (str[i] == 'W')
      y++;
    else
      x++;
  }

  int res = min(x, y);

  for (int i = 0; i < n; ++i) {
    if (str[i] == 'W') {
      for (int j = n - 1; j > i; --j) {
        if (str[j] == 'R') {
          str[j] = 'W';
          c++;
          break;
        }
      }
    }
  }

  res = min(res, c);
  cout << res << endl;

  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

int main() {
  int N;
  std::cin >> N;
  std::vector<char> c(N);
  int n_red = 0;
  for (auto &&e : c) {
    std::cin >> e;
    if (e == 'R')
      n_red++;
  }
  int r_diff = 0;
  for (int i = 0; i < n_red; i++) {
    if (c[i] == 'W')
      r_diff++;
  }
  int w_diff = 0;
  for (int i = n_red; i < N; i++) {
    if (c[i] == 'R')
      w_diff++;
  }
  std::cout << std::max(r_diff, w_diff) << std::endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

int main() {
  int N;
  std::cin >> N;
  std::vector<char> c(N);
  int n_red = 0;
  for (auto &&e : c) {
    std::cin >> e;
    if (e == 'R')
      n_red++;
  }
  int r_diff = 0;
  for (int i = 0; i < n_red; i++) {
    if (c[i] == 'W')
      r_diff++;
  }
  int w_diff = 0;
  for (int i = n_red; i < N; i++) {
    if (c[i + n_red] == 'R')
      w_diff++;
  }
  std::cout << std::max(r_diff, w_diff) << std::endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

signed main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> r;
  vector<int> w;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      w.push_back(i);
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == 'R')
      r.push_back(i);
  }
  int ans = 0;
  int m = min(r.size(), w.size());
  for (int i = 0; i < m; i++) {
    if (w[i] < r[i])
      ans++;
  }
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
  int n;
  cin >> n;
  int w = 0;
  string s;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    if (s[i] == 'W')
      w++;
  }
  int ans = w;
  for (int i = n; i > n - w; i--) {
    if (s[i] == 'W')
      ans--;
  }
  cout << ans;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

signed main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> r;
  vector<int> w;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      w.push_back(i);
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == 'R')
      r.push_back(i);
  }
  int ans = 0;
  int m = min(r.size(), w.size());
  for (int i = 0; i < m; i++) {
    if (w[i] < r[i])
      ans++;
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int w = 0, r = 0, ans = INT_MAX;
  for (int i = 0; i <= s.size(); i++) {
    for (int j = 0; j < i; j++) {
      if (s[j] == 'W')
        w++;
    }
    for (int j = i; j <= s.size(); j++) {
      if (s[j] == 'R')
        r++;
    }
    if (w > r)
      ans = min(ans, w);
    else
      ans = min(ans, r);
    w = 0;
    r = 0;
  }
  cout << ans;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define endl "\\n"
#define int long long int
#define forn(i, n) for (int i = 0; i < n; i++)
#define rforn(i, n) for (int i = n - 1; i >= 0; i--)
#define mod (int)(1e9 + 7)
#define T int
#define pi pair<int, int>
#define umap unordered_map<T, T>
#define uset unordered_set<T>
#define ff first
#define ss second
#define vi vector<int>
#define pb push_back
#define all(v) v.begin(), v.end()
#define setbit(n) __builtin_popcount(n)
#define flash                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

int32_t main() {

  flash;
  cout << fixed;
  cout.precision(10);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n;
  cin >> n;
  string s;
  cin >> s;

  int i = 0, j = n - 1;
  int ans = 0;
  while (i < n && j >= 0 && i <= j) {

    while (i < n && s[i] == 'R')
      i++;

    while (j >= 0 && s[j] == 'W')
      j--;

    if (i < n && j >= 0 && i < j) {
      swap(s[i], s[j]);
      ans++;
    }
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define endl "\\n"
#define int long long int
#define forn(i, n) for (int i = 0; i < n; i++)
#define rforn(i, n) for (int i = n - 1; i >= 0; i--)
#define mod (int)(1e9 + 7)
#define T int
#define pi pair<int, int>
#define umap unordered_map<T, T>
#define uset unordered_set<T>
#define ff first
#define ss second
#define vi vector<int>
#define pb push_back
#define all(v) v.begin(), v.end()
#define setbit(n) __builtin_popcount(n)
#define toUpper(s) transform(s.begin(), s.end(), s.begin(), ::toupper)
#define toLower(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
#define flash                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

int lcm(int a, int b) { return (a / __gcd(a, b) * b); }

int32_t main() {

  flash;
  cout << fixed;
  cout.precision(10);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n;
  cin >> n;
  string s;
  cin >> s;

  int i = 0, j = n - 1;
  int ans = 0;
  while (i <= j && i < n && j >= 0) {
    if (s[i] == 'W' && s[j] == 'R') {
      swap(s[i], s[j]);
      i++;
      j--;
      ans++;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  string s;
  int n;
  cin >> n;
  cin >> s;
  int l = 0, r = n - 1;
  while (l < n and s[l] != 'W')
    l++;
  while (r > 0 and s[r] != 'R')
    r--;
  int ans = 0;
  while (l < r) {
    if (s[l] == 'W' and s[r] == 'R')
      ans++;
    l++, r--;
    while (l < n and s[l] != 'W')
      l++;
    while (r > 0 and s[r] != 'R')
      r--;
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
bool isDis(string s, int n) {
  for (int i = 0; i < n - 1; i++) {
    if (s[i] == 'W' && s[i + 1] == 'R')
      return true;
  }
  return false;
}
int getW(string s, int n) {
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      return i;
  }
  return -1;
}
int getR(string s, int n) {
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == 'R')
      return i;
  }
  return n;
}
int fun(string s, int n) {
  int a = 0;
  while (isDis(s, n)) {
    a += 1;
    int w = getW(s, n);
    int r = getR(s, n);
    if (w == -1 || r == n)
      break;
    else {
      char c = s[w];
      s[w] = s[r];
      s[r] = c;
    }
  }
  return a;
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  if (!isDis(s, n))
    cout << "0\\n";
  else
    cout << fun(s, n);

  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 2 * 1e5 + 100;
typedef long long ll;

int x[N] = {0};

int main() {
  int n;
  string s;
  cin >> n >> s;
  int a = 0, b = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W') {
      a++;
    }
    x[i] = a;
  }
  cout << x[n - a - 1] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5;
typedef long long ll;

int x[N] = {0};

int main() {
  int n;
  string s;
  cin >> n >> s;
  int a = 0, b = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W') {
      a++;
    }
    x[i] = a;
  }
  cout << x[n - a - 1] << endl;
  return 0;
}

/*
6
WWWWRR
8
WWWWRRWR
5
WRRRR
9
RWRWRWWRW
*/
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long int
using namespace std;

// Author: Ashish

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  string str;
  cin >> str;
  vector<int> red, white;
  for (int i = 0; i < n; i++) {
    if (str[i] == 'R')
      red.push_back(i);
    else
      white.push_back(i);
  }
  int answer = 0;
  for (int i = red.size() - 1, j = 0; i >= 0 && j < white.size(); i--, j++) {
    if (red[i] < white[j])
      break;
    answer++;
  }
  cout << answer;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define int long long int
using namespace std;

// Author: Ashish

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  string str;
  cin >> str;
  vector<int> red, white;
  for (int i = 0; i < n; i++) {
    if (str[i] == 'R')
      red.push_back(i);
    else
      white.push_back(i);
  }
  int answer = 0;
  for (int i = red.size(), j = 0; i >= 0 && j < white.size(); i--, j++) {
    if (red[i] < white[j])
      break;
    answer++;
  }
  cout << answer;

  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  vector<ll> r, w;
  for (ll i = 0; i < n; i++) {
    if (s[i] == 'R')
      r.push_back(i);
    else
      w.push_back(i);
  }
  ll ans = 0;
  ll p = r.size() - 1;
  for (ll i = 0; i < w.size(); i++) {
    if (p >= 0 && r[p] > w[i]) {
      ans++;
      p--;
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  vector<ll> r, w;
  for (ll i = 0; i < n; i++) {
    if (s[i] == 'R')
      r.push_back(i);
    else
      w.push_back(i);
  }
  ll ans = 0;
  ll p = r.size() - 1;
  for (ll i = 0; i < w.size(); i++) {
    if (r[p] > w[i] && p >= 0) {
      ans++;
      p--;
    }
  }
  cout << ans << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  long long n, a = 1, b, p, i, r = 0, w = 0, ans;
  cin >> n;
  char s[n];
  for (i = 0; i < n; i++) {
    cin >> s[i];
    if (s[i] == 'W')
      w++;
  }
  ans = w;

  for (i = n - 1; i >= n - w; i--) {
    if (s[i] == 'W')

      ans--;
  }
  cout << ans << '\\n';
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  long long n, a = 1, b, p, i, r = 0, w = 0, ans;
  cin >> n;
  string s;
  for (i = 0; i < n; i++) {
    cin >> s[i];
    if (s[i] == 'W')
      w++;
  }
  ans = w;

  for (i = n - 1; i >= n - w; i--) {
    if (s[i] == 'W')

      ans--;
  }
  cout << ans << '\\n';
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <iostream>
#include <string>
using namespace std;

int main(void) {
  int r = 0, w, cnt = 0;
  cin >> w;
  string s;
  cin >> s;
  w -= 1;
  while (r < w) {
    while (r < w && s.at(r) == 'R')
      r++;
    while (w > r && s.at(w) == 'W')
      w--;
    if (w <= r)
      break;
    cnt++;
    r++;
    w--;
  }
  cout << cnt << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <iostream>
#include <string>
using namespace std;

int main(void) {
  int n, r = 0, w, cnt = 0;
  cin >> n;
  string s;
  cin >> s;
  w = n - 1;
  while (r < w) {
    while (s[r] == 'R' && r < n)
      r++;
    while (s[w] == 'W' && w >= 0)
      w--;
    if (w <= r)
      break;
    cnt++;
    r++;
    w--;
  }
  cout << cnt << '\\n';
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#define DIN freopen("input.txt", "r", stdin);
#define DOUT freopen("output.txt", "w", stdout);
#include <bits/stdc++.h>
#include <cstdio>
#define mem(a, b) memset(a, b, sizeof(a))
#define REP(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define REP_(i, a, b) for (int i = (a); i >= (b); i--)
#define pb push_back
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<LL, LL> P;
int read() {
  int x = 0, flag = 1;
  char c = getchar();
  while ((c > '9' || c < '0') && c != '-')
    c = getchar();
  if (c == '-')
    flag = 0, c = getchar();
  while (c <= '9' && c >= '0') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
  return flag ? x : -x;
}

const int maxn = 2e5 + 5;
char s[maxn];
int l, r;

int main() {
  int n = read();
  scanf("%s", s + 1);
  l = 1, r = n;
  int ans = 0;
  while (l <= r) {
    while (l <= n && s[l] == 'R')
      l++;
    while (r >= 1 && s[r] == 'W')
      r--;
    if (l < r)
      swap(s[l], s[r]), ans++;
  }
  cout << ans;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int ans = 0;
  int l = 1, r = n;
  while (l <= r) {
    while (l <= n && s[l] == 'R')
      l++;
    while (r >= 1 &&　s[r] == 'W')
      r--;
    if (l < r)
      swap(s[l], s[r]), ans++;
  }
  cout << ans;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const ll MAX = 1000000000000000;
static const int NIL = -1;
using namespace std;
const long double EPS = 0.0000000001;
const long double PI = (acos(-1));
const int MOD = 1000000007;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

int main() {

  ll n;
  cin >> n;
  ll rNum = 0;
  vector<ll> Rpos(200000, -1);
  string s;
  cin >> s;
  ll ans = 0;
  ll front = 0, tail = s.length() - 1;

  rep(i, n) {
    if (s[i] == 'R') {
      rNum++;
    }
  }

  while (front < tail) {
    if (s[front] == 'W' && s[tail] == 'R') {
      front++;
      tail--;
      ans++;
    } else if (s[tail] == 'W') {
      tail--;
    } else if (s[tail] == 'R' && s[front] == 'R') {
      front++;
    }
  }

  cout << ans;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const ll MAX = 1000000000000000;
static const int NIL = -1;
using namespace std;
const long double EPS = 0.0000000001;
const long double PI = (acos(-1));
const int MOD = 1000000007;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

int main() {

  ll n;
  cin >> n;
  ll rNum = 0;
  ll rcount = 0;
  vector<ll> Rpos(200000, -1);
  string s;
  cin >> s;
  ll pos = 1;
  ll wNum = 0;
  ll ans = 0;
  ll front = 0, tail = s.length() - 1;

  rep(i, n) {
    if (s[i] == 'R') {
      rNum++;
    }
  }

  while (front < tail) {
    if (s[front] == 'W' && s[tail] == 'R') {
      front++;
      tail--;
      ans++;
    } else if (s[front] == 'W') {
      tail--;
    } else if (s[tail] == 'R' && s[front] == 'R') {
      front++;
    }
  }

  cout << ans;

  return 0;
}