prithivMLmods/Coder-Stat huggingface dataset:
Problem p02597 (C++)

Buggy submission (status: 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];
  }
  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;
}

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];
  }
  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;
}
Problem p02597 (C++)

Buggy submission (status: 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;
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}

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;
        }
        if (i == j - 1)
          mae = i;
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02597 (C++)

Buggy submission (status: 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');
  // 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;
}

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');
  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;
}
Problem p02597 (C++)

Buggy submission (status: 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);
    }
  }
  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;
}

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);
    }
  }
  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;
}
Problem p02597 (C++)

Buggy submission (status: 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 (s[ri] == 'R')
        break;
      ri--;
    }
    if (li >= ri)
      break;
    swap(s[li], s[ri]);
    ans++;
  }

  cout << ans << endl;
  return 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 (ri < 0)
        break;
      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 (status: 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(), j = 0; i >= 0 && j < white.size(); i--, j++) {
    if (red[i] < white[j])
      break;
    answer++;
  }
  cout << answer;

  return 0;
}

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() - 1, 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 (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using str = string;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
const double PI = 3.141592653589793238;
const ll mod = 1000000007;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define REP(i, m, n) for (int i = m; i < (int)n; i++)
#define all(a) (a).begin(), (a).end()
#define allg(a) (a).begin(), (a).end(), greator<>()
#define d20 std::setprecision(20)
#define veci vector<int>
#define vecll vector<long long int>
#define vecb vector<bool>
#define vecd vector<double>
template <typename T> T lcm(T a, T b) { return a / __gcd(a, b) * b; }

int main() {
  int n, ans = 0;
  str s;
  cin >> n >> s;
  size_t left = 0, right = s.size() - 1;
  while (left < right) {
    if (s[left] == 'W') {
      while (s[right] == 'W' && right >= 0) {
        right--;
      }
      // cout << "left = "<< left << " right = " << right << endl;
      if (left < right) {
        ans++;
      }
      right--;
    }
    left++;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using str = string;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
const double PI = 3.141592653589793238;
const ll mod = 1000000007;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define REP(i, m, n) for (int i = m; i < (int)n; i++)
#define all(a) (a).begin(), (a).end()
#define allg(a) (a).begin(), (a).end(), greator<>()
#define d20 std::setprecision(20)
#define veci vector<int>
#define vecll vector<long long int>
#define vecb vector<bool>
#define vecd vector<double>
template <typename T> T lcm(T a, T b) { return a / __gcd(a, b) * b; }

int main() {
  int n, ans = 0;
  str s;
  cin >> n >> s;
  int left = 0, right = s.size() - 1;
  while (left < right && right >= 0 && left < s.size()) {
    if (s[left] == 'W') {
      while (s[right] == 'W' && right >= 0) {
        right--;
      }
      // cout << "left = "<< left << " right = " << right << endl;
      if (left < right) {
        ans++;
      }
      right--;
    }
    left++;
  }
  cout << ans << endl;

  return 0;
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  int nn;
  cin >> nn;
  string s;
  cin >> s;
  vector<int> vec;
  bool mrk = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'W') {
      vec.push_back(i);
      mrk = 1;
    }
  }
  if (!mrk)
    cout << 0 << endl;
  else {
    int in = 0, cnt = 0;
    for (int j = s.size() - 1; j >= 0; j--) {
      if (s[j] == 'R') {
        if (vec[in] < j) {
          cnt++;
          swap(s[vec[in++]], s[j]);
          if (in >= s.size())
            break;
        } else {
          break;
        }
      }
    }
    cout << cnt << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  int nn;
  cin >> nn;
  string s;
  cin >> s;
  vector<int> vec;
  bool mrk = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'W') {
      vec.push_back(i);
      mrk = 1;
    }
  }
  if (!mrk)
    cout << 0 << endl;
  else {
    int in = 0, cnt = 0;
    for (int j = s.size() - 1; j >= 0; j--) {
      if (s[j] == 'R') {
        if (vec[in] < j) {
          cnt++;
          swap(s[vec[in++]], s[j]);
          if (in >= j)
            break;
          if (in >= s.size())
            break;
        } else {
          break;
        }
      }
    }
    cout << cnt << endl;
  }
}
Problem p02597 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define MOD 1e9 + 7;
using namespace std;
using ll = long long;

