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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;

template <typename T1, typename T2> inline void chmin(T1 &a, const T2 &b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, const T2 &b) {
  if (a < b)
    a = b;
}

#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()

/* do your best */

int main() {

  int n;
  cin >> n;
  string s;
  cin >> s;
  int ri = n - 1;
  int ans = 0;
  for (int li = 0; li < n; li++) {
    if (s[li] == 'R')
      continue;
    while (1) {
      if (ri < 0)
        break;
      if (s[ri] == 'R')
        break;
      ri--;
    }
    if (li >= ri)
      break;
    swap(s[li], s[ri]);
    ans++;
  }

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;

template <typename T1, typename T2> inline void chmin(T1 &a, const T2 &b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, const T2 &b) {
  if (a < b)
    a = b;
}

#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()

/* do your best */

int main() {

  int n;
  cin >> n;
  string s;
  cin >> s;
  int ri = n - 1;
  int ans = 0;
  for (int li = 0; li < n; li++) {
    if (s[li] == 'R')
      continue;
    while (1) {
      if (s[ri] == 'R')
        break;
      ri--;
    }
    if (li >= ri)
      break;
    swap(s[li], s[ri]);
    ans++;
  }

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

#include <bits/stdc++.h>

int main() {
  int n, count = 0;
  char cor[200001];
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf(" %c", &cor[i]);
  bool vai = 1;
  unsigned int st = 0, ed = n - 1;
  while (vai) {
    while (cor[st] != 'W') {
      st++;
      if (st == ed) {
        vai = false;
        break;
      }
    }
    if (!vai)
      break;
    while (cor[ed] != 'R') {
      ed--;
      if (st == ed) {
        vai = false;
        break;
      }
    }
    if (!vai)
      break;
    char _1st = cor[st], _2nd = cor[ed];
    cor[st] = _2nd;
    cor[ed] = _1st;
    count++;
  }
  printf("%d\\n", count);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

int main() {
  int n, count = 0;
  char cor[200001];
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf(" %c", &cor[i]);
  bool vai = 1;
  unsigned int st = 0, ed = n - 1;
  while (vai) {
    while (cor[st] != 'W') {
      st++;
      if (st == ed)
        vai = false;
    }
    if (!vai)
      break;
    while (cor[ed] != 'R') {
      ed--;
      if (st == ed)
        vai = false;
    }
    if (!vai)
      break;
    char _1st = cor[st], _2nd = cor[ed];
    cor[st] = _2nd;
    cor[ed] = _1st;
    count++;
  }
  printf("%d\\n", count);
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int main(void) {
  int stones_cnt;
  string stones;

  cin >> stones_cnt;
  cin >> stones;

  int left_white = 0;
  int right_red = stones_cnt - 1;
  int swap_cnt = 0;

  while (left_white < right_red) {
    if (stones[left_white] == 'W' && stones[right_red] == 'R') {
      swap_cnt += 1;
      left_white += 1;
      right_red -= 1;
    }

    while (stones[left_white] != 'W' && left_white < stones_cnt - 1) {
      left_white += 1;
    }
    while (stones[right_red] != 'R' && right_red > 0) {
      right_red -= 1;
    }
  }

  cout << swap_cnt << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int main(void) {
  int stones_cnt;
  string stones;

  cin >> stones_cnt;
  cin >> stones;

  int left_white = 0;
  int right_red = stones_cnt - 1;
  int swap_cnt = 0;

  while (left_white < right_red) {
    if (stones[left_white] == 'W' && stones[right_red] == 'R') {
      swap_cnt += 1;
      left_white += 1;
      right_red -= 1;
    }

    while (stones[left_white] != 'W') {
      left_white += 1;
    }
    while (stones[right_red] != 'R') {
      right_red -= 1;
    }
  }

  cout << swap_cnt << endl;

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

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

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int n;
  string c;
  cin >> n >> c;
  int ans = 0;
  for (int i = 0, r = n - 1; i < n && i < r; ++i) {
    if (c.at(i) == 'R')
      continue;
    while (r > 0 && c.at(r) != 'R')
      --r;
    if (c.at(r) == 'R' && i < r) {
      swap(c.at(i), c.at(r));
      ++ans;
    }
  }
  cout << ans << "\\n"s;
}


Error: 0

Fixed submission:

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

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int n;
  string c;
  cin >> n >> c;
  int ans = 0;
  for (int i = 0, r = n - 1; i < n && i < r; ++i) {
    if (c.at(i) == 'R')
      continue;
    while (r >= 0 && c.at(r) != 'R')
      --r;
    if (c.at(r) == 'R' && i < r) {
      swap(c.at(i), c.at(r));
      ++ans;
    }
  }
  cout << ans << "\\n"s;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

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

  int r = 0, w = 0, swap = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'W') {
      ++w;
    } else {
      ++r;
    }
  }
  int ans = min(r, w);

  for (int i = 0; i < r; ++i) {
    if (s[i] == 'W') {
      ++swap;
    }
  }
  ans = min(ans, swap);

  cout << ans << '\\n';

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

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

  int r = 0, w = 0, swap = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'W') {
      ++w;
    } else {
      ++r;
    }
  }
  int ans = min(r, w);

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

  cout << ans << '\\n';

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)n; i++)
#define sp(n) cout << fixed << setprecision(n)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
using namespace std;
int main(void) {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll res = 0;
  priority_queue<ll> que;
  rep(i, n) {
    if (s[i] == 'R') {
      que.push(i);
    }
  }
  if (que.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  rep(i, n) {
    if (s[i] == 'W') {
      ll x = que.top();
      que.pop();
      if (x <= i)
        break;
      s[x] = 'W';
      s[i] = 'R';
      res++;
    }
  }
  cout << res << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define rep1(i, n) for (int i = 1; i <= (int)n; i++)
#define sp(n) cout << fixed << setprecision(n)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
using namespace std;
int main(void) {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll res = 0;
  priority_queue<ll> que;
  rep(i, n) {
    if (s[i] == 'R') {
      que.push(i);
    }
  }
  rep(i, n) {
    if (s[i] == 'W') {
      ll x = que.top();
      que.pop();
      if (x <= i)
        break;
      s[x] = 'W';
      s[i] = 'R';
      res++;
    }
  }
  cout << res << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int n, i, j, count = 0;
  cin >> n;
  char temp, ch[n];

  for (i = 0; i < n; i++)
    cin >> ch[i];
  i = 0;
  j = n - 1;
  while (i < j) {
    while (ch[i] != 'W' && i < n)
      i++;
    while (ch[j] != 'R' && j >= 0)
      j--;
    if (i < j) {
      count++;
      temp = ch[i];
      ch[i] = ch[j];
      ch[j] = temp;
      i++;
      j--;
    }
  }
  cout << count;
  return (0);
}


Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n, i, count = 0;
  cin >> n;
  char ch[n];
  for (int i = 0; i < n; i++)
    cin >> ch[i];
  i = 0;
  int j = n - 1;
  while (i < j) {
    while (ch[i] != 'W' && i < n)
      i++;
    while (ch[j] != 'R' && j >= 0)
      j++;
    if (i < j) {
      count++;
      char temp;
      temp = ch[i];
      ch[i] = ch[j];
      i++, j--;
    }
  }
  cout << count << "\\n";
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  const int R = 0;
  const int W = 1;
  vector<int> c(n);
  // set<int> rs;
  vector<int> rs;
  for (int i = 0; i < n; i++) {
    char ch;
    cin >> ch;
    if (ch == 'R') {
      c[i] = R;
    } else {
      c[i] = W;
    }
    if (c[i] == R) {
      rs.push_back(i);
    }
  }
  int index = 0;
  int ans = 0;
  while (!rs.empty() && index < n) {
    if (c[index] == R) {
      index++;
      continue;
    }
    int ri = rs.back();
    if (index < ri) {
      c[index] = R;
      c[ri] = W;
      rs.erase(rs.end() - 1);
      ans++;
    } else {
      break;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  const int R = 0;
  const int W = 1;
  vector<int> c(n);
  // set<int> rs;
  vector<int> rs;
  int last_w = -1;
  for (int i = 0; i < n; i++) {
    char ch;
    cin >> ch;
    if (ch == 'R') {
      c[i] = R;
    } else {
      c[i] = W;
    }
    if (c[i] == W) {
      last_w = i;
    } else if (c[i] == R) {
      if (last_w != -1)
        rs.push_back(i);
    }
  }
  int index = 0;
  int ans = 0;
  while (index < n) {
    if (c[index] == R) {
      index++;
      continue;
    }
    int ri = rs.back();
    if (index < ri) {
      c[index] = R;
      c[ri] = W;
      rs.erase(rs.end() - 1);
      ans++;
    } else {
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define ll long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define PI 3.141592653589
#define MOD 1000000007

using namespace std;

template <typename T> T gcd(T a, T b) { return (a % b) ? gcd(b, a % b) : b; }
template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); }

int main(void) {
  int n;
  cin >> n;
  string s;
  cin >> s;

  int i = 0, j = n - 1;
  int ctr = 0;
  while (i < j) {
    while (s[i] == 'R')
      i++;
    while (s[j] == 'W')
      j--;
    if (i < j)
      ctr++;
    i++;
    j--;
  }
  cout << ctr << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

#define ll long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define PI 3.141592653589
#define MOD 1000000007

using namespace std;

template <typename T> T gcd(T a, T b) { return (a % b) ? gcd(b, a % b) : b; }
template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); }

int main(void) {
  int n;
  cin >> n;
  char s[n + 3];
  cin >> s;

  int i = 0, j = n - 1;
  int ctr = 0;
  while (i < j) {
    while (s[i] != 'W')
      i++;
    while (s[j] != 'R')
      j--;
    if (i < j)
      ctr++;
    i++;
    j--;
  }
  cout << ctr << endl;

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

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int i = 0, j = n - 1, ans = 0;
  while (1) {
    while (i < j and s[i] == 'R')
      ++i;
    while (i < j and s[j] == 'W')
      --j;
    if (i == j)
      break;
    swap(s[i], s[j]);
    ans++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int i = 0, j = n - 1, ans = 0;
  while (1) {
    while (i < j and s[i] == 'R')
      ++i;
    while (i < j and s[j] == 'W')
      ++j;
    if (i == j)
      break;
    swap(s[i], s[j]);
    ans++;
  }
  cout << ans << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, ans = 0;
  string c;
  cin >> n >> c;
  priority_queue<int> r;
  vector<int> w;
  for (auto i = 0; i < n; i++) {
    if (c[i] == 'R')
      r.push(i);
    else
      w.push_back(i);
  }
  if (r.empty() or w.empty()) {
    cout << 0 << endl;
    return 0;
  }
  for (auto i = 0; i < (int)w.size(); i++) {
    if (r.top() > w[i]) {
      auto tmp = r.top();
      r.pop();
      c[w[i]] = 'R';
      c[tmp] = 'W';
      r.push(w[i]);
      w[i] = tmp;
      ans++;
    }
    //        cout << c << endl;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n, ans = 0;
  string c;
  cin >> n >> c;
  priority_queue<int> r;
  vector<int> w;
  for (auto i = 0; i < n; i++) {
    if (c[i] == 'R')
      r.push(i);
    else
      w.push_back(i);
  }
  for (auto i = 0; i < (int)w.size(); i++) {
    if (r.top() > w[i]) {
      auto tmp = r.top();
      r.pop();
      c[w[i]] = 'R';
      c[tmp] = 'W';
      r.push(w[i]);
      w[i] = tmp;
      ans++;
    }
    //        cout << c << endl;
  }
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 2e9;
typedef pair<int, int> pi;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  string s;
  cin >> n >> s;
  vector<int> a, b;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      a.push_back(i);
    else
      b.push_back(i);
  }
  reverse(b.begin(), b.end());
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (i > ((int)a.size() - 1) || i > ((int)b.size()) - 1) {
      break;
    }
    if (a[i] > b[i])
      break;
    ;
    ans++;
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 2e9;
typedef pair<int, int> pi;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  string s;
  cin >> n >> s;
  vector<int> a, b;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      a.push_back(i);
    else
      b.push_back(i);
  }
  reverse(b.begin(), b.end());
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (i > a.size() - 1 || i > b.size() - 1) {
      break;
    }
    if (a[i] > b[i])
      break;
    ;
    ans++;
  }
  cout << ans;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

long long int N;
vector<char> c;

int main() {
  cin >> N;
  char a = 'a';
  int R_amo = 0;

  for (int i = 0; i < N; i++) {
    cin >> a;
    c.push_back(a);

    if (c[i] == 'R') {
      R_amo++;
    }
  }

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

  cout << W_amo << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

long long int N;
string c;

int main() {
  cin >> N;
  string a = "";

  bool flag = false;
  int R_amo = 0;
  int ans = 0;

  for (int i = 1; i < N + 1; i++) {
    cin >> a;
    c += a;

    if (c[i - 1] == 'R') {
      R_amo++;
    }
  }

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

  cout << W_amo << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

using namespace std;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
const long long MOD = 1000000000 + 7;
const double PI = acos(-1);

int main() {
  int N;
  cin >> N;
  vector<char> c(N);
  rep(i, N) cin >> c[i];
  int count = 0;
  rep(i, N) {
    if (c[i] == 'R')
      ++count;
  }
  if (count == 0 || count == N) {
    cout << 0;
    return 0;
  }

  int cw = 0;
  int cr = 0;

  int r = N - 1;
  int l = 0;
  rep(i, N) {
    if (c[r - i] == 'R') {
      rep(j, N - l) {
        if (c[l + j] == 'W') {
          if (r - i < l + j) {
            cout << cr;
            return 0;
          }
          ++cr;
          c[l + j] = 'R';
          c[r - i] = 'W';
          l = l + j;
          break;
        }
      }
    }
  }

  cout << cr;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

using namespace std;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
const long long MOD = 1000000000 + 7;
const double PI = acos(-1);

int main() {
  int N;
  cin >> N;
  vector<char> c(N);
  rep(i, N) cin >> c[i];
  int count = 0;
  rep(i, N) {
    if (c[i] == 'R')
      ++count;
  }
  if (count == 0 || count == N) {
    cout << 0;
    return 0;
  }

  int cw = 0;
  int cr = 0;

  int r = N - 1;
  int l = 0;
  rep(i, N) {
    if (c[r - i] == 'R') {
      rep(j, N - l) {
        if (c[l + j] == 'W') {
          if (r - i < l + j)
            break;
          ++cr;
          c[l + j] = 'R';
          c[r - i] = 'W';
          l = l + j;
          break;
        }
      }
    }
  }

  cout << cr;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n, x, y, p, q, i, count;
  cin >> n;
  string str, s, t;
  cin >> str;
  s = str;
  sort(s.begin(), s.end());
  x = 0;
  y = 0;
  for (i = 0; i < n; i++) {
    if (str[i] != s[i]) {
      if (str[i] == 'R') {
        x++;
      } else {
        y++;
      }
    }
  }
  cout << (max(x, y));
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  long long int n, x, y, p, q, i, count;
  cin >> n;
  string str, s, t;
  cin >> str;
  count = 0;
  for (i = 0; i < n; i++) {
    if (str[i] == 'R') {
      count++;
    }
  }
  for (i = 0; i < count; i++) {
    s = s + "R";
  }
  for (i = count; i < n; i++) {
    s = s + "W";
  }
  x = 0;
  y = 0;
  p = 0;
  q = 0;
  for (i = 0; i < n; i++) {
    if (str[i] != s[i]) {
      if (str[i] == 'R') {
        x++;
      } else {
        y++;
      }
    }
  }
  cout << (max(x, y));
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {

  int n;
  cin >> n;
  string s;
  cin >> s;
  // int n=s.length();
  int i = 0, j = n - 1;
  int op = 0;
  while (i < j) {
    while (i < n && s[i] != 'W') {
      i++;
    }
    while (j >= 0 && s[j] != 'R') {
      j--;
    }
    if (i < j) {
      op++;
      swap(s[i], s[j]);
    }
  }
  cout << op;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {

  int n;
  cin >> n;
  string s;
  cin >> s;
  // int n=s.length();
  int i = 0, j = n - 1;
  int op = 0;
  while (i < j) {
    while (s[i] != 'W') {
      i++;
    }
    while (s[j] != 'R') {
      j--;
    }
    if (i < j) {
      op++;
      swap(s[i], s[j]);
    }
  }
  cout << op;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define faster ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)

#define endl "\\n"

int main() {
  faster;

  int n;
  cin >> n;
  string s;
  cin >> s;
  int r = n - 1, l = 0, cnt = 0;

  while (r > l) {
    while (l < n && s[l] == 'R')
      l++;
    while (r >= 0 && s[r] == 'W')
      r--;
    if (l > r)
      break;
    cnt++;
    s[l] = 'R', s[r] = 'W';
  }

  cout << cnt << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define faster ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)

#define endl "\\n"

int scheck(string s, int start) {
  int n = s.size(), w = 0, r = 0, i;
  for (i = start; i < n; i++) {
    if (start == 0 && w == 0 && r == 0) {
      if (s[i] == 'W') {
        w++;
      }
      continue;
    }
    if (s[i] == 'W') {
      w++;
    } else {
      r++;
    }
    if (i == n - 1 || (s[i + 1] == 'W' && s[i] == 'R')) {
      i++;
      break;
    }
  }
  if (i == n) {
    return min(w, r);
  }
  return min(w + scheck(s, i), r + scheck(s, i));
}

int main() {
  faster;

  int n;
  cin >> n;
  string s;
  cin >> s;
  int w = 0, r = 0, cnt = 0;

  cout << scheck(s, 0) << endl;

  return 0;
}