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

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

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

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

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

Buggy submission (status: Time Limit Exceeded):
#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 - 1); i >= 0; --i)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;

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

  int l = 0, r = N - 1;
  int ans = 0;
  while (l < r) {
    bool flg = 0;
    for (int i = l; i < r; ++i) {
      if (S[i] == 'W' && S[i + 1] == 'R')
        flg = 1;
    }
    if (flg) {
      while (S[l] == 'R')
        l++;
      while (S[r] == 'W')
        r--;
      if (l < r) {
        swap(S[l], S[r]);
        ans++;
      }
    }
    l++, r--;
  }

  cout << ans << endl;
}

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 - 1); i >= 0; --i)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;

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

  int l = 0, r = N - 1;
  int ans = 0;
  while (l < r) {
    while (S[l] == 'R')
      l++;
    while (S[r] == 'W')
      r--;
    if (l < r) {
      swap(S[l], S[r]);
      ans++;
    }
    l++, r--;
  }

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

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int i = 0, j = s.size() - 1;
  int count = 0;
  while (i < n && j >= 0) {
    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]);
      count++;
    }
  }
  cout << count << endl;
}


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

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

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

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


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;

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

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define INF 2000000007
#define LINF 100000000000000007
#define MOD 1000000007
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n - 1; i >= 0; i--)
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define ARDEB(i, X)                                                            \
  {}
#define END                                                                    \
  {}
#endif
// typedef long long int ll;
typedef pair<int, int> P;
struct edge {
  int to, cost;
};
int ceil2(int a, int b) {
  if (a % b) {
    return a / b + 1;
  } else {
    return a / b;
  }
}
int n, k, ans;
string s;
signed main() {
  cin >> n >> s;
  int j = n - 1;
  rep(i, n) {
    if (s[i] == 'W') {
      while (s[j] != 'R') {
        j--;
      }
      if (i >= j)
        break;
      swap(s[i], s[j]);
      j--;
      ans++;
    }
  }
  // cout<<s<<endl;
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define INF 2000000007
#define LINF 100000000000000007
#define MOD 1000000007
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n - 1; i >= 0; i--)
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \
  {}
#define ARDEB(i, X)                                                            \
  {}
#define END                                                                    \
  {}
#endif
// typedef long long int ll;
typedef pair<int, int> P;
struct edge {
  int to, cost;
};
int ceil2(int a, int b) {
  if (a % b) {
    return a / b + 1;
  } else {
    return a / b;
  }
}
int n, k, ans;
string s;
signed main() {
  cin >> n >> s;
  int j = n - 1;
  rep(i, n) {
    if (s[i] == 'W') {
      while (j >= 0 && s[j] != 'R') {
        j--;
      }
      if (i >= j)
        break;
      swap(s[i], s[j]);
      j--;
      ans++;
    }
  }
  // cout<<s<<endl;
  cout << ans << endl;
}
Problem p02597 (C++)

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

int main() {
  int N;
  cin >> N;
  string Iro;
  cin >> Iro;
  int Ans = 0;
  int Red = 0;

  for (int i = 0; i < N; i++) {
    if (Iro.at(i) == 'R') {
      Red++;
    }
  }

  if (Red != 0) {
    for (int i = 0; i <= Red; i++) {
      if (Iro.at(i) == 'W') {
        Ans++;
      }
    }
  }
  cout << Ans;
}

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

int main() {
  int N;
  cin >> N;
  string Iro;
  cin >> Iro;
  int Ans = 0;
  int Red = 0;

  for (int i = 0; i < N; i++) {
    if (Iro.at(i) == 'R') {
      Red++;
    }
  }

  if (Red != 0) {
    Red--;
    for (int i = 0; i <= Red; i++) {
      if (Iro.at(i) == 'W') {
        Ans++;
      }
    }
  }
  cout << Ans;
}
Problem p02597 (C++)

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