int main() {
  int n, ans = 0;
  cin >> n;
  char str;
  vector<int> c(n);
  rep(i, n) {
    cin >> str;
    if (str == 'W')
      c[i] = 0;
    else
      c[i] = 1;
  }
  int x = 0, y = n - 1;
  while (x != y) {
    if (c[x] == 0 && c[y] == 1) {
      x++;
      if (x != y)
        y--;
      ans++;
    } else if (c[x] == 0 && c[y] == 0)
      y--;
    else if (c[x] == 1 && c[y] == 1)
      x++;
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define MOD 1e9 + 7;
using namespace std;
using ll = long long;

int main() {
  int n, ans = 0;
  cin >> n;
  char str;
  vector<int> c(n);
  rep(i, n) {
    cin >> str;
    if (str == 'W')
      c[i] = 0;
    else
      c[i] = 1;
  }
  int x = 0, y = n - 1;
  while (x != y) {
    if (c[x] == 0 && c[y] == 1) {
      x++;
      if (x != y)
        y--;
      ans++;
    } else if (c[x] == 0 && c[y] == 0)
      y--;
    else if (c[x] == 1 && c[y] == 1)
      x++;
    else if (c[x] == 1 && c[y] == 0)
      x++;
  }
  cout << ans;
  return 0;
}
Problem p02597 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\n"

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  int rCount = 0;
  for (int i = 0; i < N; ++i) {
    if (s[i] == 'R')
      ++rCount;
  }

  int count = 0;
  for (int i = 0; i < s.length(); ++i) {
    if (s[i] == 'W') {
      for (int j = max(i + 1, rCount); j < s.length(); ++j) {
        if (s[j] == 'R') {
          swap(s[i], s[j]);
          ++count;
          break;
        }
      }
    }
  }

  cout << count << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\n"

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  int rCount = 0;
  for (int i = 0; i < N; ++i) {
    if (s[i] == 'R')
      ++rCount;
  }

  int count = 0;
  for (int i = 0; i < rCount; ++i) {
    if (s[i] == 'W')
      ++count;
  }

  cout << count << endl;

  return 0;
}
Problem p02597 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()

int main() {
  ll N;
  cin >> N;
  string S;
  cin >> S;
  ll ans = 0;
  ll red = 0;
  for (ll i = 0; i < N; i++) {
    if (S[i] == 'W') {
      for (ll j = red; j < N - i - 1; j++) {
        if (S[N - 1 - j] == 'R') {
          ans++;
          red = j + 1;
          break;
        }
      }
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()

int main() {
  ll N;
  cin >> N;
  string S;
  cin >> S;
  ll ans = 0;
  ll red = 0;
  for (ll i = 0; i < N; i++) {
    if (S[i] == 'W') {
      for (ll j = red; j < N - i - 1; j++) {
        if (S[N - 1 - j] == 'R') {
          ans++;
          red = j + 1;
          break;
        }
        if (j == N - i - 2) {
          red = j + 1;
        }
      }
      if (red == N - i - 1) {
        break;
      }
    }
  }

  cout << ans << endl;
}
Problem p02597 (C++)

Buggy submission (status: 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' && 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;
}

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') {
      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;
}
Problem p02597 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define PI acos(-1)
#define INF 0x3f3f3f
#define ll long long
#define pb push_back
#define mp make_pair
#define pair pair<int, int>
#define C(a) cout << a << endl;
#define CY cout << "YES" << endl;
#define CN cout << "NO" << endl;
#define Cy cout << "Yes" << endl;
#define Cn cout << "No" << endl;
#define ull unsigned long long
#define me(a) memset(a, 0, sizeof(a));
#define my(a, b) memcpy(a, b, sizeof(b));
#define cio ios::sync_with_stdio(false)
ll mod = 1e9 + 7;
ll gcd(ll a, ll b) { return (a % b == 0) ? b : gcd(b, a % b); }
char s[200005];
int solve(int n) {
  int cnt = 0, ans = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'R')
      cnt++;
  }
  for (int i = 1; i <= cnt; i++) {
    if (s[i] == 'W')
      ans++;
  }
  cout << ans << endl;
}
int main() {
  int n;
  cin >> n;
  scanf("%s", s + 1);
  solve(n);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define PI acos(-1)
#define INF 0x3f3f3f
#define ll long long
#define pb push_back
#define mp make_pair
#define pair pair<int, int>
#define C(a) cout << a << endl;
#define CY cout << "YES" << endl;
#define CN cout << "NO" << endl;
#define Cy cout << "Yes" << endl;
#define Cn cout << "No" << endl;
#define ull unsigned long long
#define me(a) memset(a, 0, sizeof(a));
#define my(a, b) memcpy(a, b, sizeof(b));
#define cio ios::sync_with_stdio(false)
ll mod = 1e9 + 7;
ll gcd(ll a, ll b) { return (a % b == 0) ? b : gcd(b, a % b); }
char s[200005];
inline void solve(int n) {
  int cnt = 0, ans = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'R')
      cnt++;
  }
  for (int i = 1; i <= cnt; i++) {
    if (s[i] == 'W')
      ans++;
  }
  cout << ans << endl;
}
int main() {
  int n;
  cin >> n;
  scanf("%s", s + 1);
  solve(n);
  return 0;
}
Problem p02597 (C++)

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

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

  int w = s.find_first_of("W");
  int r = s.find_last_of("R");

  if (r == string::npos || w == string::npos) {
    cout << "0" << endl;
    return 0;
  }

  int ans = 0;
  while (w < r) {
    ++ans;
    s[r] = 'W';
    s[w] = 'R';

    w = s.find_first_of("W");
    r = s.find_last_of("R");
  }
  cout << ans << endl;
}


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

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

  int rcnt = count(s.cbegin(), s.cend(), 'R');
  int wcnt = count(s.cbegin(), s.cbegin() + rcnt, 'W');
  cout << wcnt << endl;
}
Problem p02597 (C++)

