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

#include <bits/stdc++.h>

#define F first
#define S second
#define ll long long
#define pb push_back
#define endl '\\n'

using namespace std;

const int MOD = 1e9 + 7;
const int N = 510;
int num, x;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  char ch[n];
  for (int i = 0; i < n; ++i) {
    cin >> ch[i];
    if (ch[i] == 'R')
      ++num;
  }
  for (int i = 0; i < n; ++i) {
    if (i < num and ch[i] == 'R')
      ++x;
  }
  cout << num - x << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define F first
#define S second
#define ll long long
#define pb push_back
#define endl '\\n'

using namespace std;

const int MOD = 1e9 + 7;
const int N = 510;
int num, x;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  char ch[n];
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> ch[i];
    if (ch[i] == 'R')
      ++num;
  }
  for (int i = 0; i < n; ++i) {
    if (i < num and ch[i] == 'R')
      ++x;
  }
  cout << num - x << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

/*                    ॐ Shree ॐ                     */
/* ॐ ॐ ॐ
ॐ भूर् भुवः स्वः
तत् सवितुर्वरेण्यं
भर्गो देवस्य धीमहि
धियो यो नः प्रचोदयात्
*/
using namespace std;
typedef long long int ll;
vector<int> ve(3006, 0);
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
// online submission
#endif
  int n;
  cin >> n;
  string s;
  cin >> s;
  int ctr = 0, ctw = 0, mx = INT_MAX, r, w;
  vector<int> v(n, 0);
  vector<int> v2(n, 0);
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W') {
      ctw++;
    } else {
      ctr++;
    }
  }
  int rx = 0, ry = 0;
  for (int i = 0; i < ctr; i++) {
    if (s[i] != 'R') {
      rx++;
    }
  }
  for (int i = ctr; i < n; i++) {
    if (s[i] != 'W') {
      ry++;
    }
  }
  cout << abs(rx - ry) + min(rx, ry);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

/*                    ॐ Shree ॐ                     */
/* ॐ ॐ ॐ
ॐ भूर् भुवः स्वः
तत् सवितुर्वरेण्यं
भर्गो देवस्य धीमहि
धियो यो नः प्रचोदयात्
*/
using namespace std;
typedef long long int ll;
vector<int> ve(3006, 0);
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
// online submission
#endif
  int n;
  cin >> n;
  string s;
  cin >> s;
  int R = 0, W = 0;
  int ctr = 0, ctw = 0, mx = INT_MAX;
  for (int i = 0; i <= n; i++) {
    ctw = 0;
    ctr = 0;
    for (int j = 0; j < i; j++) {
      if (s[j] != 'R') {
        ctr++;
      }
    }
    for (int j = i; j < n; j++) {
      if (s[j] != 'W') {
        ctw++;
      }
    }
    mx = min(mx, (abs(ctw - ctr) + min(ctw, ctr)));
  }
  cout << mx;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
const int N = 2e5 + 5;
using namespace std;
template <typename tp> inline void r(tp &n) {
  n = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c))
    n = n * 10 + c - 48, c = getchar();
}
char s[N];
int main() {

  int n, ans = 0;
  r(n);
  scanf("%s", s + 1);
  queue<int> v;
  for (int i = 1; i <= n; ++i)
    if (s[i] == 'W')
      v.push(i);
  for (int i = n; i >= 1; --i) {
    if (!v.size())
      break;
    if (s[i] == 'R')
      if (v.front() > i)
        break;
      else
        ++ans, v.pop();
  }
  printf("%d\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
const int N = 2e5 + 5;
using namespace std;
template <typename tp> inline void r(tp &n) {
  n = 0;
  char c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c))
    n = n * 10 + c - 48, c = getchar();
}
char s[N];
int main() {

  int n, ans = 0;
  r(n);
  scanf("%s", s + 1);
  queue<int> v;
  for (int i = 1; i <= n; ++i)
    if (s[i] == 'W')
      v.push(i);
  for (int i = n; i >= 1; --i)
    if (s[i] == 'R')
      if (v.front() > i)
        break;
      else
        ++ans, v.pop();
  printf("%d\\n", ans);
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: 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;
  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;
}