int main(void) {
  int N;
  string C;
  cin >> N;
  cin >> C;
  int ans = 0;
  int m = 0, n = N - 1;

  char ch = C[0];
  for (int i = 0; i < N; i++) {
    if (ch != C[i]) {
      break;
    }
    if (i == N - 1) {
      cout << "0" << endl;
      return 0;
    }
  }

  while (m <= n) {
    for (int k = m; k < N - 1; k++) {
      if (C[k] == 'W') {
        m = k;
        break;
      }
    }
    if (m >= n)
      break;
    for (int k = n; k > 0; k--) {
      if (C[k] == 'R') {
        n = k;
        break;
      }
    }
    if (m >= n)
      break;
    if (n != m) {
      ans++;
      C[m] = 'R';
      C[n] = 'W';
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int main(void) {
  int N;
  string C;
  cin >> N;
  cin >> C;
  int ans = 0;
  int m = 0, n = N - 1;

  char ch = C[0];
  for (int i = 0; i < N; i++) {
    if (ch != C[i]) {
      break;
    }
    if (i == N - 1) {
      cout << "0" << endl;
      return 0;
    }
  }

  while (m <= n) {
    for (int k = m; k < N - 1; k++) {
      if (C[k] == 'W') {
        m = k;
        break;
      }
    }
    if (m >= n)
      break;
    for (int k = n; k > 0; k--) {
      if (C[k] == 'R') {
        n = k;
        break;
      }
    }
    if (m >= n)
      break;
    if (n != m) {
      ans++;
      m++;
      n--;
      // C[m]='R';
      // C[n]='W';
    }
  }
  cout << ans << endl;
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double d;
// #define for(i,a,b) for(int i=a;i<b;i++)
// #define f(i,a,b) for(int i=a;i<=b;i++)
int main() {
  int n;
  string s, r;
  int cnt1 = 0, cnt2 = 0;
  cin >> n;
  cin >> s;
  r = s;
  for (int i = 0, j = n - 1; j >= 0 && i <= n - 1; j--) {
    if (s[j] == 'R') {
      while (s[i] != 'W') {
        i++;
        if (i == n)
          break;
      }
      if (i >= j)
        break;
      swap(s[i], s[j]);
      cnt1++;
    }
  }
  s = r;
  for (int i = 0, j = n - 1; j >= 0 && i <= n - 1; i++) {
    if (s[i] == 'W') {
      while (s[j] != 'R') {
        j--;
        if (i == n)
          break;
      }
      if (i >= j)
        break;
      swap(s[i], s[j]);
      cnt2++;
    }
  }
  cout << min(cnt1, cnt2) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double d;
// #define for(i,a,b) for(int i=a;i<b;i++)
// #define f(i,a,b) for(int i=a;i<=b;i++)
int main() {
  int n;
  string s, r;
  int cnt1 = 0, cnt2 = 0;
  cin >> n;
  cin >> s;
  r = s;
  for (int i = 0, j = n - 1; j >= 0 && i <= n - 1; j--) {
    if (s[j] == 'R') {
      while (s[i] != 'W') {
        i++;
        if (i == n)
          break;
      }
      if (i >= j)
        break;
      swap(s[i], s[j]);
      cnt1++;
    }
  }
  s = r;
  for (int i = 0, j = n - 1; j >= 0 && i <= n - 1; i++) {
    if (s[i] == 'W') {
      while (s[j] != 'R') {
        j--;
        if (j == -1)
          break;
      }
      if (i >= j)
        break;
      swap(s[i], s[j]);
      cnt2++;
    }
  }
  cout << min(cnt1, cnt2) << endl;
}
Problem p02597 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define endl "\n"
#define pb push_back
#define PI 3.1415926535897932384626433832795l
#define F first
#define S second
#define mp make_pair
#define f(i, n) for (int i = 0; i < n; i++)
#define fastio                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define gcd(a, b) __gcd((a), (b))
#define fill(a, value) memset(a, value, sizeof(a));
#define minn(v) *min_element(v.begin(), v.end());
#define maxx(v) *max_element(v.begin(), v.end());
#define print(x) cout << (x) << endl;
#define sum(v) +x accumulate(v.begin(), v.end(), x);
#define debug(x) cout << #x << '=' << (x) << endl;
typedef pair<int, int> pii;
typedef vector<int> vi;
signed main() {
  fastio;
  cout << fixed << setprecision(12);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int i = 0;
  int j = n - 1;
  int ans = 0;
  while (i < j) {
    if (s[i] == 'W' and s[j] == 'R') {
      i++;
      j--;
      ans++;
    } else if (s[i] == 'W')
      j--;
    else if (s[j] == 'R')
      i++;
  }
  print(ans) return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define endl "\n"
#define pb push_back
#define PI 3.1415926535897932384626433832795l
#define F first
#define S second
#define mp make_pair
#define f(i, n) for (int i = 0; i < n; i++)
#define fastio                                                                 \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define gcd(a, b) __gcd((a), (b))
#define fill(a, value) memset(a, value, sizeof(a));
#define minn(v) *min_element(v.begin(), v.end());
#define maxx(v) *max_element(v.begin(), v.end());
#define print(x) cout << (x) << endl;
#define sum(v) +x accumulate(v.begin(), v.end(), x);
#define debug(x) cout << #x << '=' << (x) << endl;
typedef pair<int, int> pii;
typedef vector<int> vi;
signed main() {
  fastio;
  cout << fixed << setprecision(12);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int i = 0;
  int j = n - 1;
  int ans = 0;
  while (i < j) {
    if (s[i] == 'W' and s[j] == 'R') {
      i++;
      j--;
      ans++;
    } else if (s[i] == 'W')
      j--;
    else if (s[j] == 'R')
      i++;
    else {
      i++;
      j--;
    }
  }
  print(ans) return 0;
}
Problem p02597 (C++)

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

int main() {
  int N;
  string S;
  int l;
  int sum = 0;

  cin >> N >> S;
  l = N - 1;
  for (int i = 0; i < N; ++i) {
    if (i == l)
      break;
    if (S.at(i) == 'W') {
      for (int j = l; j > i; --j) {
        if (S.at(j) == 'R') {
          swap(S.at(i), S.at(j));
          l = j - 1;
          sum += 1;
          break;
        }
      }
    }
  }
  cout << sum << endl;
  return 0;
}


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

int main() {
  int N;
  string S;
  int l;
  int sum = 0;

  cin >> N >> S;
  l = N - 1;
  for (int i = 0; i < N; ++i) {
    if (i == l)
      break;
    if (S.at(i) == 'W') {
      for (int j = l; j > i; --j) {
        if (S.at(j) == 'R') {
          swap(S.at(i), S.at(j));
          l = j - 1;
          sum += 1;
          break;
        }
        if (j == i + 1)
          l = j;
      }
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02597 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

int main(int argc, char *argv[]) {
  int N;
  std::cin >> N;
  std::vector<char> v;

  char t;
  for (int i = 0; i < N; i++) {
    std::cin >> t;
    v.push_back(t);
  }

  // for(int i=0; i<N; i++) {
  //     std::cout << v[i];
  // }

  int min = N + 1;
  for (int b = 0; b < v.size() + 1; b++) {
    int p = 0, q = 0;
    for (int i = 0; i < N; i++) {
      if (i < b && v[i] == 'W') {
        p++;
      }
      if (i >= b && v[i] == 'R') {
        q++;
      }
    }
    // std::cout << "b=" << b << "pair(" << p << ", " << q << ")" << std::endl;
    int ans;
    if (p > q) {
      ans = q + (p - q);
    } else {
      ans = p + (q - p);
    }
    if (ans < min)
      min = ans;
  }
  std::cout << min;

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>

int main(int argc, char *argv[]) {
  int N;
  std::cin >> N;
  std::vector<char> v;

  char t;
  for (int i = 0; i < N; i++) {
    std::cin >> t;
    v.push_back(t);
  }

  // for(int i=0; i<N; i++) {
  //     std::cout << v[i];
  // }

  int min = N + 1;
  int p = 0, q = 0;
  for (int i = 0; i < N; i++) {
    if (v[i] == 'R') {
      q++;
    }
  }
  min = q;

  for (int b = 1; b < v.size() + 1; b++) {
    // for(int i=0; i<N; i++) {
    //     if(i<b && v[i]=='W') {
    //         p++;
    //     }
    //     if(i>=b && v[i]=='R') {
    //         q++;
    //     }
    // }
    if (v[b - 1] == 'W') {
      p++;
    }
    if (v[b - 1] == 'R') {
      q--;
    }
    // std::cout << "b=" << b << "pair(" << p << ", " << q << ")" << std::endl;
    int ans;
    if (p > q) {
      ans = q + (p - q);
    } else {
      ans = p + (q - p);
    }
    if (ans < min)
      min = ans;
  }
  std::cout << min;

  return 0;
}
Problem p02597 (C++)

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

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

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

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

Buggy submission (status: Runtime Error):
// D - Alter Altar
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

#define LL long long
#define Lowbit(X) (X & (-X))
#define Lson (X << 1)
#define Rson (X << 1 | 1)
#define Cl(X, Y) memset((X), (Y), sizeof(X))
#define FOR(i, A, B) for (LL i = A; i <= B; i++)
#define BOR(i, A, B) for (LL i = A; i >= B; i--)
#define FOR_SIDE(i, A) for (LL i = Head[A]; i; i = Next[i])
#define INF 0x7fffffff
using namespace std;
const int MAXN = 1e5 + 10;

int Red, White, Loc[MAXN], Total, Ans = INF;
char Opt;

inline void File() {
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
}

int main() {
  // File();
  scanf("%d", &Total);
  Opt = getchar();
  FOR(i, 1, Total) {
    Opt = getchar();
    if (Opt == 'W')
      Loc[i] = 0, White++;
    else
      Loc[i] = 1, Red++;
  }
  int New = 0, Res = 0;
  FOR(i, 1, Total) if (Loc[i] == 1 && i > Red) New++;
  printf("%d\n", New);
  // fclose(stdin); fclose(stdout);
  // system("pause");
  return 0;
}

Fixed submission:
// D - Alter Altar
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

#define LL long long
#define Lowbit(X) (X & (-X))
#define Lson (X << 1)
#define Rson (X << 1 | 1)
#define Cl(X, Y) memset((X), (Y), sizeof(X))
#define FOR(i, A, B) for (LL i = A; i <= B; i++)
#define BOR(i, A, B) for (LL i = A; i >= B; i--)
#define FOR_SIDE(i, A) for (LL i = Head[A]; i; i = Next[i])
#define INF 0x7fffffff
using namespace std;
const int MAXN = 2e5 + 10;

int Red, White, Loc[MAXN], Total, Ans = INF;
char Opt;

inline void File() {
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
}

int main() {
  // File();
  scanf("%d", &Total);
  Opt = getchar();
  FOR(i, 1, Total) {
    Opt = getchar();
    if (Opt == 'W')
      Loc[i] = 0, White++;
    else
      Loc[i] = 1, Red++;
  }
  int New = 0, Res = 0;
  FOR(i, 1, Total) if (Loc[i] == 1 && i > Red) New++;
  printf("%d\n", New);
  // fclose(stdin); fclose(stdout);
  // system("pause");
  return 0;
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("O3")

#define fi first
#define se second
#define ep emplace
#define eb emplace_back
#define mp make_pair
#define pi pair<int, int>
#define vi vector<int>
#define vpi vector<pair<int, int>>
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
#define endl '\n'

#define si(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= (ll)(m); ++i)
#define rep3(i, n, m) for (ll i = n; i >= (ll)(m); --i)

template <class T, class U> bool chmax(T &a, U b) {
  if (a < b)
    a = b;
  return true;
}
template <class T, class U> bool chmin(T &a, U b) {
  if (a > b)
    a = b;
  return true;
}

using ll = long long;
using ld = long double;
using namespace std;

constexpr ll MOD = 1000000007;

signed main() {
  cout << fixed << setprecision(20);
  ios::sync_with_stdio(false);
  cin.tie(0);

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

  int r;
  rep(i, n) if (s[i] == 'R')++ r;

  int ans = 0;
  rep(i, r) if (s[i] == 'W')++ ans;

  cout << ans << endl;

  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("O3")

#define fi first
#define se second
#define ep emplace
#define eb emplace_back
#define mp make_pair
#define pi pair<int, int>
#define vi vector<int>
#define vpi vector<pair<int, int>>
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
#define endl '\n'

#define si(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= (ll)(m); ++i)
#define rep3(i, n, m) for (ll i = n; i >= (ll)(m); --i)

template <class T, class U> bool chmax(T &a, U b) {
  if (a < b)
    a = b;
  return true;
}
template <class T, class U> bool chmin(T &a, U b) {
  if (a > b)
    a = b;
  return true;
}

using ll = long long;
using ld = long double;
using namespace std;

constexpr ll MOD = 1000000007;

signed main() {
  cout << fixed << setprecision(20);
  ios::sync_with_stdio(false);
  cin.tie(0);

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

  int r = 0;
  rep(i, n) if (s[i] == 'R')++ r;

  int ans = 0;
  rep(i, r) if (s[i] == 'W')++ ans;

  cout << ans << endl;

  return (0);
}
Problem p02597 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  int n;
  while (cin >> n >> s) {
    int ans = 0, now = 0;
    for (int i = n - 1; i >= 0; i--) {
      if (s[i] == 'R') {
        for (int j = now; j < i; j++) {
          if (s[j] == 'W') {
            now = j + 1;
            ans++;
            break;
          }
        }
      }
    }
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  int n;
  while (cin >> n >> s) {
    int ans = 0, now = 0;
    for (int i = n - 1; i >= 0; i--) {
      if (s[i] == 'R') {
        for (int j = now; j < i; j++) {
          if (s[j] == 'W') {
            now = j + 1;
            ans++;
            break;
          }
          now = j + 1;
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p02597 (C++)

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

int main() {
  int n, ans = 1, i, cou1 = 0, cou2 = 0;
  char c[20010];

  scanf("%d", &n);
  scanf("%s", c);
  for (i = 0; i < n; i++)
    if (c[i] == 'R')
      cou1++;
  for (i = 0; i < cou1; i++)
    if (c[i] == 'R')
      cou2++;

  printf("%d\n", cou1 - cou2);
  return 0;
}

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

int main() {
  int n, ans = 1, i, cou1 = 0, cou2 = 0;
  char c[200010];

  scanf("%d", &n);
  scanf("%s", c);
  for (i = 0; i < n; i++)
    if (c[i] == 'R')
      cou1++;
  for (i = 0; i < cou1; i++)
    if (c[i] == 'R')
      cou2++;

  printf("%d\n", cou1 - cou2);
  return 0;
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define LL long long int
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u], to; k; k = ed[k].nxt)
#define cls(s, v) memset(s, v, sizeof(s))
#define mp(a, b) make_pair<int, int>(a, b)
#define cp pair<int, int>
using namespace std;
const int maxn = 100005, maxm = 100005, INF = 0x3f3f3f3f;
inline int read() {
  int out = 0, flag = 1;
  char c = getchar();
  while (c < 48 || c > 57) {
    if (c == '-')
      flag = 0;
    c = getchar();
  }
  while (c >= 48 && c <= 57) {
    out = (out << 1) + (out << 3) + c - 48;
    c = getchar();
  }
  return flag ? out : -out;
}
char s[maxn];
int main() {
  int n = read();
  scanf("%s", s + 1);
  int l = 1, r = n, ans = 0;
  while (l < r) {
    while (l < r && s[l] == 'R')
      l++;
    while (l < r && s[r] == 'W')
      r--;
    if (l < r && s[l] == 'W' && s[r] == 'R')
      ans++, s[l] = 'R', s[r] = 'W';
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define LL long long int
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u], to; k; k = ed[k].nxt)
#define cls(s, v) memset(s, v, sizeof(s))
#define mp(a, b) make_pair<int, int>(a, b)
#define cp pair<int, int>
using namespace std;
const int maxn = 200005, maxm = 100005, INF = 0x3f3f3f3f;
inline int read() {
  int out = 0, flag = 1;
  char c = getchar();
  while (c < 48 || c > 57) {
    if (c == '-')
      flag = 0;
    c = getchar();
  }
  while (c >= 48 && c <= 57) {
    out = (out << 1) + (out << 3) + c - 48;
    c = getchar();
  }
  return flag ? out : -out;
}
char s[maxn];
int main() {
  int n = read();
  scanf("%s", s + 1);
  int l = 1, r = n, ans = 0;
  while (l < r) {
    while (l < r && s[l] == 'R')
      l++;
    while (l < r && s[r] == 'W')
      r--;
    if (l < r && s[l] == 'W' && s[r] == 'R')
      ans++, s[l] = 'R', s[r] = 'W';
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02597 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

const ll P = 1000000007;
const long long INF = 1LL << 60;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
vector<char> Alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
                         'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                         'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
vector<char> alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                         'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                         's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
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;
}

int main() {
  cout << fixed << setprecision(10);
  int N;
  cin >> N;
  int r = N - 1, cnt = 0;
  vector<char> c(N);
  rep(i, N) cin >> c[i];
  rep(i, N) {
    if (c[i] == 'W') {
      for (int j = r; j >= i; j--) {
        if (c[j] == 'R') {
          swap(c[i], c[j]);
          r = j;
          cnt++;
          break;
        }
      }
    }
  }
  cout << cnt << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

const ll P = 1000000007;
const long long INF = 1LL << 60;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
vector<char> Alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
                         'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                         'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
vector<char> alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                         'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                         's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
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;
}

int main() {
  cout << fixed << setprecision(10);
  int N;
  cin >> N;
  int r = N - 1, cnt = 0;
  vector<char> c(N);
  rep(i, N) cin >> c[i];
  rep(i, N) {
    if (c[i] == 'W') {
      for (int j = r; j >= i; j--) {
        if (c[j] == 'R') {
          swap(c[i], c[j]);
          r = j;
          cnt++;
          break;
        }
        if (j == i) {
          cout << cnt << endl;
          return 0;
        }
      }
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02597 (C++)

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

#ifdef local
template <class T> void _E(T x) { cerr << x; }
void _E(double x) { cerr << fixed << setprecision(6) << x; }
void _E(string s) { cerr << "\"" << s << "\""; }
template <class A, class B> void _E(pair<A, B> x) {
  cerr << '(';
  _E(x.first);
  cerr << ", ";
  _E(x.second);
  cerr << ")";
}
template <class T> void _E(vector<T> x) {
  cerr << "[";
  for (auto it = x.begin(); it != x.end(); ++it) {
    if (it != x.begin())
      cerr << ", ";
    _E(*it);
  }
  cerr << "]";
}
void ERR() {}
template <class A, class... B> void ERR(A x, B... y) {
  _E(x);
  cerr << (sizeof...(y) ? ", " : " ");
  ERR(y...);
}
#define debug(x...)                                                            \
  do {                                                                         \
    cerr << "{ " #x " } -> { ";                                                \
    ERR(x);                                                                    \
    cerr << "}" << endl;                                                       \
  } while (false)
#else
#define debug(...) 114514.1919810
#endif

using ll = long long;

int main(int argc, char *argv[]) {
  cin.sync_with_stdio(false), cin.tie(nullptr);

  int n;
  string s;
  cin >> n >> s;
  int ans = 0;
  for (int i = 0, j = n - 1; i < j;) {
    if (s[i] == s[j] && s[i] == 'R') {
      ++i;
    } else if (s[i] == s[j] && s[i] == 'W') {
      --j;
    } else if (s[i] == 'W') {
      ++ans, ++i, --j;
    }
  }
  cout << ans << "\n";
}


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

#ifdef local
template <class T> void _E(T x) { cerr << x; }
void _E(double x) { cerr << fixed << setprecision(6) << x; }
void _E(string s) { cerr << "\"" << s << "\""; }
template <class A, class B> void _E(pair<A, B> x) {
  cerr << '(';
  _E(x.first);
  cerr << ", ";
  _E(x.second);
  cerr << ")";
}
template <class T> void _E(vector<T> x) {
  cerr << "[";
  for (auto it = x.begin(); it != x.end(); ++it) {
    if (it != x.begin())
      cerr << ", ";
    _E(*it);
  }
  cerr << "]";
}
void ERR() {}
template <class A, class... B> void ERR(A x, B... y) {
  _E(x);
  cerr << (sizeof...(y) ? ", " : " ");
  ERR(y...);
}
#define debug(x...)                                                            \
  do {                                                                         \
    cerr << "{ " #x " } -> { ";                                                \
    ERR(x);                                                                    \
    cerr << "}" << endl;                                                       \
  } while (false)
#else
#define debug(...) 114514.1919810
#endif

using ll = long long;

int main(int argc, char *argv[]) {
  cin.sync_with_stdio(false), cin.tie(nullptr);

  int n;
  string s;
  cin >> n >> s;
  int ans = 0;
  for (int i = 0, j = n - 1; i < j;) {
    if (s[i] == s[j] && s[i] == 'R') {
      ++i;
    } else if (s[i] == s[j] && s[i] == 'W') {
      --j;
    } else if (s[i] == 'W') {
      ++ans, ++i, --j;
    } else {
      ++i, --j;
    }
  }
  cout << ans << "\n";
}
Problem p02597 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <iostream>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#include <algorithm>
#include <array>
#include <cinttypes>
#include <cstdio>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <stdio.h>
#include <type_traits>

int main() {
  int N;
  cin >> N;
  std::vector<int> table;
  table.resize(N);

  for (int i = 0; i < N; ++i) {
    char c;
    cin >> c;
    if (c == 'W') {
      table[i] = 1;
    }
  }
  // r = 0
  // w = 1
  int r = 0;
  int last = N - 1;
  for (int i = 0; i < N; ++i) {
    if (table[i] == 0) {
      continue;
    }
    bool b = false;
    for (int j = last; i < j; --j) {
      if (table[j] == 0) {
        table[j] = 1;
        table[i] = 0;
        b = true;
        last = j;
        ++r;
        break;
      }
    }
    if (b) {
      continue;
    }
  }

  printf("%d\n", r);
  return 0;
}


Fixed submission:

#include <iostream>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#include <algorithm>
#include <array>
#include <cinttypes>
#include <cstdio>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <stdio.h>
#include <type_traits>

int main() {
  int N;
  cin >> N;
  std::vector<int> table;
  table.resize(N);

  for (int i = 0; i < N; ++i) {
    char c;
    cin >> c;
    if (c == 'W') {
      table[i] = 1;
    }
  }
  // r = 0
  // w = 1
  int r = 0;
  int last = N - 1;
  for (int i = 0; i < N; ++i) {
    if (table[i] == 0) {
      continue;
    }
    bool b = false;
    for (int j = last; i < j; --j) {
      if (table[j] == 0) {
        table[j] = 1;
        table[i] = 0;
        b = true;
        last = j;
        ++r;
        break;
      }
    }
    if (b) {
      continue;
    }

    break;
  }

  printf("%d\n", r);
  return 0;
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define MAXN 1000010
#define rep(i, n) for (int i = 0; i < (n); ++i)
const ll mod = 1e9 + 7;

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

bool pairCompare(const P &firstElof, const P &secondElof) {
  return firstElof.second < secondElof.second;
}

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define MAXN 1000010
#define rep(i, n) for (int i = 0; i < (n); ++i)
const ll mod = 1e9 + 7;

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

bool pairCompare(const P &firstElof, const P &secondElof) {
  return firstElof.second < secondElof.second;
}

int main() {
  int n;
  string s;
  cin >> n;
  cin >> s;
  int r, l;
  l = 0;
  r = s.length() - 1;
  int ans = 0;
  while (l < r) {
    while (s[l] != 'W' or s[r] != 'R') {
      if (s[l] != 'W')
        l++;
      if (s[r] != 'R')
        r--;
      if (l >= r or r == 0 or l == s.length()) {
        break;
      }
    }
    if (l < r) {
      ans++;
      l++;
      r--;
    }
  }
  cout << ans << endl;
}
Problem p02597 (C++)

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

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


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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <unordered_set>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
using P = pair<ll, ll>;
using V = vector<ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rep3(i, m, n) for (ll i = m; i <= (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define MOD 1000000007
#define INF 1000000000
#define LLINF 1e18

void solve_abc_d() {
  ll n, m;
  // ll a, b;
  ll x, y, z;
  // ll h, w;
  ll k;
  string s, rs;
  ll l_ans = 0;

  cin >> n;
  cin >> s;

  rs = s;
  reverse(ALL(rs));

  V r, w;
  rep(i, n) {
    if (s[i] == 'W') {
      w.push_back(i);
    }
  }
  rep(i, n) {
    if (rs[i] == 'R') {
      r.push_back(i);
    }
  }

  if (n == 1) {
    cout << 0 << "\n";
    return;
  }

  auto itr_w = w.begin();
  auto itr_r = r.begin();
  rep(i, n) {
    if (s[i] == 'W') {
      if (*itr_w > n - 1 - *itr_r) {
        break;
      }
      itr_w++;
      itr_r++;
      l_ans++;
    }
  }

  cout << l_ans << "\n";

  return;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed;
  cout << setprecision(15);

  solve_abc_d();

  return 0;
}


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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
using P = pair<ll, ll>;
using V = vector<ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rep3(i, m, n) for (ll i = m; i <= (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define MOD 1000000007
#define INF 1000000000
#define LLINF 1e18

void solve_abc_d() {
  ll n, m;
  // ll a, b;
  ll x, y, z;
  // ll h, w;
  ll k;
  string s, rs;
  ll l_ans = 0;

  cin >> n;
  cin >> s;

  rs = s;
  reverse(ALL(rs));

  V r, w;
  rep(i, n) {
    if (s[i] == 'W') {
      w.push_back(i);
    }
  }
  rep(i, n) {
    if (rs[i] == 'R') {
      r.push_back(i);
    }
  }

  if ((w.size() == n) || (r.size() == n)) {
    cout << 0 << "\n";
    return;
  }

  auto itr_w = w.begin();
  auto itr_r = r.begin();
  rep(i, n) {
    if (s[i] == 'W') {
      if (*itr_w > n - 1 - *itr_r) {
        break;
      }
      itr_w++;
      itr_r++;
      l_ans++;
    }
  }

  cout << l_ans << "\n";

  return;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed;
  cout << setprecision(15);

  solve_abc_d();

  return 0;
}
Problem p02597 (C++)

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

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

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


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

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

      ans--;
  }
  cout << ans << '\n';
}
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 rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

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

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

  int cw = 0;
  int cr = 0;

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

  cout << cr;
}

Fixed submission:
#include "bits/stdc++.h"

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

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

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

  int cw = 0;
  int cr = 0;

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

  cout << cr;
}
Problem p02597 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #define rep(i,a,b) for(int i=a;i<b;i++)
#define T                                                                      \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
using namespace std;
typedef long long ll;
const ll maxn = 1e5 + 10;
// typedef unsigned long long ull;
int main() {
  ll n;
  scanf("%lld", &n);
  char c[maxn];
  ll r = 0, w = 0;
  scanf("%s", c);
  for (ll i = 0; i < strlen(c); i++) {
    if (c[i] == 'R') {
      r++;
    }
  }
  for (ll i = 0; i < r; i++) {
    if (c[i] == 'W')
      w++;
  }
  cout << w << endl;
}


Fixed submission:
#include <bits/stdc++.h>
// #define rep(i,a,b) for(int i=a;i<b;i++)
#define T                                                                      \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
using namespace std;
typedef long long ll;
const ll maxn = 2e5 + 10;
// typedef unsigned long long ull;
int main() {
  ll n;
  scanf("%lld", &n);
  char c[maxn];
  ll r = 0, w = 0;
  scanf("%s", c);
  for (ll i = 0; i < strlen(c); i++) {
    if (c[i] == 'R') {
      r++;
    }
  }
  for (ll i = 0; i < r; i++) {
    if (c[i] == 'W')
      w++;
  }
  cout << w << endl;
}
Problem p02597 (C++)

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

#define DEBUG 0

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

  vector<char> c(N);

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

  int counter = 0;

  int reverse_count = N - 1;

  for (int i = 0; i < N; i++) {
    if (c.at(i) == 'W') {
      for (int j = reverse_count; j > i; j--) {
        if (c.at(j) == 'R') {
          counter++;
          reverse_count = j - 1;
          if (reverse_count == i) {
            cout << counter << endl;
            return 0;
          }

          break;

#if DEBUG
          cout << i << j << c.at(i) << c.at(j) << endl;
#endif
        }
        if (j == i - 1) {
          cout << counter << endl;
          return 0;
        }
      }
    }
  }

  cout << counter << endl;

  return 0;
}


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

#define DEBUG 0

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

  vector<char> c(N);

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

  int counter = 0;

  int reverse_count = N - 1;

  for (int i = 0; i < N; i++) {
    if (c.at(i) == 'W') {
      for (int j = reverse_count; j > i; j--) {
        if (c.at(j) == 'R') {
          counter++;
          reverse_count = j - 1;
          if (reverse_count == i) {
            cout << counter << endl;
            return 0;
          }

          break;

#if DEBUG
          cout << i << j << c.at(i) << c.at(j) << endl;
#endif
        }
        if (j == i + 1) {
          cout << counter << endl;
          return 0;
        }
      }
    }
  }

  cout << counter << 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++)
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;
}

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;
  }
  if (red == 0)
    cout << 0 << endl;
  else
    cout << count.at(red - 1) << endl;
  return 0;
}