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

#include <bits/stdc++.h>

int main() {
  int n;
  int a = 0;    // R->Wの数
  int b = 0;    // W->Rの数
  int n_change; // 交換する回数
  std::string s;

  std::cin >> n >> s;

  for (int i = 0; i < n; i++)
    if (s[i] == 'R')
      a++; // 赤の数でaを初期化(交換回数は赤の数)

  // 境界を一番左においた時の交換回数
  n_change = std::max(a, b);

  // はじめの完成形はすべて白. 境界を左から右に移動させ左から赤色していく
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      b++; // b(W->R)が一つ増える
    else
      a--; // a(R->W)がひとつ減る

    // 最小の交換回数を更新
    if (n_change > std::max(a, b))
      n_change = std::max(a, b);
  }

  std::cout << n_change << std::endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

int main() {
  int n;
  int a = 0;             // R->Wの数
  int b = 0;             // W->Rの数
  int n_change = 999999; // 交換する回数
  std::string s;

  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> s[i];
    if (s[i] == 'R')
      a++; // 赤の数でaを初期化(交換回数は赤の数)
  }

  // はじめの完成形はすべて白. 左から順に赤色していく
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      b++; // b(W->R)が一つ増える
    else
      a--; // a(R->W)がひとつ減る

    // 最小の交換回数を更新
    if (n_change > std::max(a, b))
      n_change = std::max(a, b);
  }

  std::cout << n_change << std::endl;

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

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
#define LINF 1e18
typedef long long ll;

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  deque<char> S;
  REP(i, N) { S.push_back(s[i]); }
  int ans = 0;
  while (1) {
    if (S.empty())
      break;
    while (!S.empty()) {
      if (S.front() == 'R')
        S.pop_front();
      else
        break;
    }
    if (S.empty())
      break;

    while (!S.empty()) {
      if (S.back() == 'W')
        S.pop_back();
      else
        break;
    }
    if (S.empty())
      break;

    ans++;
    S.pop_front();
    S.pop_back();
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
#define LINF 1e18
typedef long long ll;

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  deque<char> S;
  REP(i, N) { S.push_back(s[i]); }
  int ans = 0;
  while (1) {
    if (S.empty())
      break;
    while (S.front() == 'R' && !S.empty())
      S.pop_front();
    if (S.empty())
      break;

    while (S.back() == 'W' && !S.empty())
      S.pop_back();
    if (S.empty())
      break;

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

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void solve() {
  ll N;
  cin >> N;
  string S;
  cin >> S;
  ll ans = 0;
  ll last = N - 1;
  rep(i, N) {
    if (S[i] == 'W') {
      rrep(j, last, i + 1) {
        if (S[j] == 'R') {
          ans++;
          char tmp = S[i];
          S[i] = S[j];
          S[j] = tmp;
          last = j;
          break;
        }
      }
      if (S[i] == 'W') {
        break;
      }
    }
  }
  cout << ans << '\\n';
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void solve() {
  ll N;
  cin >> N;
  string S;
  cin >> S;
  ll ans = 0;
  ll last = N - 1;
  rep(i, N) {
    if (S[i] == 'W') {
      rrep(j, last, i + 1) {
        if (S[j] == 'R') {
          ans++;
          char tmp = S[i];
          S[i] = S[j];
          S[j] = tmp;
          break;
        }
      }
    }
  }
  cout << ans << '\\n';
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  char c[200000];
  cin >> n;
  cin >> c;
  int rs = 0, ws = 0;
  rep(i, n) {
    if (c[i] == 'R')
      rs++;
    else
      ws++;
  }
  int diff = 0;
  for (int i = 0; i < rs; i++) {
    if (c[i] != 'R')
      diff++;
  }

  for (int i = rs; i < n; i++) {
    if (c[i] != 'W')
      diff++;
  }
  cout << diff / 2 << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n;
  char c[20000];
  cin >> n;
  cin >> c;
  int rs = 0, ws = 0;
  rep(i, n) {
    if (c[i] == 'R')
      rs++;
    else
      ws++;
  }
  int diff = 0;
  for (int i = 0; i < rs; i++) {
    if (c[i] != 'R')
      diff++;
  }

  for (int i = rs; i < n; i++) {
    if (c[i] != 'W')
      diff++;
  }
  cout << diff / 2 << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long
#define ld long double
#define endl "\\n"
using namespace std;

int32_t main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int ans = 0;
  int i = 0, j = n - 1;
  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;
  cout << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long
#define ld long double
#define endl "\\n"
using namespace std;

int32_t main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int ans = 0;
  int i = 0, j = n - 1;
  while (i < j) {
    if (s[i] == 'R') {
      i++;
    } else {
      while (s[j] != 'R')
        j--;
      swap(s[i], s[j]);
      ans++;
    }
  }
  cout << ans;
  cout << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n, a[200010], b[200010], ans = 1e9, x, y;
string s;
int main() {
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    if (i != 0)
      a[i] = a[i - 1];
    if (s[i] == 'W')
      a[i]++;
    if (i != 0)
      b[i] = b[i - 1];
    if (s[i] == 'R')
      b[i]++;
  }
  a[n] = a[n - 1];
  b[n] = b[n - 1];
  for (int i = 0; i <= n; i++)
    ans = min(ans, max(a[i], b[n] - b[i]));
  for (int i = 0; i < n; i++)
    if (s[i] == 'W')
      x++;
  if (x == 0 || x == n) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = n - x; i < n; i++)
    if (s[i] == 'R')
      y++;
  cout << min(ans, y + max(0, x - y - y)) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, a, b, ans = 1e9;
string s;
int main() {
  cin >> n >> s;
  for (int i = 0; i <= n; i++) {
    a = 0;
    b = 0;
    for (int j = 0; j < i; j++)
      if (s[j] == 'W')
        a++;
    for (int j = i; j < n; j++)
      if (s[j] == 'R')
        b++;
    ans = min(ans, max(a, b));
  }
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }

int n;
string s;

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

  int rcnt = 0;
  for (int i = 0; i < n; ++i)
    if (s[i] == 'R')
      rcnt++;
  int cnt = 0;
  for (int i = 0; i < rcnt; ++i)
    if (s[i] == 'W')
      cnt++;
  cout << cnt << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }

int n;
string s;

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

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

  for (int i = 0; i < n - 1; ++i) {
    if (s[i] == 'W' && s[i + 1] == 'R')
      cnt++;
  }
  cout << cnt << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, ans = 0, i;
  string s;
  char c;
  cin >> n >> s;
  ans = count(s.begin(), s.end(), 'R');
  if (ans == n)
    cout << 0;
  else {
    int cnt = ans;
    for (i = 0; i < cnt; i++) {
      if (s[i] == 'R')
        ans--;
    }
    cout << ans;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, cnt, ans = 0, i, j;
  string s;
  char c;
  cin >> n >> s;
  for (i = 0; i < n; i++) {
    if (s[i] == 'W') {
      j = n - 1, cnt = 0;
      while (s[j] != 'R' && j > i)
        j--;
      if (j == i)
        break;
      else {
        c = s[i];
        s[i] = s[j];
        s[j] = c;
        ans++;
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  ll ans = 0, l = 0, r = s.length() - 1;
  while (l < r) {
    while (l < s.length() && s[l] != 'W')
      l++;
    while (r >= 0 && s[r] != 'R')
      r--;
    if (l >= r)
      break;
    ans++;
    swap(s[l], s[r]);
    l++;
    r--;
  }
  cout << ans << endl;
}

int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}

Error: 0

Fixed submission:

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

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  ll ans = 0, l = 0, r = s.length() - 1;
  while (l < r) {
    while (s[l] != 'W')
      l++;
    while (s[r] != 'R')
      r--;
    if (l >= r)
      break;
    ans++;
    swap(s[l], s[r]);
    l++;
    r--;
  }
  cout << ans << endl;
}

int main() {
  ll t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

// DPCoded
//

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef set<int> si;
typedef set<string> ss;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef pair<int, int> ii;

// #define FILEINOUT
#ifdef FILEINOUT
ifstream In("Input.txt");
ofstream Out("Output.txt");
#else
#define In cin
#define Out cout
#endif

void print(vb v) {
  for (bool t : v) {
    if (t)
      Out << "W";
    else
      Out << "R";
  }
  Out << endl;
}

int main() {

  int n;
  In >> n;

  vb v;

  for (int i = 0; i < n; i++) {
    char c;
    In >> c;
    if (c == 'R')
      v.push_back(false);
    else
      v.push_back(true);
  }

  int l = 0;
  int r = n - 1;
  int ans = 0;

  while (l < r) {
    while (l < n - 1 && !v[l])
      l++;
    while (0 <= r && v[r])
      r--;

    if (l >= r)
      continue;

    // swap(v[l], v[r]);
    // print(v);
    ans++;
    l++;
    r--;
  }

  Out << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

// DPCoded
//

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef set<int> si;
typedef set<string> ss;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef pair<int, int> ii;

// #define FILEINOUT
#ifdef FILEINOUT
ifstream In("Input.txt");
ofstream Out("Output.txt");
#else
#define In cin
#define Out cout
#endif

void print(vb v) {
  for (bool t : v) {
    if (t)
      Out << "W";
    else
      Out << "R";
  }
  Out << endl;
}

int main() {

  int n;
  In >> n;

  vb v;

  for (int i = 0; i < n; i++) {
    char c;
    In >> c;
    if (c == 'R')
      v.push_back(false);
    else
      v.push_back(true);
  }

  int l = 0;
  int r = n - 1;
  int ans = 0;

  while (l < r) {
    while (!v[l])
      l++;
    while (v[r])
      r--;

    if (l >= r)
      continue;

    // swap(v[l], v[r]);
    // print(v);
    ans++;
    l++;
    r--;
  }

  Out << ans << endl;

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

// const int N=1e5;
#define INF 1e9
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
int n;
string s;
void sol() {
  cin >> n;
  cin >> s;
  int red = 0, w = 0, swp = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'W')
      ++w;
    else
      ++red;
  }
  int cnt = min(red, w);
  for (int i = 0; i < red; ++i) {
    if (s[i] == 'W')
      ++swp;
  }
  cnt = min(cnt, swp);
  cout << cnt << "\\n";
}

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

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin >> t;
  while (t--) {
    sol();
  }

  return 0;
}

Error: TLE

Fixed submission:

// const int N=1e5;
#define INF 1e9
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
int n;
string s;
void sol() {
  cin >> n;
  cin >> s;
  int red = 0, w = 0, swp = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'W')
      ++w;
    else
      ++red;
  }
  int cnt = min(red, 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';
          ++swp;
          break;
        }
      }
    }
  }
  cnt = min(cnt, swp);
  cout << cnt << "\\n";
}

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

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin >> t;
  while (t--) {
    sol();
  }

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