Error: 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;
  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;
}
Problem p02597 (C++) — buggy submission, verdict: 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;
  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;
}

Error: WA

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;
  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;
}
Problem p02597 (C++) — buggy submission, verdict: 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;
  if (r.empty()) {
    prtl(0);
    return 0;
  }
  for (auto x : w) {
    if (r.top() < x) {
      break;
    } else {
      r.pop();
      c++;
    }
  }
  prtl(c);
}


Error: 0

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;
  for (auto x : w) {
    if (r.top() < x) {
      break;
    } else {
      r.pop();
      c++;
    }
  }
  prtl(c);
}
Problem p02597 (C++) — buggy submission, verdict: 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 or r == 0 or l == s.length()) {
        break;
      }
    }
    if (l < r) {
      ans++;
      l++;
      r--;
    }
  }
  cout << ans << endl;
}


Error: 0

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) {
      ans++;
      l++;
      r--;
    }
  }
  cout << ans << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

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

  string c;
  cin >> c;

  vector<ll> rsumv(n, 0);
  if (c.at(0) == 'R')
    rsumv.at(0)++;

  for (int i = 1; i < n; i++) {
    if (c.at(i) == 'R') {
      rsumv.at(i) = rsumv.at(i - 1) + 1;
    } else
      rsumv.at(i) = rsumv.at(i - 1);
  }
  ll rsum = rsumv.at(n - 1);

  ll ans = 0;
  if (rsum) {
    ans = rsum - rsumv.at(rsum - 1);
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

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

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

  string c;
  cin >> c;

  vector<ll> rsumv(n, 0);
  if (c.at(0) == 'R')
    rsumv.at(0)++;

  for (int i = 1; i < n; i++) {
    if (c.at(i) == 'R') {
      rsumv.at(i) = rsumv.at(i - 1) + 1;
    } else
      rsumv.at(i) = rsumv.at(i - 1);
  }
  ll rsum = rsumv.at(n - 1);

  ll ans = rsum - rsumv.at(rsum - 1);

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

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

int main() {

  int i, j, N, flag_r, flag_w, count;
  cin >> N;
  flag_r = 0;
  flag_w = 0;
  count = 0;

  int L[N];

  string S;
  cin >> S;

  for (i = 0; i < N; i++) {
    if (S.at(i) == 'R') {
      L[i] = 1;
      flag_r = 1;

    } else {
      L[i] = 0;
      flag_w = 1;
    }
  }

  if (flag_r == 0 || flag_w == 0) {
    cout << "0"
         << "\\n";
    return 0;
  }

  i = 0;
  j = N - 1;

  while (1) {

    while (L[i] == 1) {
      i++;
    }

    while (L[j] == 0) {
      j--;
    }

    if (i > j)
      break;

    L[i] = 1;
    L[j] = 0;
    count++;
  }

  cout << count << "\\n";

  return 0;
}


Error: -11

Fixed submission:

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

int main() {

  int i, j, N, L[N], flag_r, flag_w, count;
  cin >> N;
  flag_r = 0;
  flag_w = 0;
  count = 0;

  string S;
  cin >> S;

  for (i = 0; i < N; i++) {
    if (S.at(i) == 'R') {
      L[i] = 1;
      flag_r = 1;

    } else {
      L[i] = 0;
      flag_w = 1;
    }
  }

  if (flag_r == 0 || flag_w == 0) {
    cout << "0"
         << "\\n";
    return 0;
  }

  i = 0;
  j = N - 1;

  while (1) {

    while (L[i] == 1) {
      i++;
    }

    while (L[j] == 0) {
      j--;
    }

    if (i > j)
      break;

    L[i] = 1;
    L[j] = 0;
    count++;
  }

  cout << count << "\\n";

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

#include <bits/stdc++.h>
using namespace std;
string s;
int n, ans = 0, t;
signed main() {
  // cin>>t;
  // while(t--){
  cin >> n >> s;
  int a = 0, b = n - 1;
  while (a < b) {
    //	cout<<a<<"v"<<b<<endl;
    if (s[a] == 'R' && s[b] == 'W') {
      a++;
      b--;
      continue;
    }
    if (s[a] == 'R') {
      a++;
      continue;
    }
    if (s[b] == 'W') {
      b--;
      continue;
    }
    ans++;
    a++;
    b--;
  }
  cout << ans << endl;
  ans = 0;
  //}
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string s;
int n, ans = 0, t;
signed main() {
  // cin>>t;
  // while(t--){
  cin >> n >> s;
  int a = 0, b = n - 1;
  while (a < b) {
    //	cout<<a<<"v"<<b<<endl;
    if (s[a] == 'R' && s[b] == 'W')
      continue;
    if (s[a] == 'R') {
      a++;
      continue;
    }
    if (s[b] == 'W') {
      b--;
      continue;
    }
    ans++;
    a++;
    b--;
  }
  cout << ans << endl;
  ans = 0;
  //}
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

int main(void) {
  ll n, r = 0, w = 0;
  cin >> n;
  vector<char> vec(n);
  rep(i, n) {
    cin >> vec.at(i);
    if (vec.at(i) == 'R')
      r++;
    else if (vec.at(i) == 'W')
      w++;
  }
  ll ans1 = 0, ans2 = 0;
  rep(i, r) {
    if (vec.at(i) == 'W')
      ans1++;
  }
  for (int i = r; i < n; i++) {
    if (vec.at(i) == 'R')
      ans2++;
  }
  ll ans = max(ans1, ans2);
  cout << ans << endl;

  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 18446744072736633250) >= this->size() (which is 4)


Fixed submission:

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

int main(void) {
  ll n, r, w;
  cin >> n;
  vector<char> vec(n);
  rep(i, n) {
    cin >> vec.at(i);
    if (vec.at(i) == 'R')
      r++;
    else if (vec.at(i) == 'W')
      w++;
  }
  ll ans1 = 0, ans2 = 0;
  rep(i, r) {
    if (vec.at(i) == 'W')
      ans1++;
  }
  for (int i = r; i < n; i++) {
    if (vec.at(i) == 'R')
      ans2++;
  }
  ll ans = max(ans1, ans2);
  cout << ans << endl;

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

/******

AUTHOR : RUDRANSH TRIPATHI, IIIT NAGPUR, CCID(rt24) & CFID(rsgt24)

MOTTO : Use criticism as fuel and you will never run out of energy.
        AIM : TO BECOME A BETTER CODER
        INTROSPECT + COURSE CORRECTIONS = CATALYST FOR SUCCESS
        Every moment is an opportunity to better yourself

*******/

#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define int long long int
#define pb push_back
#define d(x) cout << x << "\\n"

/*int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

int powermod(int A, int p, int MOD = mod){
    A%=MOD;
    int res  = 1;
    while(p){
        if(p&1)
            res = ((__int128)res*A)%MOD;

        A = ((__int128)A*A)%MOD;
        p>>=1;
    }

    return res;
}*/

// bool comp()
//{
// }

int32_t main(void) {
  fastio int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    string x;
    cin >> x;
    string temp = "";
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (x[i] == 'R')
        temp += 'R';
    }
    for (int i = 0; i < n; i++) {
      if (x[i] == 'W')
        temp += 'W';
    }
    int count = 0;
    for (int i = 0; i < n; i++) {
      if (temp[i] == 'R') {
        if (temp[i] != x[i])
          cnt++;
      } else {
        if (temp[i] != x[i])
          count++;
      }
    }

    cout << min(count, cnt) << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

/******

AUTHOR : RUDRANSH TRIPATHI, IIIT NAGPUR, CCID(rt24) & CFID(rsgt24)

MOTTO : Use criticism as fuel and you will never run out of energy.
        AIM : TO BECOME A BETTER CODER
        INTROSPECT + COURSE CORRECTIONS = CATALYST FOR SUCCESS
        Every moment is an opportunity to better yourself

*******/

#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define int long long int
#define pb push_back
#define d(x) cout << x << "\\n"

/*int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}

int powermod(int A, int p, int MOD = mod){
    A%=MOD;
    int res  = 1;
    while(p){
        if(p&1)
            res = ((__int128)res*A)%MOD;

        A = ((__int128)A*A)%MOD;
        p>>=1;
    }

    return res;
}*/

// bool comp()
//{
// }

int32_t main(void) {
  fastio int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    cin >> n;
    string x;
    cin >> x;
    int cnt = 0;
    vector<int> ans;
    for (int i = 0; i < n; i++) {
      if (x[i] == 'R')
        ans.pb(i + 1);
      if (x[i] == 'W')
        cnt++;
    }
    int count = 0;
    for (int i = 0; i < ans.size() - 1; i++) {
      count += ans[i + 1] - ans[i] - 1;
    }

    if (count == 0)
      cout << cnt << "\\n";
    else
      cout << count << "\\n";
  }

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

#include <iostream>
using namespace std;

int main() {
  unsigned long n, red = 0, count = 0;
  string c;
  cin >> n >> c;

  // 最後の赤を最初の白と交換していく
  // 赤を数える
  for (unsigned long i = 0; i < n; i++)
    if (c[i] == 'R')
      red++;
  // 赤の数分の左側のもののうち白の数を数える
  // その白を赤と交換すればよい
  for (unsigned long i = 0; i < red; i++)
    if (c[i] == 'W')
      count++;

  cout << count;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  unsigned long n, count = 0;
  string c;
  cin >> n >> c;

  // 最後の赤を最初の白と交換していく
  for (unsigned long i = n - 1; c.find("WR") != string::npos; i--) {
    if (c[i] == 'R') {
      count++;
      c[i] = 'W';
      c[c.find('W')] = 'R';
    }
  }

  cout << count;

  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: 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];
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;
}

Error: TLE

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];
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;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPB(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORB(i, m, n) for (int i = m; i >= n; i--)
#define ll long long
#define pb push_back
#define popb pop_back
#define debug(i) cout << #i << "= " << i << endl
using namespace std;

int main() {
  int n, firstw = -1, firstr = -1;
  int ans = 1000000;
  string c;
  cin >> n >> c;
  int count[2] = {};
  bool white = false, red = false;
  REP(i, n) {
    if (c[i] == 'R') {
      if (!red) {
        red = true;
        firstr = i;
      }
      count[0]++;
    } else {
      count[1]++;
      if (!white) {
        white = true;
        firstw = i;
      }
    }
  }
  if (firstw == -1) {
    cout << 0 << endl;
    return 0;
  }
  int w = 0;
  int r = count[0];
  int k = max(w, r);
  REP(i, n + 1) {
    k = max(w, r);
    ans = min(k, ans);
    // debug(w);
    // debug(r);
    // debug(ans);
    if (i != n) {
      if (c[i] == 'W') {
        w++;
      } else {
        r--;
      }
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPB(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORB(i, m, n) for (int i = m; i >= n; i--)
#define ll long long
#define pb push_back
#define popb pop_back
#define debug(i) cout << #i << "= " << i << endl
using namespace std;

int main() {
  int n;
  int ans;
  string c;
  cin >> n >> c;
  ans = 0;
  REP(i, n) {
    if (c[i] == 'R')
      continue;
    FORB(j, n - 1, i) {
      if (c[i] == 'W' && c[j] == 'R') {
        ans++;
        swap(c[i], c[j]);
        break;
        // debug(i);
        // debug(j);
      }
    }
  }
  cout << ans << endl;
}