Buggy submission (status: 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 (r[p] > w[i] && p >= 0) {
      ans++;
      p--;
    }
  }
  cout << ans << endl;
}

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 (p >= 0 && r[p] > w[i]) {
      ans++;
      p--;
    }
  }
  cout << ans << endl;
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#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;
}


Fixed submission:
#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;
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define endl "\n"
#define sz(x) ((ll)(x).size())
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define prt(x) cout << (x)
#define prtl(x) cout << (x) << endl
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (ll i = a; i > b; i--)
#define mp(a, b) make_pair(a, b)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define debug(v) cout << #v << ": " << v << endl;
#define fst first
#define snd second
using namespace std;
using vi = vector<int>;
using vll = vector<long long int>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vc = vector<char>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<long long int>>;
using vvc = vector<vector<char>>;
using ll = long long int;
using P = pair<long long int, long long int>;
using Map = map<long long int, long long int>;
ll INF = 1LL << 60;
ll M = 1000000007;

int main() {
  ll n;
  cin >> n;
  vc s(n);
  rep(i, 0, n) { cin >> s[i]; }
  vll w(0);
  priority_queue<ll> r;
  rep(i, 0, n) {
    if (s[i] == 'R') {
      r.push(i + 1);
    } else {
      w.emplace_back(i + 1);
    }
  }
  ll c = 0;
  for (auto x : w) {
    if (r.top() < x) {
      break;
    } else {
      r.pop();
      c++;
    }
  }
  prtl(c);
}


Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define endl "\n"
#define sz(x) ((ll)(x).size())
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define prt(x) cout << (x)
#define prtl(x) cout << (x) << endl
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (ll i = a; i > b; i--)
#define mp(a, b) make_pair(a, b)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define debug(v) cout << #v << ": " << v << endl;
#define fst first
#define snd second
using namespace std;
using vi = vector<int>;
using vll = vector<long long int>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vc = vector<char>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<long long int>>;
using vvc = vector<vector<char>>;
using ll = long long int;
using P = pair<long long int, long long int>;
using Map = map<long long int, long long int>;
ll INF = 1LL << 60;
ll M = 1000000007;

int main() {
  ll n;
  cin >> n;
  vc s(n);
  rep(i, 0, n) { cin >> s[i]; }
  vll w(0);
  priority_queue<ll> r;
  rep(i, 0, n) {
    if (s[i] == 'R') {
      r.push(i + 1);
    } else {
      w.emplace_back(i + 1);
    }
  }
  ll c = 0;
  if (r.empty()) {
    prtl(0);
    return 0;
  }
  for (auto x : w) {
    if (r.top() < x) {
      break;
    } else {
      r.pop();
      c++;
    }
  }
  prtl(c);
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = int64_t;
const int MAX = 2e5;

int32_t main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> R;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      R.emplace_back(i);
  }
  int cnt = 0;
  for (int i = 0; i < R.size() - 1; i++) {
    int x = R[i + 1] - R[i] - 1;
    if (x > 0) {
      cnt += x;
      for (int j = R[i] + 1; j < R[i + 1]; j++)
        s[j] = 'R';
      while (x--) {
        s[R.back()] = 'W';
        R.pop_back();
      }
    }
  }
  int x = 0;
  for (auto it : s) {
    if (it == 'W')
      x++;
    else if (x > 0)
      cnt += x, x = 0;
  }

  cout << cnt;

  return 0;
}

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