#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <unordered_set>
// #define local
#ifdef local
#include "dbg-macro/dbg.h"
#endif
#define ll long long
#define pi (acos(-1))
#define rep(i, n)                                                              \\
  for (unsigned long long i = 0; i < (unsigned long long)(n); ++i)
#define vec std::vector
using namespace std;
std::vector<unsigned ll> genprimevec(const unsigned ll N);
int main() {
  int n;
  cin >> n;
  vec<char> s(n);
  int r = 0, w = 0;
  rep(i, n) {
    cin >> s[i];
    if (s[i] == 'R') {
      ++r;
    } else {
      ++w;
    }
  }
  int lw = 0, rr = r;
  int ans;

  ans = r;
#ifdef local
  dbg(r, w, lw, rr, ans);

#endif
  rep(i, n) {
    if (s[i] == 'R') {
      --rr;
    } else {
      ++lw;
    }
#ifdef local
    dbg(i, lw, rr);
#endif
    ans = min(ans, max(lw, rr));
  }

  ans = min(ans, w);
  cout << ans << endl;

  return 0;
}

ll extgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  ll d = extgcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}

std::vector<unsigned ll> genprimevec(const unsigned ll N) {
  std::vector<bool> is_prime(N + 1);
  for (unsigned ll i = 0; i <= N; i++) {
    is_prime[i] = true;
  }
  std::vector<unsigned ll> P;
  for (unsigned ll i = 2; i <= N; i++) {
    if (is_prime[i]) {
      for (unsigned ll j = 2 * i; j <= N; j += i) {
        is_prime[j] = false;
      }
      P.emplace_back(i);
    }
  }
  return P;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <unordered_set>
// #define local
#ifdef local
#include "dbg-macro/dbg.h"
#endif
#define ll long long
#define pi (acos(-1))
#define rep(i, n)                                                              \\
  for (unsigned long long i = 0; i < (unsigned long long)(n); ++i)
using namespace std;
std::vector<unsigned ll> genprimevec(const unsigned ll N);
int main() {
  int n;
  cin >> n;
  string s;
  int r = 0, w = 0;
  rep(i, n) {
    cin >> s[i];
    if (s[i] == 'R') {
      ++r;
    } else {
      ++w;
    }
  }
  int lw = 0, rr = r;
  int ans;

  ans = r;
#ifdef local
  dbg(r, w, lw, rr, ans);

#endif
  rep(i, n) {
    if (s[i] == 'R') {
      --rr;
    } else {
      ++lw;
    }
#ifdef local
    dbg(i, lw, rr);
#endif
    ans = min(ans, max(lw, rr));
  }

  ans = min(ans, w);
  cout << ans << endl;

  return 0;
}

ll extgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  ll d = extgcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}

