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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, a, b) for (int i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()    // 昇順ソート
#define rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define FastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
typedef long long ll;
using P = pair<int, int>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int n;
  string s;
  cin >> n >> s;
  int left = 0, right = n - 1;
  int ans = 0;
  while (left < right) {
    while (right >= 0 && s[right] != 'R')
      right--;
    while (left < n && s[left] != 'W')
      left++;
    if (left >= right)
      break;
    ans++;
    s[left] = 'R';
    s[right] = 'W';
    left++;
    right--;
  }
  int wNum = 0;
  rep(i, n) {
    if (s[i] == 'W')
      wNum++;
  }
  cout << min(ans, wNum) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, a, b) for (int i = a; i < b; i++)
#define all(x) (x).begin(), (x).end()    // 昇順ソート
#define rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define FastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
typedef long long ll;
using P = pair<int, int>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int n;
  string s;
  cin >> n >> s;
  int left = 0, right = n - 1;
  int ans = 0;
  while (left < right) {
    while (s[right] != 'R')
      right--;
    while (s[left] != 'W')
      left++;
    if (left >= right)
      break;
    ans++;
    s[left] = 'R';
    s[right] = 'W';
    left++;
    right--;
  }
  int wNum = 0;
  rep(i, n) {
    if (s[i] == 'W')
      wNum++;
  }
  cout << min(ans, wNum) << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int N;

  cin >> N;
  string buff;
  for (int n = 0; n < N; ++n) {
    cin >> buff;
  }

  vector<int> c(N); // R = 0, W = 1 の配列
  const char *cbuff = buff.c_str();
  for (int n = 0; n < N; ++n) {
    c[n] = (cbuff[n] == 'R') ? 0 : 1;
  }

  int count = 0;
  int start_min_w = 0;
  int start_max_r = N - 1;
  for (int n = 0; n < N; ++n) {
    // 最小の 1 のインデックス
    int min_w = -1;
    for (int i = start_min_w; i < N; ++i) {
      if (c[i] == 1) {
        min_w = i;
        break;
      }
    }

    if (min_w == -1) {
      break;
    } // ないときは終了
    start_min_w = min_w + 1;

    // 最大の 0 のインデックス
    int max_r = N;
    for (int i = start_max_r; i >= 0; --i) {
      if (c[i] == 0) {
        max_r = i;
        break;
      }
    }

    if (max_r == N) {
      break;
    } // ないときは終了
    start_max_r = max_r - 1;

    if (min_w > max_r) {
      break;
    } else // 交換
    {
      c[max_r] = 1;
      c[min_w] = 0;
      ++count;
    }
  }

  cout << count << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int N;

  cin >> N;
  string buff;
  for (int n = 0; n < N; ++n) {
    cin >> buff;
  }

  vector<int> c(N); // R = 0, W = 1 の配列
  const char *cbuff = buff.c_str();
  for (int n = 0; n < N; ++n) {
    c[n] = (cbuff[n] == 'R') ? 0 : 1;
  }

  int count = 0;
  for (int n = 0; n < N; ++n) {
    // 最小の 1 のインデックス
    int min_w = -1;
    for (int i = 0; i < N; ++i) {
      if (c[i] == 1) {
        min_w = i;
        break;
      }
    }

    if (min_w == -1) {
      break;
    } // ないときは終了

    // 最大の 0 のインデックス
    int max_r = N;
    for (int i = N - 1; i >= 0; --i) {
      if (c[i] == 0) {
        max_r = i;
        break;
      }
    }

    if (max_r == N) {
      break;
    } // ないときは終了

    if (min_w > max_r) {
      break;
    } else // 交換
    {
      c[max_r] = 1;
      c[min_w] = 0;
      ++count;
    }
  }

  cout << count << endl;

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

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

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

  int countR = 0;
  int countW = 0;
  for (int i = 0; i < N; i++) {
    if (S.at(i) == 'R') {
      countR++;
    } else if (S.at(i) == 'W') {
      countW++;
    }
  }

  if (countR == 0 || countW == 0) {
    cout << 0 << endl;
    return 0;
  }

  int A = 0;
  int B = 0;
  vector<int> red(countR);
  vector<int> white(countW);
  for (int i = 0; i < N; i++) {
    if (S.at(i) == 'R') {
      red.at(A) = i;
      A++;
    } else if (S.at(i) == 'W') {
      white.at(B) = i;
      B++;
    }
  }
  sort(red.begin(), red.end());
  reverse(red.begin(), red.end());
  sort(white.begin(), white.end());

  int counta = 0;
  int countb = 0;
  for (int i = 0; i < countR; i++) {
    if (red.at(i) > countR - 1) {
      counta++;
    } else {
      break;
    }
  }
  for (int i = 0; i < countW; i++) {
    if (white.at(i) < countR) {
      countb++;
    } else {
      break;
    }
  }

  cout << max(counta, countb) << endl;
}


