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

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>

using boost::multiprecision::cpp_int;
using namespace std;

#if __has_include("print.hpp")
#include "print.hpp"
#endif

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MOD 1000000007

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;
typedef pair<ll, ll> p;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  ll count = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'W')
      count++;
  }
  ll res = min(count, n - count);
  int countW, countR = 0;
  vector<int> vW, vR;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'W') {
      countW++;
      vW.push_back(i);
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R') {
      countR++;
      vR.push_back(i);
    }
  }

  int ind = int(vR.size()) - 1;
  count = 0;
  // print(vW);
  // print(vR);
  if (vW.size() == 0 || vR.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = 0; i < int(vW.size()); ++i) {
    if (vW[i] < vR[ind]) {
      swap(s[vW[i]], s[vR[ind]]);
      ind--;
      count++;
    }
  }
  chmin(res, count);

  cout << res << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>

using boost::multiprecision::cpp_int;
using namespace std;

#if __has_include("print.hpp")
#include "print.hpp"
#endif

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define MOD 1000000007

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;
typedef pair<ll, ll> p;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  ll count = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'W')
      count++;
  }
  ll res = min(count, n - count);
  int countW, countR = 0;
  vector<int> vW, vR;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'W') {
      countW++;
      vW.push_back(i);
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R') {
      countR++;
      vR.push_back(i);
    }
  }

  int ind = int(vR.size()) - 1;
  count = 0;
  // print(vW);
  // print(vR);
  for (int i = 0; i < int(vW.size()); ++i) {
    if (vW[i] < vR[ind]) {
      swap(s[vW[i]], s[vR[ind]]);
      ind--;
      count++;
    }
  }
  chmin(res, count);

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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int n, memo, ans = 0, w = 0, r = 0;
  char str[200001];
  vector<int> index_W(200001, -1), index_R(200001, -1);
  scanf("%d%s", &n, &str);
  memo = n - 1;
  for (int i = 0; i < n; ++i) {
    if (str[i] == 'W') {
      index_W[w++] = i;
    }
    if (str[n - 1 - i] == 'R') {
      index_R[r++] = n - 1 - i;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (index_W[i] == -1 || index_R[i] == -1 || (index_W[i] > index_R[i]))
      break;
    swap(str[index_W[i]], str[index_R[i]]);
    ans++;
  }
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int n, memo, ans = 0;
  char str[200001];
  scanf("%d%s", &n, &str);
  memo = n - 1;
  for (int i = 0; i < n; ++i) {
    if (str[i] == 'W') {
      for (int j = memo; j > i; --j) {
        if (str[j] == 'R') {
          str[i] = 'R';
          str[j] = 'W';
          memo = j - 1;
          ans++;
          break;
        }
      }
    }
    if (i >= memo)
      break;
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

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

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

    if (i < j) {
      c++;
      i++;
      j--;
    }
  }
  cout << c << endl;
}

int main() {
  solve();

  return 0;
}

Error: 0

Fixed submission:

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

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

  int c = 0;
  int i = 0, j = n - 1;
  while (i < j) {
    while (s[i] != 'W')
      i++;
    while (s[j] != 'R')
      j--;

    if (i < j) {
      c++;
      i++;
      j--;
    }
  }
  cout << c << endl;
}