std::vector<unsigned ll> genprimevec(const unsigned ll N) {
  std::vector<bool> is_prime(N + 1);
  for (unsigned ll i = 0; i <= N; i++) {
    is_prime[i] = true;
  }
  std::vector<unsigned ll> P;
  for (unsigned ll i = 2; i <= N; i++) {
    if (is_prime[i]) {
      for (unsigned ll j = 2 * i; j <= N; j += i) {
        is_prime[j] = false;
      }
      P.emplace_back(i);
    }
  }
  return P;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T, class C> void chmax(T &a, C b) { a > b ?: a = b; }
template <class T, class C> void chmin(T &a, C b) { a < b ?: a = b; }
int main() {
  ll N;
  cin >> N;
  priority_queue<ll> R;
  priority_queue<ll, vector<ll>, greater<ll>> W;
  for (ll i = 0; i < N; i++) {
    char c;
    cin >> c;
    if (c == 'R')
      R.push(i);
    else if (c == 'W')
      W.push(i);
  }
  if (R.size() == 0 || W.size() == 0) {
    cout << "0" << endl;
    return 0;
  }
  ll cnt = 0;
  while (1) {
    ll r = R.top();
    ll w = W.top();
    if (r > w) {
      R.pop();
      W.pop();
      R.push(w);
      W.push(r);
      cnt++;
    } else {
      break;
    }
  }
  cout << cnt << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T, class C> void chmax(T &a, C b) { a > b ?: a = b; }
template <class T, class C> void chmin(T &a, C b) { a < b ?: a = b; }
int main() {
  ll N;
  cin >> N;
  priority_queue<ll> R;
  priority_queue<ll, vector<ll>, greater<ll>> W;
  for (ll i = 0; i < N; i++) {
    char c;
    cin >> c;
    if (c == 'R')
      R.push(i);
    else if (c == 'W')
      W.push(i);
  }
  if (R.size() == 0 || W.size() == 0) {
    cout << "0" << endl;
  }
  ll cnt = 0;
  while (1) {
    ll r = R.top();
    ll w = W.top();
    if (r > w) {
      R.pop();
      W.pop();
      R.push(w);
      W.push(r);
      cnt++;
    } else {
      break;
    }
  }
  cout << cnt << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define fi first
#define se second
using namespace std;
void solve() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll r = 0;
  for (ll i = 0; i < n; i++)
    if (s[i] == 'R')
      r++;
  ll cnt = 0;
  for (ll i = 0; i < r; i++) {
    if (s[i] == 'W')
      cnt++;
  }
  cout << cnt << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define pb push_back
#define fi first
#define se second
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll rb = 0, we = n - 1;
  for (ll i = 0; i < n; i++)
    if (s[i] == 'W')
      break;
    else
      rb++;
  for (ll i = n - 1; i >= 0; i--)
    if (s[i] == 'R')
      break;
    else
      we++;
  if (rb == n || we == n || rb + we == n) {
    cout << 0;
    return 0;
  }
  ll ans = 0;
  for (ll i = we; i >= 0;) {
    ll r = 0, w = 0;
    while (i >= 0 && s[i] == 'R')
      r++, i--;
    while (i >= 0 && s[i] == 'W')
      w++, i--;
    // cout<<r<<" "<<w<<" ";
    ans += min(r, w);
  }
  cout << ans;
  return 0;
}