Error: TLE

Fixed submission:

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

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

  int countR = 0;
  int countW = 0;
  for (int i = 0; i < N; i++) {
    if (S.at(i) == 'R') {
      countR++;
    } else if (S.at(i) == 'W') {
      countW++;
    }
  }

  if (countR == 0 || countW == 0) {
    cout << 0 << endl;
    return 0;
  }

  int A = 0;
  int B = 0;
  vector<int> red(countR);
  vector<int> white(countW);
  for (int i = 0; i < N; i++) {
    if (S.at(i) == 'R') {
      red.at(A) = i;
      A++;
    } else if (S.at(i) == 'W') {
      countW++;
      white.at(B) = i;
      B++;
    }
  }
  sort(red.begin(), red.end());
  reverse(red.begin(), red.end());
  sort(white.begin(), white.end());

  int count = 0;
  while (red.at(0) > white.at(0)) {
    int C = red.at(0);
    int D = white.at(0);
    red.at(0) = D;
    white.at(0) = C;
    sort(red.begin(), red.end());
    reverse(red.begin(), red.end());
    sort(white.begin(), white.end());
    count++;
  }
  cout << count << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll n, cnt = 0, ans = 0;
  cin >> n;
  char a[200005];
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  for (ll i = 1; i <= n; i++) {
    if (a[i] == 'W')
      cnt++;
  }
  for (ll i = n - cnt + 1; i <= n; i++) {
    if (a[i] != 'W')
      ans++;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, cntW = 0, cntR = 0, nWR = 0, nRW = 0;
  cin >> n;
  char c;
  string s;
  while (n--) {
    cin >> c;
    (c == 'W') ? cntW++ : cntR++;
    s += c;
  }
  for (int i = 0, j = n - cntW; i < cntR, j < n; i++, j++) {
    if (s[i] == 'W')
      nWR++;
    if (s[j] == 'R')
      nRW++;
  }
  cout << max(nWR, nRW) << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define INF 1000000000000
typedef long long ll;

vector<char> c;

int main() {
  int n, w = 0, r = 0;
  char tempC;
  cin >> n;
  REP(i, n) {
    cin >> tempC;
    c.push_back(tempC);
  }

  REP(i, n) {
    if (c[i] == 'R') {
      r++;
    }
  }
  int result = max(w, r);
  REP(i, n) {
    if (c[i] == 'W') {
      w++;
    } else if (c[i] == 'R') {
      r--;
    }
    int now = max(w, r);
    result = min(result, now);
  }
  cout << result << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define INF 1000000000000
typedef long long ll;

vector<char> c;

int main() {
  int n, result = 0;
  char tempC;
  cin >> n;
  REP(i, n) {
    cin >> tempC;
    c.push_back(tempC);
  }
  REP(i, n) {
    if (c[i] == 'W') {
      for (int j = n - 1; i < j; j--) {
        if (c[j] == 'R') {
          c[j] = 'N';
          result++;
          break;
        }
      }
    }
  }
  cout << result << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;

  int count = 0;
  int j_index = N - 1;
  for (int i = 0; i < N; i++) {
    if (i >= j_index) {
      break;
    }

    if (s[i] == 'R') {
      continue;
    }

    for (int j = j_index; j >= 0; j--) {
      j_index = j - 1;
      if (s[j] == 'R' && j > i) {
        count += 1;
        break;
      }
    }
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;

  int count = 0;
  int j_index = N - 1;
  for (int i = 0; i < N; i++) {
    if (s[i] == 'R') {
      continue;
    }

    if (i >= j_index) {
      break;
    }

    for (int j = j_index; j >= 0; j--) {
      if (s[j] == 'R' && j > i) {
        count += 1;
        j_index = j - 1;
        break;
      }
    }
  }

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

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

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

  int n;
  cin >> n;
  string s;
  cin >> s;
  int ans = 0;
  for (int l = 0, r = n - 1; l < r;) {
    if (s[l] == 'W') {
      if (s[r] == 'R') {
        swap(s[l], s[r]);
        ans++;
        l++;
      }

      r--;
    } else {
      l++;
    }
  }

  cout << ans << '\\n';
  return 0;
}

Error: WA

Fixed submission:

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\\n"
const int mod = 1e9 + 7;
const int MAX = 1e6 + 7;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> a;
  for (int i = 0; i < n; i++)
    if (s[i] == 'R')
      a.push_back(i);
  int ans = 0, cnt = 0, l = 1, r = a.size();
  for (int i = 0; i < n; i++) {
    if (i == a[cnt])
      cnt++;
    else {
      ans++;
      r--;
    }
    if (cnt >= r) {
      break;
    }
  }
  cout << ans;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdlib.h>
#include <vector>
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
const int MAXN = 2e5 + 10;
const int mod = 1e9 + 7;
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
#define eps 1e-8
#define PI 3.1415926535898
#define lowbit(x) (x & (-x))
using namespace std;
// getline(cin,s);
// set<ll>::iterator it;
// map<int,int>::iterator it;
typedef long long ll;
char s[MAXN];
int main() {
  // 前R后W
  // 大体思路 从前往后一遍 到第i个前面有几个r几个w 后面有几个r几个w 然后贪心
  // 因为需要前面R 后面W 所以前面w的个数个后面r的个数的max值
  int n;
  scanf("%d", &n);
  scanf("%s", s + 1);
  int ans = inf, r = 0, w = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'R')
      r++;
    else
      w++;
  }
  int r1 = 0, w1 = 0, r2 = r, w2 = w;
  for (int i = 0; i <= n; i++) {
    ans = min(ans, max(w1, r2));
    if (i) {
      if (s[i] == 'R') {
        r1++;
        r2--;
      } else {
        w1++;
        w2--;
      }
    }
  }
  ans = min(ans, max(w1, r2));
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdlib.h>
#include <vector>
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
const int MAXN = 2e5 + 10;
const int mod = 1e9 + 7;
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
#define eps 1e-8
#define PI 3.1415926535898
#define lowbit(x) (x & (-x))
using namespace std;
// getline(cin,s);
// set<ll>::iterator it;
// map<int,int>::iterator it;
typedef long long ll;
char s[MAXN];
int main() {
  /*	W R W W R W R R
          R R W W W W R R
          R R W W W W W W
          全R  4
          全W  4
          前面R 后面W
          4R4W
          R R R R W W W W */
  int n;
  scanf("%d", &n);
  scanf("%s", s + 1);
  int ans = inf;
  for (int i = 0; i <= n; i++) {
    int sum = 0, r = 0, c = 0;
    for (int j = 1; j <= n; j++) {
      if (j <= i) {
        if (s[j] != 'R') {
          r++;
        }
      } else {
        if (s[j] != 'W') {
          c++;
        }
      }
    }
    ans = min(ans, max(r, c));
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int solve(int k);

int main() {

  int n;
  cin >> n;

  char *str = new char[n];
  cin >> str;

  int count = 0;
  int i = 0;
  int j = n - 1;

  while (1) {
    while (str[i] != 'W') {

      i++;
      if (i == n)
        break;
    }

    while (str[j] != 'R') {
      j--;
      if (j < 0)
        break;
    }

    if (i >= j)
      break;
    i++;
    j--;
    count++;
  }

  printf("%d\\n", count);

  delete str;
  return 0;
}


Error: -11

Fixed submission:

#include <iostream>

using namespace std;

int solve(int k);

int main() {

  int n;
  cin >> n;

  char *str = new char[n];
  cin >> str;

  int count = 0;
  int i = 0;
  int j = n - 1;

  while (1) {
    while (str[i] != 'W')
      i++;

    while (str[j] != 'R')
      j--;

    if (i >= j)
      break;
    i++;
    j--;
    count++;
  }

  printf("%d\\n", count);

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

#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef pair<lli, lli> pii;
#define ff first
#define ss second
#define MOD 1000000007
#define pb push_back
#define EPS 1e-6
const lli MAX = 1e9 + 7;

int main() {
  fast;
  int n;
  cin >> n;
  string s;
  cin >> s;
  int r = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      r++;
  }
  int cnt = r;
  for (int i = 0; i < r; i++) {
    if (s[i] == 'R')
      cnt--;
  }
  cout << cnt << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
typedef pair<lli, lli> pii;
#define ff first
#define ss second
#define MOD 1000000007
#define pb push_back
#define EPS 1e-6
const lli MAX = 1e9 + 7;

int main() {
  fast;
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0, w = 0, r = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      r++;
  }
  for (int i = 0; i < n;) {
    while (i < n && s[i] == 'W') {
      w++;
      i++;
    }
    cnt += min(r, w);
    r -= w;
    w = 0;
  }
  cout << cnt << "\\n";
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int most_right_white(char *c, int N, int b) {
  for (int i = b; i < N; i++) {
    if (c[i] == 'R')
      continue;
    if (c[i] == 'W')
      return i;
  }
  return N - 1;
}

int most_left_red(char *c, int N, int t) {
  for (int i = t; i >= 0; i--) {
    if (c[i] == 'W')
      continue;
    if (c[i] == 'R')
      return i;
  }
  return 0;
}

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

  int count = 0;
  int b = most_right_white(c, N, 0);
  int t = most_left_red(c, N, N - 1);
  while (b < t) {
    char tmp = c[b];
    c[b] = c[t];
    c[t] = tmp;
    count++;
    b = most_right_white(c, N, b);
    t = most_left_red(c, N, t);
  }

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

Error: TLE

Fixed submission:

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

int most_right_white(char *c, int N) {
  for (int i = 0; i < N; i++) {
    if (c[i] == 'R')
      continue;
    if (c[i] == 'W')
      return i;
  }
  return N - 1;
}

int most_left_red(char *c, int N) {
  for (int i = N - 1; i >= 0; i--) {
    if (c[i] == 'W')
      continue;
    if (c[i] == 'R')
      return i;
  }
  return 0;
}

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

  int count = 0;
  int b = most_right_white(c, N);
  int t = most_left_red(c, N);
  while (b < t) {
    char tmp = c[b];
    c[b] = c[t];
    c[t] = tmp;
    count++;
    b = most_right_white(c, N);
    t = most_left_red(c, N);
  }

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

#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll mod = 1000000007;
const ll INF = 1001001001;
const ll LINF = 1001001001001001001;

void prvec(vector<ll> vec) {
  ll n = vec.size();
  rep(i, n) cout << i << " " << vec.at(i) << "\\n";
}

void pr2d(vector<vector<ll>> vvec) {
  ll h = vvec.size();
  ll w = vvec.at(0).size();
  rep(i, h) {
    rep(j, w) { cout << vvec.at(i).at(j) << " "; }
    cout << "\\n";
  }
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll l = 0, r = n - 1, cnt = 0;
  while (r - l >= 1) {
    while (s.at(l) == 'R') {
      l++;
      if (l >= n)
        goto aa;
    }
    s.at(l) = 'R';
    while (s.at(r) == 'W') {
      r--;
      if (r < 0)
        goto aa;
    }
    s.at(r) = 'W';
    if (r - l < 1)
      break;
    cnt++;
    // cout << s << endl;
    // cout << l << " " << r << endl;
  }
aa:
  cout << cnt << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll mod = 1000000007;
const ll INF = 1001001001;
const ll LINF = 1001001001001001001;

void prvec(vector<ll> vec) {
  ll n = vec.size();
  rep(i, n) cout << i << " " << vec.at(i) << "\\n";
}

void pr2d(vector<vector<ll>> vvec) {
  ll h = vvec.size();
  ll w = vvec.at(0).size();
  rep(i, h) {
    rep(j, w) { cout << vvec.at(i).at(j) << " "; }
    cout << "\\n";
  }
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll l = 0, r = n - 1, cnt = 0;
  while (r - l > 1) {
    while (s.at(l) == 'R') {
      l++;
    }
    s.at(l) = 'R';
    while (s.at(r) == 'W') {
      r--;
    }
    s.at(r) = 'W';
    if (r - l < 1)
      break;
    cnt++;
    // cout << s << endl;
    // cout << l << " " << r << endl;
  }
  cout << cnt << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int i = 0, j = n - 1, ans = 0;
  while (i <= j) {
    while (i < n && s[i] != 'W')
      i++;
    while (j >= 0 && s[j] != 'R')
      j--;
    if (i > j)
      break;
    swap(s[i], s[j]);
    ans++;
    i++;
    j--;
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  // int r=0, w=0;
  // for(char ch : s) if(ch == 'R') r++; else w++;
  // if(r==0 || w==0) {cout<<0; return 0;}
  int i = 0, j = n - 1, ans = 0;
  while (i <= j) {
    while (i < n && s[i] != 'W')
      i++;
    while (i >= 0 && s[j] != 'R')
      j--;
    if (i > j)
      break;
    swap(s[i], s[j]);
    ans++;
    i++;
    j--;
  }
  cout << ans;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
const double pi = 3.14159265358979323846;
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
int main() {
  speed;

  string s;
  ll n, w = 0, r = 0;
  cin >> n >> s;

  r = count(s.begin(), s.end(), 'R');

  for (int i = 0; i < r; i++) {
    if (s[i] == 'W')
      w++;
  }
  cout << w;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
const double pi = 3.14159265358979323846;
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
int main() {
  speed;

  string s;
  ll n, w = 0, r = 0;
  cin >> n;

  r = count(s.begin(), s.end(), 'R');

  for (int i = 0; i < (!r ? n : r); i++) {
    if (s[i] == 'W')
      w++;
  }
  cout << w;
  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++)
using namespace std;
using ll = long long;

int main(void) {
  int N;
  cin >> N;
  string c;
  cin >> c;
  int red = 0, white = 0;
  int ans_min = 300000;
  int count_W = 0, count_R = 0;
  for (int i = 0; i < N; i++) {
    if (c[i] == 'R')
      red++;
    else if (c[i] == 'W')
      white++;
  }

  if (N == red || N == white) {
    cout << 0 << endl;
    return 0;
  }

  for (int i = 0; i < N - 1; i++) {
    int ans;
    if (c[i] == 'R')
      count_R++;
    else if (c[i] == 'W')
      count_W++;
    int count = red - count_R;
    if (count_W <= count)
      ans = count;
    else
      ans = count_W;
    if (ans_min > ans)
      ans_min = ans;
  }
  cout << ans_min << endl;
}

Error: TLE

Fixed submission:

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

int main(void) {
  int N;
  cin >> N;
  string c;
  cin >> c;
  int red = 0, white = 0;
  int ans_min = 300000;
  for (int i = 0; i < N; i++) {
    if (c[i] == 'R')
      red++;
    else if (c[i] == 'W')
      white++;
  }

  for (int i = 0; i < N - 1; i++) {
    int count_W = 0, count_R = 0;
    int ans;
    for (int j = 0; j <= i; j++) {
      if (c[j] == 'R')
        count_R++;
      else if (c[j] == 'W')
        count_W++;
    }
    int count = red - count_R;
    if (count_W == 0)
      ans = 0;
    else {
      if (count_W <= count)
        ans = count;
      else
        ans = count_W;
    }
    if (ans_min > ans)
      ans_min = ans;
  }
  cout << ans_min << endl;
}