int main() {
  solve();

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;

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

  int r = count(s.begin(), s.end(), 'R');
  int ans = count(s.begin(), s.begin() + r, 'W');

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;

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

  if (all_of(s.begin(), s.end(), [s](char x) { return s[0] == x; })) {
    cout << 0 << endl;
    return 0;
  }

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

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

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

#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie();                                                                   \\
  cout.tie();

#define MAXX 10005

#define PI 3.14159265358979323846264338327950

#define F first
#define S second

#define ll long long int

#define mod 1000000007

int main() {
  FAST;
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll x = n - 1;
  ll h = 0;
  for (ll i = 0; i < n - 1; i++) {
    if (s[i] == 'W') {
      for (ll j = x; j >= 0; j--) {
        x = j;
        if (s[j] == 'R') {

          break;
        }
      }
      if (s[i] == 'W' && s[x] == 'R' & x > i) {
        swap(s[x], s[i]);
        h++;
        x--;
      }
    }
  }
  cout << h;
  return 0;
}

Error: TLE

Fixed submission:

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

#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie();                                                                   \\
  cout.tie();

#define MAXX 10005

#define PI 3.14159265358979323846264338327950

#define F first
#define S second

#define ll long long int

#define mod 1000000007

int main() {
  FAST;
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll x = n - 1;
  ll h = 0;
  for (ll i = 0; i < n - 1; i++) {
    if (s[i] == 'W' && x > i) {
      for (ll j = x; j > i; j--) {
        if (s[j] == 'R' || !j) {
          x = j;
          break;
        }
      }
      if (s[i] == 'W' && s[x] == 'R' & x > i) {
        swap(s[x], s[i]);
        h++;
        x--;
      }
    }
  }
  cout << h;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

int main() {
  int N;
  std::cin >> N;
  std::vector<char> c(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> c[i];
  }

  int count = 0;
  int w = 0, r = N - 1;
  while (true) {
    for (int i = w; i < N; ++i) {
      w = i;
      if (c[i] == 'W') {
        break;
      }
    }
    for (int j = r; j >= 0; --j) {
      r = j;
      if (c[j] == 'R') {
        break;
      }
    }
    if (w >= r)
      break;
    else {
      // c[i] = 'R';
      // c[j] = 'W';
      w++;
      r--;
      count++;
    }
  }

  std::cout << count << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

int main() {
  int N;
  std::cin >> N;
  std::vector<char> c(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> c[i];
  }

  int count = 0;
  while (true) {
    int i = 0, j = N - 1;
    for (; i < N; ++i) {
      if (c[i] == 'W') {
        break;
      }
    }
    for (; j >= 0; --j) {
      if (c[j] == 'R') {
        break;
      }
    }
    if (i >= j)
      break;
    else {
      c[i] = 'R';
      c[j] = 'W';
      count++;
    }
  }

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

#include <bits/stdc++.h>
#define ll long long
const ll MAX = 100050;
const ll modn = 1e9 + 7;
using namespace std;

void slove() {
  int n;
  string a;
  cin >> n >> a;
  int ans1 = 0;
  int ans2 = 0;
  int l = -1, r = -1;
  for (int i = 0; i < a.length(); i++) {
    if (a[i] == 'W') {
      l = i;
      break;
    }
  }
  for (int i = a.length() - 1; i >= 0; i--) {
    if (a[i] == 'R') {
      r = i;
      break;
    }
  }
  if (l == -1 || r == -1) {
    cout << "0\\n";
    return;
  }
  while (l < r) {
    ans1++;
    ans2++;
    l++;
    r--;
    while (a[l] == 'R')
      l++;
    while (a[r] == 'W')
      r--;
  }
  cout << ans1 << endl;
}

int main() {
  int _;
  // scanf("%d",&_);
  _ = 1;
  while (_--) {
    slove();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
const ll MAX = 100050;
const ll modn = 1e9 + 7;
using namespace std;

void slove() {
  int n;
  string a;
  cin >> n >> a;
  // cout<<a<<endl;
  int ans = 0;
  int l = a.find('W');
  int r = a.find_last_of('R');
  // cout<<l<<" "<<r;
  if (l == -1 || r == -1) {
    cout << "0\\n";
    return;
  }
  while (l < r) {
    a[l] = 'R';
    a[r] = 'W';
    l = a.find("W");
    r = a.find_last_of("R");
    ans++;
  }
  cout << ans << endl;
}

int main() {
  int _;
  // scanf("%d",&_);
  _ = 1;
  while (_--) {
    slove();
  }
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;

int main() {
  unsigned long long int n;
  cin >> n;
  std::string s;
  cin >> s;
  int cnt = std::count(s.begin(), s.end(), 'R');
  if (cnt == 0) {
    cout << 0 << endl;
    return 0;
  }
  // cout << cnt << endl;

  int cnt2 = 0;
  int size = s.length();
  std::vector<int> vindex;
  for (int i = 0; i < size; i++) {
    if (s[i] == 'R') {
      vindex.push_back(i);
      if (i + 1 > cnt)
        cnt2++;
    }
  }
  // cout << cnt2 << endl;
  // cout << vindex[vindex.size()-1]+1 << endl;
  if (cnt == vindex[vindex.size() - 1] + 1) {
    cout << 0 << endl;
  } else {
    cout << cnt2 << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <vector>
using namespace std;

int main() {
  unsigned long long int n;
  cin >> n;
  std::string s;
  cin >> s;
  int cnt = std::count(s.begin(), s.end(), 'R');
  // cout << cnt << endl;

  int cnt2 = 0;
  int size = s.length();
  std::vector<int> vindex;
  for (int i = 0; i < size; i++) {
    if (s[i] == 'R') {
      vindex.push_back(i);
      if (i + 1 > cnt)
        cnt2++;
    }
  }
  // cout << cnt2 << endl;
  // cout << vindex[vindex.size()-1]+1 << endl;
  if (cnt == vindex[vindex.size() - 1] + 1) {
    cout << 0 << endl;
  } else {
    cout << cnt2 << endl;
  }

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

#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int n;
  string s;
  cin >> n >> s;
  vector<bool> flag(n);
  rep(i, n) {
    if (s[i] == 'R')
      flag[i] = false;
    if (s[i] == 'W')
      flag[i] = true;
  }

  int ans = 0;
  int mae = 0;
  for (int j = n - 1; j >= mae; j--) {
    if (flag[j] == false) {
      for (int i = mae; i < j; i++) {
        if (flag[i] == true) {
          flag[i] = false;
          flag[j] = true;
          mae = i + 1;
          ans++;
          break;
        }
        if (i == j - 1)
          mae = i;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int n;
  string s;
  cin >> n >> s;
  vector<bool> flag(n);
  rep(i, n) {
    if (s[i] == 'R')
      flag[i] = false;
    if (s[i] == 'W')
      flag[i] = true;
  }

  int ans = 0;
  int mae = 0;
  for (int j = n - 1; j >= mae; j--) {
    if (flag[j] == false) {
      for (int i = mae; i < j; i++) {
        if (flag[i] == true) {
          flag[i] = false;
          flag[j] = true;
          mae = i + 1;
          ans++;
          break;
        }
      }
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>

#define F first
#define S second
#define N 222222
#define M 1000000007
#define pb push_back
#define cend(s) return cout << s, 0
using namespace std;
typedef long long ll;
typedef vector<int> vi;

int main() {
  int n, r = 0, w, r1 = 0, w1 = 0;
  string s, ss;
  cin >> n >> s;
  for (int i = 0; i < n; i++)
    r += (s[i] == 'R');
  w = n - r;
  if (!w || !r)
    cend(0);
  for (int i = 0; i < n; i++) {
    w1 += (s[i] == 'W');
    r1 += (s[i] == 'R');
    if (w1 == r - r1 || w1 == w)
      cend(w1);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define F first
#define S second
#define N 222222
#define M 1000000007
#define pb push_back
#define cend(s) return cout << s, 0
using namespace std;
typedef long long ll;
typedef vector<int> vi;

int main() {
  int n, r = 0, w, t = 0;
  string s, ss;
  cin >> n >> s;
  for (int i = 0; i < n; i++)
    r += (s[i] == 'R');
  w = n - r;
  if (!w || !r)
    cend(0);
  for (int i = 0; i < n; i++) {
    ss = s;
    reverse(ss.begin(), ss.end());
    if (s[i] == 'W') {
      int x = ss.find('R');
      if (x == w)
        cend(t);
      s[n - x - 1] = 'W';
      s[i] = 'R';
      t++;
    }
    // cout << s << ' ' << ss << '\\n';
  }
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
char a[2000000];
int main() {
  int n, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int i = 0;
  int j = n - 1;
  while (i < j) {
    if (a[i] == 'R')
      i++;
    else if (a[j] == 'W')
      j--;
    else {
      ans++;
      i++;
      j--;
    }
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
char a[2000000];
int main() {
  int n, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
      if (a[i] == 'W' && a[j] == 'R') {
        a[i] = 'R';
        a[j] = 'W';
        ans++;
      }
    }
  }
  cout << ans;
  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++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

int main() {
  int n;
  string s;
  cin >> n >> s;
  int red = 0, white = 0;
  vi count(n);
  rep(i, n) {
    if (s.at(i) == 'R')
      red++;
    else
      white++;
    count.at(i) = white;
  }
  if (red == 0)
    cout << 0 << endl;
  else
    cout << count.at(red - 1) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

int main() {
  int n;
  string s;
  cin >> n >> s;
  int red = 0, white = 0;
  vi count(n);
  rep(i, n) {
    if (s.at(i) == 'R')
      red++;
    else
      white++;
    count.at(i) = white;
  }
  cout << count.at(red - 1) << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <deque>
#include <iostream>

#define llong long long
#define rep(i, l, n) for (int(i) = (l); (i) < (n); (i++))
#define _min(a, b) ((a) < (b) ? (a) : (b))
#define _max(a, b) ((a) > (b) ? (a) : (b))
#define _abs(a, b) (((a) - (b)) > 0 ? ((a) - (b)) : ((b) - (a)))

using namespace std;

int main() {
  int N;
  cin >> N;
  char c[N];
  deque<int> white, red;
  rep(i, 0, N) {
    cin >> c[i];
    if (c[i] == 'W')
      white.push_back(i);
    else
      red.push_back(i);
  }

  if (white.empty() || red.empty()) {
    cout << "0";
    return 0;
  }

  int cnt = 0;
  rep(i, 0, N) {
    if (c[i] == 'W') {
      if (white.front() < red.back()) {
        swap(c[white.front()], c[red.back()]);
        white.pop_front();
        red.pop_back();
        cnt++;
      }
      if (white.empty() || red.empty()) {
        break;
      }
    }
  }
  cout << cnt;

  return 0;
}

Error: -11

Fixed submission:

#include <deque>
#include <iostream>

#define llong long long
#define rep(i, l, n) for (int(i) = (l); (i) < (n); (i++))
#define _min(a, b) ((a) < (b) ? (a) : (b))
#define _max(a, b) ((a) > (b) ? (a) : (b))
#define _abs(a, b) (((a) - (b)) > 0 ? ((a) - (b)) : ((b) - (a)))

using namespace std;

int main() {
  int N;
  cin >> N;
  char c[N];
  deque<int> white, red;
  rep(i, 0, N) {
    cin >> c[i];
    if (c[i] == 'W')
      white.push_back(i);
    else
      red.push_back(i);
  }

  int cnt = 0;
  rep(i, 0, N) {
    if (c[i] == 'W') {
      if (white.front() < red.back()) {
        swap(c[white.front()], c[red.back()]);
        white.pop_front();
        red.pop_back();
        cnt++;
      }
    }
  }
  cout << cnt;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF = 1e18;

vector<int> cost(1000000);
vector<vector<int>> data(1000000);

int main() {
  int n, count = 0;
  cin >> n;
  vector<char> c(n);
  stack<int> red;
  for (int i = 0; i < n; i++) {
    char x;
    cin >> x;
    if (x == 'R')
      red.push(i);
    c.at(i) = x;
  }
  for (int i = 0; i < n; i++) {
    if (red.empty())
      break;
    if (c.at(i) == 'W') {
      c.at(red.top()) = 'W';
      // cout << i << " " << red.top() << endl;
      c.at(i) = 'R';
      red.pop();

      count++;
    }
    if (red.empty())
      break;
    if (i >= red.top())
      break;
  }
  cout << count << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF = 1e18;

vector<int> cost(1000000);
vector<vector<int>> data(1000000);

int main() {
  int n, count;
  cin >> n;
  vector<char> c(n);
  stack<int> red;
  for (int i = 0; i < n; i++) {
    char x;
    cin >> x;
    if (x == 'R')
      red.push(i);
    c.at(i) = x;
  }
  for (int i = 0; i < n; i++) {
    if (c.at(i) == 'W') {
      c.at(red.top()) = 'W';
      c.at(i) = 'R';
      red.pop();

      count++;
    }
    if (red.empty())
      break;
    if (i >= red.top())
      break;
  }
  cout << count << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<char> colors(N);
  for (int i = 0; i < N; i++) {
    cin >> colors[i];
  }
  int counter = 0;
  int l = 0, r = N - 1;
  while (l < r) {
    if ((colors[l] == 'W') && (colors[r] == 'R')) {
      swap(colors[l], colors[r]);
      l++;
      r--;
      counter++;
    } else if ((colors[l] == 'R') && (colors[r] == 'R')) {
      while (colors[l] != 'W' && l < r)
        l++;
      if (l < r) {
        swap(colors[l], colors[r]);
        r--;
        l++;
        counter++;
      }
    } else if ((colors[l] == 'W') && (colors[r] == 'W')) {
      while (colors[r] != 'R' && l < r)
        r--;
      if (l < r) {
        swap(colors[l], colors[r]);
        r--;
        l++;
        counter++;
      }
    } else {
      r--;
      l++;
    }
  }
  cout << counter << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<char> colors(N);
  for (int i = 0; i < N; i++) {
    cin >> colors[i];
  }
  int counter = 0;
  int l = 0, r = N - 1;
  while (l < r) {
    if ((colors[l] == 'W') && (colors[r] == 'R')) {
      swap(colors[l], colors[r]);
      l++;
      r--;
      counter++;
    } else if ((colors[l] == 'R') && (colors[r] == 'R')) {
      while (colors[l] != 'W')
        l++;
      if (l < r) {
        swap(colors[l], colors[r]);
        r--;
        l++;
        counter++;
      }
    } else if ((colors[l] == 'W') && (colors[r] == 'W')) {
      while (colors[r] != 'R')
        r--;
      if (l < r) {
        swap(colors[l], colors[r]);
        r--;
        l++;
        counter++;
      }
    } else {
      r--;
      l++;
    }
  }
  cout << counter << endl;

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  queue<int> ind;
  int c = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == 'R')
      ind.push(i);
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W' && !ind.empty() && i < ind.front()) {
      int x = ind.front();
      ind.pop();
      s[x] = 'W';
      s[i] = 'R';
      c++;
    }
  }
  cout << c << "\\n";
  // cout<<s<<"\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  queue<int> ind;
  int c = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == 'R')
      ind.push(i);
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W' && i < ind.front()) {
      int x = ind.front();
      ind.pop();
      s[x] = 'W';
      s[i] = 'R';
      c++;
    }
  }
  cout << c << "\\n";
  // cout<<s<<"\\n";
}