using ll = int64_t;
const int MAX = 2e5;

int32_t main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = count(s.begin(), s.end(), 'R');
  int ans = 0;
  for (int i = 0; i < cnt; i++)
    if (s[i] == 'W')
      ans++;
  cout << ans;

  return 0;
}
Problem p02597 (C++)

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

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

  int n;
  cin >> n;
  string s;
  int w = 0;
  for (i = 1; i <= n; i++) {
    cin >> s[i];
    if (s[i] == 'W')
      w++;
  }
  int ans = w;
  for (i = n; i > n - w; i--) {
    if (s[i] == 'W')
      ans--;
  }
  cout << ans << endl;
  return 0;
}

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

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

  int n;
  cin >> n;
  char s[n];
  int w = 0;
  for (i = 1; i <= n; i++) {
    cin >> s[i];
    if (s[i] == 'W')
      w++;
  }
  int ans = w;
  for (i = n; i > n - w; i--) {
    if (s[i] == 'W')
      ans--;
  }
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back

// g++ -std=c++17 -Wshadow -Wall -g -fsanitize=address -fsanitize=undefined
// -D_GLIBCXX_DEBUG -o

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      v.pb(i);
  }
  int ans = 0;
  int i = 0;
  while (i < n - 1) {
    if (s[i] == 'W') {
      if (v.back() > i) {
        swap(s[i], s[v.back()]);
        v.pop_back();
        ans++;
      } else if (s[i + 1] == 'R') {
        s[i] = 'R';
        ans++;
      }
    }
    i++;
  }
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back

// g++ -std=c++17 -Wshadow -Wall -g -fsanitize=address -fsanitize=undefined
// -D_GLIBCXX_DEBUG -o

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      v.pb(i);
  }
  int ans = 0;
  int i = 0;
  while (i < n - 1) {
    if (s[i] == 'W') {
      if (!v.empty() && v.back() > i) {
        swap(s[i], s[v.back()]);
        v.pop_back();
        ans++;
      } else if (s[i + 1] == 'R') {
        s[i] = 'R';
        ans++;
      }
    }
    i++;
  }
  cout << ans << "\n";
  return 0;
}
Problem p02597 (C++)

Buggy submission (status: 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 + n_red] == 'R')
      w_diff++;
  }
  std::cout << std::max(r_diff, w_diff) << std::endl;
}

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] == 'R')
      w_diff++;
  }
  std::cout << std::max(r_diff, w_diff) << std::endl;
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int N;
  string S;
  cin >> N;
  cin >> S;

  priority_queue<int> red;
  priority_queue<int, vector<int>, greater<int>> white;
  for (int i = 0; i < N; i++) {
    if (S[i] == 'R') {
      red.push(i);
    } else {
      white.push(i);
    }
  }

  int min_index_white = N;
  int max_index_red = -1;
  int count = 0;
  while (!white.empty()) {
    min_index_white = white.top();
    max_index_red = red.top();

    if (min_index_white > max_index_red) {
      break;
    }

    white.pop();
    red.pop();
    count++;
  }

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

Fixed submission:
#include <bits/stdc++.h>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int N;
  string S;
  cin >> N;
  cin >> S;

  priority_queue<int> red;
  priority_queue<int, vector<int>, greater<int>> white;
  for (int i = 0; i < N; i++) {
    if (S[i] == 'R') {
      red.push(i);
    } else {
      white.push(i);
    }
  }

  int min_index_white = N;
  int max_index_red = -1;
  int count = 0;
  while (!white.empty()) {

    if (red.empty()) {
      break;
    }

    min_index_white = white.top();
    max_index_red = red.top();

    if (min_index_white > max_index_red) {
      break;
    }

    white.pop();
    red.pop();
    count++;
  }

  cout << count << endl;
  return 0;
}
Problem p02597 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int func(int n, int c[], int r_num) {
  int re = 0;
  for (int i = 0; i < r_num; i++) {
    if (c[i] != 1)
      re++;
  }
  return re;
}

