alexjercan/bugnet huggingface dataset:
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
#define N 300001
typedef long long ll;
typedef long double ld;

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()

int stk[N], top;
int main() {
  fastIO;

  int n, m = 0, cnt = 0, ans = 0;
  string s;

  cin >> n >> s;
  for (int i = 0; i < n; i++)
    if (s[i] == 'R')
      m++;
    else
      stk[top++] = i;

  for (int i = 0; i < n && cnt < m; i++) {
    if (s[i] == 'R')
      cnt++;
    else {
      int idx = stk[--top];
      s[idx] = 'W';
      s[i] = 'R';
      cnt++, ans++;
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
#define N 300001
typedef long long ll;
typedef long double ld;

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()

int main() {
  fastIO;

  int n, m = 0, cnt = 0, ans = 0;
  string s;

  cin >> n >> s;
  for (int i = 0; i < n; i++)
    if (s[i] == 'R')
      m++;

  for (int i = 0; i < n && cnt < m; i++) {
    if (s[i] == 'R')
      cnt++;
    else {
      for (int j = n - 1; j > i; j--)
        if (s[j] == 'R') {
          s[i] = 'R';
          s[j] = 'W';
          cnt++, ans++;
          break;
        }
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: 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;
          }
          now = j + 1;
        }
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>

using namespace std;

int main() {
  int N;
  cin >> N;
  string str;
  cin >> str;
  int c[N];
  int N0 = 0, N1 = 0;

  for (int i = 0; i < N; i++) {
    c[i] = (str.at(i) == 'R') ? 0 : 1;
    if (c[i] == 0) {
      N0++;
    } else {
      N1++;
    }
  }
  if (N0 == N || N1 == N) {
    cout << 0;
    return 0;
  }

  int i_leftmost_1, i_rightmost_0;

  for (int i = 0; i < N; i++) {
    if (c[i] == 1) {
      i_leftmost_1 = i;
      break;
    }
  }

  for (int i = N - 1; i >= 0; i--) {
    if (c[i] == 0) {
      i_rightmost_0 = i;
      break;
    }
  }

  int swapped = 0;

  while (i_leftmost_1 < i_rightmost_0) {
    c[i_leftmost_1] = 0;
    c[i_rightmost_0] = 1;
    swapped++;
    for (int i = i_leftmost_1 + 1; i < N; i++) {
      if (c[i] == 1) {
        i_leftmost_1 = i;
        break;
      }
    }

    for (int i = i_rightmost_0 - 1; i >= 0; i--) {
      if (c[i] == 0) {
        i_rightmost_0 = i;
        break;
      }
    }
  }
  cout << swapped;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>

using namespace std;

int main() {
  int N;
  cin >> N;
  string str;
  cin >> str;
  int c[N];

  for (int i = 0; i < N; i++) {
    c[i] = (str.at(i) == 'R') ? 0 : 1;
  }

  int i_rightmost_0, i_leftmost_1;
  for (int i = 0; i < N; i++) {
    if (c[i] == 1) {
      i_leftmost_1 = i;
      break;
    }
    if (i == N - 1) {
      cout << 0;
      return 0;
    }
  }

  for (int i = N - 1; i >= 0; i--) {
    if (c[i] == 0) {
      i_rightmost_0 = i;
      break;
    }
    if (i == 0) {
      cout << 0;
      return 0;
    }
  }

  int swapped = 0;

  while (i_leftmost_1 < i_rightmost_0) {
    c[i_leftmost_1] = 0;
    c[i_rightmost_0] = 1;
    swapped++;
    for (int i = 0; i < N; i++) {
      if (c[i] == 1) {
        i_leftmost_1 = i;
        break;
      }
    }

    for (int i = N - 1; i >= 0; i--) {
      if (c[i] == 0) {
        i_rightmost_0 = i;
        break;
      }
    }
  }
  cout << swapped;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

Error: 0

Fixed submission:

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using ll = long long;
using ld = long double;
using namespace std;
const int INF = 1e9 + 100;
const ll INF64 = 7e18l;
const int mod = 1000000007;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define FIN                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

int main() {
  FIN;
  ll n;
  cin >> n;
  vector<char> c(n);
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    cin >> c[i];
    if (c[i] == 'W')
      ans++;
  }

  ll ans2 = 0;
  ll cur = n - 1;
  for (ll i = 0; i < n; i++) {
    if (c[i] == 'W') {
      for (ll j = cur; j >= i; j--) {
        if (c[j] == 'R') {
          c[i] = 'R';
          c[j] = 'W';
          ans2++;
          break;
        }
        cur = j;
      }
    }
    if (cur <= i)
      break;
  }

  cout << min(ans, ans2) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using ll = long long;
using ld = long double;
using namespace std;
const int INF = 1e9 + 100;
const ll INF64 = 7e18l;
const int mod = 1000000007;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define FIN                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

int main() {
  FIN;
  ll n;
  cin >> n;
  vector<char> c(n);
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    cin >> c[i];
    if (c[i] == 'W')
      ans++;
  }

  ll ans2 = 0;
  ll cur = n - 1;
  for (ll i = 0; i < n; i++) {
    if (c[i] == 'W') {
      for (ll j = cur; j >= i; j--) {
        if (c[j] == 'R') {
          c[i] = 'R';
          c[j] = 'W';
          cur = j;
          ans2++;
          break;
        }
      }
    }
    if (cur <= i)
      break;
  }

  cout << min(ans, ans2) << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {

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

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {

  int n;
  cin >> n;
  string s;
  cin >> s;
  int count = 0;
  for (int i = 0; i < n; i++) {

    if (s[i] == 'W') {
      for (int j = n - 1; j > i; j--) {
        if (s[j] == 'R') {
          swap(s[i], s[j]);
          count++;
          break;
        }
      }
    }
  }
  cout << count;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: 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 = 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;
}

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

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

int main() {

  int n;
  cin >> n;

  string s;
  cin >> s;

  // 赤を左に寄せる
  // 左からと右から
  int64_t left = 0, right = n - 1;

  int64_t cnt = 0;
  while (left < right) {

    while (s.at(left) == 'R') {
      left++;
      if (left == n - 1)
        break;
    }

    while (s.at(right) == 'W') {
      right--;
      if (left == 0)
        break;
    }

#if 0
    cout<<"---------------"<<endl;
    cout<<s<<endl;
    cout<<"left="<<left<<endl;
    cout<<"right="<<right<<endl;
    cout<<"cnt="<<cnt<<endl;
    cout<<"---------------"<<endl;
#endif

    if (s.at(left) == 'W' && s.at(right) == 'R' && left < right) {
      char wk = s.at(left);
      s.at(left) = s.at(right);
      s.at(right) = wk;
      cnt++;
    }
  }

  cout << cnt << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int main() {

  int n;
  cin >> n;

  string s;
  cin >> s;

  // 赤を右に寄せる
  // 左からと右から
  int64_t left = 0, right = n - 1;

  int64_t cnt = 0;
  while (left <= right) {

    while (s.at(left) == 'R') {
      left++;
      if (left == n - 1)
        break;
    }

    while (s.at(right) == 'W' && right >= 0) {
      right--;
      if (left == 0)
        break;
    }

    char wk = s.at(left);
    s.at(left) = s.at(right);
    s.at(right) = wk;
    cnt++;
  }

  cout << cnt - 1 << endl;

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

#include <bits/stdc++.h>
using namespace std;
char a[200010];
int main() {
  int n, rz = 0, r = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == 'R')
      rz++;
  }
  for (int i = 1; i <= rz; i++)
    if (a[i] == 'R')
      r++;
  cout << rz - r;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
char a[10010];
int main() {
  int n, rz = 0, r = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == 'R')
      rz++;
  }
  for (int i = 1; i <= rz; i++)
    if (a[i] == 'R')
      r++;
  cout << rz - r;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x, y) memset(x, y, sizeof(x))
#define pb push_back
#define mk make_pair
#define pi pair<int, int>
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = a; i >= (b); --i)
using namespace std;
int dt[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {0, 0}};
// typedef pair<int, int> P;
// priority_queue<int, vector<int>, greater<int> > q;

const int N = 2e5 + 5;

int a[N]; // 储存i之前有多少个w

int main() {
  IOS;
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cntr = 0, cntw = 0;
  rep(i, 0, n - 1) {
    if (s[i] == 'W')
      ++cntw;
    a[i] = cntw;
  }
  // int cnt = 0;
  int res = 0;
  int tt = 0;
  per(i, n - 1, 0) {
    if (s[i] == 'R') {
      if (a[i] - tt > 0) {
        ++tt;
        ++res;
      }
    }
  }
  cout << res << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x, y) memset(x, y, sizeof(x))
#define pb push_back
#define mk make_pair
#define pi pair<int, int>
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = a; i >= (b); --i)
using namespace std;
int dt[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {0, 0}};
// typedef pair<int, int> P;
// priority_queue<int, vector<int>, greater<int> > q;
int main() {
  IOS;
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cntr = 0, cntw = 0;
  int cnt = 0;
  int res = 0;
  per(i, n - 1, 0) {
    if (s[i] == 'R') {
      rep(j, cnt, i - 1) {
        if (s[j] == 'W') {
          ++res;
          cnt = j + 1;
          break;
        }
      }
    }
  }
  cout << res << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
typedef long long ll;
typedef long double ld;

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()

#define N 300001

char c[N];
int stk[N], top = -1;
int main() {
  fastIO;

  int n, m = 0, ans = 0;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> c[i];
    if (c[i] == 'R')
      m++;
    else
      stk[++top] = i;
  }
  for (int i = 0, k = 0; i < n && k < m; i++, k++) {
    if (c[i] == 'W') {
      int idx = stk[top--];
      c[i] = 'R';
      c[idx] = 'W';
      ans++;
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
typedef long long ll;
typedef long double ld;

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()

#define N 300001

char c[N];
int main() {
  fastIO;

  int n, m = 0, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> c[i];
    if (c[i] == 'R')
      m++;
  }
  for (int i = 0, k = 0; i < n && k < m; i++, k++) {
    if (c[i] == 'W') {
      int j;
      for (j = n - 1; j >= i && c[j] == 'W'; j--)
        ;
      c[i] = 'R';
      c[j] = 'W';
      ans++;
    }
  }
  cout << ans << endl;

  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 FOR(i, m, n) for (int i = m; i < n; i++)
#define ll long long
#define MOD 1000000007
#define MAX 100010
#define aout(a)                                                                \\
  REP(i, sizeof(a)) cout << a[i] << " ";                                       \\
  cout << endl;
#define vout(v)                                                                \\
  REP(i, v, size()) cout << v[i] << " ";                                       \\
  cout << endl;
using namespace std;
using Graph = vector<vector<int>>;

int main() {
  ll N;
  cin >> N;
  string c;
  cin >> c;
  ll ans = 0;
  bool flag = false;
  int j = N - 1;
  REP(i, N) {
    if (flag)
      break;
    if (c[i] == 'R')
      continue;
    else {
      while (i < j) {
        if (c[j] == 'R') {
          ans++;
          // cout<<i<<" "<<j<<endl;
          c[j] = 'W';
          break;
        }
        j--;
        if (i == j)
          flag = true;
      }
    }
  }

  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ll long long
#define MOD 1000000007
#define MAX 100010
#define aout(a)                                                                \\
  REP(i, sizeof(a)) cout << a[i] << " ";                                       \\
  cout << endl;
#define vout(v)                                                                \\
  REP(i, v, size()) cout << v[i] << " ";                                       \\
  cout << endl;
using namespace std;
using Graph = vector<vector<int>>;

int main() {
  ll N;
  cin >> N;
  string c;
  cin >> c;
  ll ans = 0;
  bool flag = false;
  REP(i, N) {
    if (flag)
      break;
    if (c[i] == 'R')
      continue;
    else {
      int j = N - 1;
      while (i < j) {
        if (c[j] == 'R') {
          ans++;
          // cout<<i<<" "<<j<<endl;
          c[j] = 'W';
          break;
        }

        j--;
        if (i == j)
          flag = true;
      }
    }
  }

  cout << ans;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
const double PI = acos(-1);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
int findSumOfDigits(int n) {
  int sum = 0;
  while (n > 0) { // n が 0 になるまで
    sum += n % 10;
    n /= 10;
  }
  return sum;
}
int main() {
  long long n;
  cin >> n;
  int ans = 0;
  bool f = false;
  int m = 0;
  vector<char> c(n);
  rep(i, n) cin >> c.at(i);
  for (int i = n - 1; i >= 0; i--) {
    if (c.at(i) == 'R') {
      for (int j = m; j < n; j++) {
        if (c.at(j) == 'W') {
          swap(c.at(j), c.at(i));
          ans++;
          m = j;
          break;
        }
        if (j == i) {
          f = true;
          break;
        }
      }
    }
    if (f)
      break;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long long n;
  cin >> n;
  int ans = 0;
  bool f = false;
  vector<char> c(n);
  rep(i, n) cin >> c.at(i);
  for (int i = n - 1; i >= 0; i--) {
    if (c.at(i) == 'R') {
      rep(j, n) {
        if (c.at(j) == 'W') {
          swap(c.at(j), c.at(i));
          ans++;
          break;
        }
        if (j == i) {
          f = true;
          break;
        }
      }
    }
    if (f)
      break;
  }
  cout << ans << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

//
// Created by TakahiroYamaji on 2020/08/03.
//
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int N;
string S;
int main() {
  cin >> N >> S;
  int a = 0, b = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == 'R')
      a++;
  }
  int res = max(a, b);
  for (int i = 0; i < N; i++) {
    if (S[i] == 'R')
      --a;
    else
      ++b;
    res = std::min(res, max(a, b));
  }
  cout << res << endl;
}


Error: 0

Fixed submission:

//
// Created by TakahiroYamaji on 2020/08/02.
//
#include <iostream>
#include <string>
using namespace std;
int n;
string S;
int main() {
  cin >> n;
  int flg = 0;
  int res;
  int r = 0;
  int w = 0;
  for (int i = 0; i < n; i++) {
    cin >> S[i];
    if (S[i] == 'R' && flg == 0) {
      flg = 1;
      res = i;
    }
    if (S[i] == 'R')
      r++;
    if (S[i] == 'W')
      w++;
  }
  int ss = res + r - 2;
  cout << ss << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;
int N;
string str;

int Ans() {
  int front = 0, back = str.size() - 1;
  int ans = 0;
  while (front < back) {
    while (front < back && str[front] != 'W')
      front++;
    while (back > front && str[back] != 'R')
      back--;
    if (front < back) {
      str[back++] = 'W';
      str[front++] = 'R';
      ans++;
    }
  }
  return ans;
}

int main() {
  cin >> N >> str;
  cout << Ans();
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int N;
string str;

int Ans() {
  int front = 0, back = str.size() - 1;
  int ans = 0;
  while (front < back) {
    while (str[front] != 'W')
      front++;
    while (str[back] != 'R')
      back--;
    if (front < back) {
      str[back++] = 'W';
      str[front++] = 'R';
      ans++;
    }
  }
  return ans;
}

int main() {
  cin >> N >> str;
  cout << Ans();
  return 0;
}