int main(void) {
  int n;
  cin >> n;
  int c[n];
  int r_num = 0;
  char color;
  for (int i = 0; i < n; i++) {
    cin >> color;
    if (color == 'R') {
      c[i] = 1;
      r_num++;
    } else if (color == 'W')
      c[i] = 2;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = func(n, c, r_num);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int func(int n, int c[], int r_num) {
  int re = 0;
  for (int i = 0; i < r_num; i++) {
    if (c[i] != 1)
      re++;
  }
  return re;
}

int main(void) {
  int n;
  cin >> n;
  int c[n];
  int r_num = 0;
  char color;
  for (int i = 0; i < n; i++) {
    cin >> color;
    if (color == 'R') {
      c[i] = 1;
      r_num++;
    } else if (color == 'W')
      c[i] = 2;
  }
  int ans = func(n, c, r_num);
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define fr(i, n) for (i = 0; i < n; i++)
#define F first
#define S second
#define fast std::ios_base::sync_with_stdio(false)
#define mod 1000000007
#define pi 3.14159265
int main() {
  fast;
  lli n;
  cin >> n;
  string s;
  cin >> s;
  lli i, ans = mod, countr = 0, countw = 0;
  fr(i, s.length()) {
    if (s[i] == 'R')
      countr++;
    else
      countw++;
  }
  ans = min(countw, countr);
  lli count = 0, count1 = 0;
  fr(i, countr) {
    if (s[i] == 'W')
      count++;
  }
  for (i = s.length() - 1; i >= s.length() - countw; i--) {
    if (s[i] == 'R')
      count1++;
  }
  ans = min(ans, count);
  ans = min(ans, count1);
  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define fr(i, n) for (i = 0; i < n; i++)
#define F first
#define S second
#define fast std::ios_base::sync_with_stdio(false)
#define mod 1000000007
#define pi 3.14159265
int main() {
  fast;
  lli n;
  cin >> n;
  string s;
  cin >> s;
  lli i, ans = mod, countr = 0, countw = 0;
  fr(i, s.length()) {
    if (s[i] == 'R')
      countr++;
    else
      countw++;
  }
  ans = min(countw, countr);
  if (ans == 0) {
    cout << 0;
    return 0;
  }
  lli count = 0, count1 = 0;
  fr(i, countr) {
    if (s[i] == 'W')
      count++;
  }
  for (i = s.length() - 1; i >= s.length() - countw; i--) {
    if (s[i] == 'R')
      count1++;
  }
  ans = min(ans, count);
  ans = min(ans, count1);
  cout << ans;

  return 0;
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#pragma warning(disable : 4996)
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <math.h>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n, rtow = 0, wtor = 0;
  string s, s2;
  cin >> n >> s;
  int k = s.size();
  rep(i, k) {
    s2[i] = 'W';
    if (s[i] == 'R')
      rtow++;
  }

  int ans = rtow;
  rep(i, k) {
    s2[i] = 'R';
    if (s[i] == 'W')
      wtor++;
    if (s[i] == 'R')
      rtow--;
    if (max(wtor, rtow) <= ans)
      ans = max(rtow, wtor);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#pragma warning(disable : 4996)
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <math.h>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n, rtow = 0, wtor = 0;
  string s, s2;
  cin >> n >> s;
  s2 = s;
  int k = s.size();
  rep(i, k) {
    s2[i] = 'W';
    if (s[i] == 'R')
      rtow++;
  }

  int ans = rtow;
  rep(i, k) {
    s2[i] = 'R';
    if (s[i] == 'W')
      wtor++;
    if (s[i] == 'R')
      rtow--;
    if (max(wtor, rtow) <= ans)
      ans = max(rtow, wtor);
  }
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long N;
  cin >> N;
  std::string S;
  cin >> S;

  long long ans = 0;
  long long J = 0;
  for (long long i = 0; i < N; ++i) {
    if (S[i] == 'W') {
      for (long long j = J; j < N; ++j) {
        if (i + j >= N) {
          break;
        }
        if (S[N - 1 - j] == 'R') {
          ans += 1;
          J = j + 1;
          break;
        }
      }

      if (i + J + 1 > N) {
        break;
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long N;
  cin >> N;
  std::string S;
  cin >> S;

  long long ans = 0;
  long long J = 0;
  for (long long i = 0; i < N; ++i) {
    if (S[i] == 'W') {
      for (long long j = J; j < N; ++j) {
        if (i + j >= N) {
          J = j;
          break;
        }
        if (S[N - 1 - j] == 'R') {
          ans += 1;
          J = j + 1;
          break;
        }
      }

      if (i + J + 1 > N) {
        break;
      }
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02597 (C++)

Buggy submission (status: 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' && i < ind.front()) {
      int x = ind.front();
      ind.pop();
      s[x] = 'W';
      s[i] = 'R';
      c++;
    }
  }
  cout << c << "\n";
  // cout<<s<<"\n";
}

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' && !ind.empty() && i < ind.front()) {
      int x = ind.front();
      ind.pop();
      s[x] = 'W';
      s[i] = 'R';
      c++;
    }
  }
  cout << c << "\n";
  // cout<<s<<"\n";
}
Problem p02597 (C++)

Buggy submission (status: 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 (!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;
}


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 (!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;
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  string str;
  int N, i, j, count = 0;
  cin >> N >> str;
  i = 0;
  j = N - 1;
  while (i < j) {
    if (str[i] == 'W' && str[j] == 'R') {
      count += 1;
      i += 1;
      j -= 1;
    } else if (str[i] == 'W' && str[j] == 'W') {
      j -= 1;
    } else if (str[i] == 'R' && str[j] == 'R') {
      i += 1;
    } else {
      i -= 1;
      j -= 1;
    }
  }
  cout << count << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  string str;
  int N, i, j, count = 0;
  cin >> N >> str;
  i = 0;
  j = N - 1;
  while (i < j) {
    if (str[i] == 'W' && str[j] == 'R') {
      count += 1;
      i += 1;
      j -= 1;
    } else if (str[i] == 'W' && str[j] == 'W') {
      j -= 1;
    } else if (str[i] == 'R' && str[j] == 'R') {
      i += 1;
    } else {
      i += 1;
      j -= 1;
    }
  }
  cout << count << endl;
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
int N;
string str;
int S[200001];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  cin >> str;
  rep(i, N) S[i] = str[i] == 'R' ? 0 : 1;
  int numzero = 0;
  int numswap = 0;
  rep(i, N) if (S[i] == 0) numzero++;
  rep(i, numzero) if (S[i] == 1) numswap++;
  int ans = numswap;
  int count = 0;
  for (int x = -1; x < N; x++) {
    if (x >= 0 && S[x] == 1) {
      // S[x] = 0;
      numzero--;
      count++;
      if (x != numzero && S[numzero] == 1)
        numswap--;
    }
    int cnt = count;
    int ns = numswap;
    int nz = numzero;
    int y = N;
    ans = min(ans, cnt + ns);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
int N;
string str;
int S[200001];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  cin >> str;
  rep(i, N) S[i] = str[i] == 'R' ? 0 : 1;
  int numzero = 0;
  int numswap = 0;
  rep(i, N) if (S[i] == 0) numzero++;
  rep(i, numzero) if (S[i] == 1) numswap++;
  int ans = numswap;
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, cnt = 0, res = 0;
  string s;
  cin >> n >> s;
  int k = n - 1;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      cnt++;
  }
  while (s[k] != 'R' && k > 0)
    k--;
  for (int i = n - cnt - k; i < n; i++) {
    if (s[i] == 'R')
      res++;
  }
  cout << min(res, cnt + k - 1);
}

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

int main() {
  int n, cnt = 0, res = 0;
  string s;
  cin >> n >> s;
  int k = n - 1;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      cnt++;
  }
  while (s[k] != 'R' && k > 0)
    k--;
  for (int i = n - cnt; i < n; i++) {
    if (s[i] == 'R')
      res++;
  }
  cout << min(res, cnt + k - 1);
}
Problem p02597 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  cout.precision(30);
#define ll long long
#define pi 3.14159265359
using namespace std;

int main() {
  IOS;
  int n;
  cin >> n;
  string s;
  cin >> s;
  int i = 0, j = n - 1;
  int c = 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]);
      c++;
    }
  }
  cout << c;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  cout.precision(30);
#define ll long long
#define pi 3.14159265359
using namespace std;

int main() {
  IOS;
  int n;
  cin >> n;
  string s;
  cin >> s;
  int i = 0, j = n - 1;
  int c = 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]);
      c++;
    }
  }
  cout << c;
  return 0;
}
Problem p02597 (C++)

Buggy submission (status: 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 < 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;
}

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 < 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;
}