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

#include <iostream>
#include <math.h>
#include <string>

using namespace std;

typedef long long ll;

string c;

int pos;

int most_right_red_pos() {
  while (pos > 0) {
    if (c[pos] == 'R')
      return pos;

    pos--;
  }
  return pos;
}

int main() {
  ll n;
  cin >> n;
  pos = n - 1;

  cin >> c;

  int count = 0;

  for (int i = 0; i < n; i++) {
    if (i >= most_right_red_pos()) {
      break;
    }
    if (c[i] == 'W') {
      c[i] = 'R';
      c[most_right_red_pos()] = 'W';
      count++;
    }
  }

  if (c == "W") {
    cout << 0;
  } else {
    cout << count;
  }
}

//
// int main() {
//	ll k;
//
//	cin >> k;
//
//	if (k % 2 == 0) {
//		cout << -1;
//		return 0;
//	}
//
//	ll seven = 7;
//
//	while (true) {
//		if (seven % k == 0) {
//			cout << seven;
//			break;
//		}
//		seven = seven * 10 + 7;
//	}
//}


Error: WA

Fixed submission:

#include <iostream>
#include <math.h>
#include <string>

using namespace std;

typedef long long ll;

string c;

int pos;

int most_right_red_pos() {
  while (pos > 0) {
    if (c[pos] == 'R')
      return pos;

    pos--;
  }
}

int main() {
  ll n;
  cin >> n;
  pos = n - 1;

  cin >> c;

  int count = 0;

  for (int i = 0; i < n; i++) {
    if (c[i] == 'W') {
      c[i] = 'R';
      c[most_right_red_pos()] = 'W';
      count++;
    }
    if (i >= most_right_red_pos()) {
      break;
    }
  }

  cout << count;
}

//
// int main() {
//	ll k;
//
//	cin >> k;
//
//	if (k % 2 == 0) {
//		cout << -1;
//		return 0;
//	}
//
//	ll seven = 7;
//
//	while (true) {
//		if (seven % k == 0) {
//			cout << seven;
//			break;
//		}
//		seven = seven * 10 + 7;
//	}
//}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

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

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

  int answer = 0;
  int l = 0, r = n - 1;
  while (l < r) {
    while (s[l] != 'W' && l < n)
      l++;
    if (r <= l)
      break;
    while (s[r] != 'R' && r >= 0)
      r--;
    s[l] = 'R';
    s[r] = 'W';
    if (r <= l)
      break;

    answer++;
  }
  cout << answer << endl;
}

Error: 0

Fixed submission:

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

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

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

  int answer = 0;
  int l = 0, r = n - 1;
  while (l < r) {
    while (s[l] != 'W')
      l++;
    if (r <= l)
      break;
    while (s[r] != 'R')
      r--;
    s[l] = 'R';
    s[r] = 'W';
    if (r <= l)
      break;

    answer++;
  }
  cout << answer << endl;
}
Problem p02597 (C++) — buggy submission, verdict: 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++;
    else {
      i++;
      j--;
    }
  }
  print(ans) return 0;
}


Error: TLE

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


Error: TLE

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

#include <algorithm>
#include <iostream>
#include <map>

using namespace std;

typedef long long ll;
const int cnt = 1e7 + 7;

map<int, int> mp;

int main() {
  //	freopen("input.txt","r",stdin);

  int n;
  cin >> n;
  string s;
  cin >> s;
  int num = 0, ans = 0; // W的数量
  for (int i = 0; i < n; i++)
    if (s[i] == 'W')
      num++;
  for (int i = n - num; i < n; i++)
    if (s[i] == 'R')
      ans++;
  cout << ans;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>

using namespace std;

typedef long long ll;
const int cnt = 1e7 + 7;

map<int, int> mp;

int main() {
  //	freopen("input.txt","r",stdin);

  int len;
  cin >> len;
  string s;
  cin >> s;
  int cnt = len - 1;
  int ans = 0;
  for (int i = 0; i < len; i++) {
    if (s[i] == 'W') {
      for (int j = cnt; j > i; j--) {
        if (s[j] == 'R') {
          ans++;
          cnt = j - 1;
          break;
        }
      }
    }
  }
  cout << ans;

  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;
using P = pair<int, int>;
using Tuple = tuple<int, int, int, string>;

int main() {
  int N;
  string C;
  cin >> N >> C;
  int cnt = 0;
  rep(i, N) {
    if (C.at(i) == 'R')
      cnt++;
  }

  int ans = 0;
  for (int i = cnt; i < N; i++) {
    if (C.at(i) == 'R')
      ans++;
  }

  cout << ans << 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;
using P = pair<int, int>;
using Tuple = tuple<int, int, int, string>;

int main() {
  int N;
  cin >> N;
  string C;
  cin >> C;
  int cnt = 0;
  rep(i, N) {
    if (C.at(i) == 'R')
      cnt++;
  }

  // priority_queue<Tuple, vector<Tuple>, greater<Tuple>> que;
  queue<Tuple> que;
  que.push(Tuple(0, cnt, C.size(), C));

  int ans;
  while (true) {
    auto t = que.front();
    que.pop();

    int t1 = get<0>(t);
    int t2 = get<1>(t);
    int t3 = get<2>(t);
    string t4 = get<3>(t);

    if (t2 == 0 || t2 == t3) {
      ans = t1;
      break;
    }

    bool isR = t4.at(0) == 'R';
    t4 = t4.substr(1);
    t3--;

    if (isR) {
      que.push(Tuple(t1, t2 - 1, t3, t4));
      continue;
    }

    // que.push(Tuple(t1 + 1, t2, t3, t4));
    for (int i = t3; i > 0; i--) {
      if (t4.at(i - 1) == 'R') {
        string newT4 = t4;
        newT4.at(i - 1) = 'W';
        que.push(Tuple(t1 + 1, t2 - 1, t3, newT4));
        break;
      }
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define debug(arr) cout << ">>" << #arr << ':' << arr << endl;
#define endl "\\n"
#define pb push_back
#define fo(i, n) for (int i = 0; i < n; i++)
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(v) v.begin(), v.end()
int gcd(int a, int b) {
  while (a && b)
    a > b ? a %= b : b %= a;
  return a + b;
}

const int N = 200001;

int main() {
  IOS int n;
  cin >> n;
  string s;
  cin >> s;
  int idx = 0;
  int st = 0;
  for (int i = s.length() - 1; i >= 0; i--) {
    if (s[i] == 'R') {
      idx = i;
      break;
    }
  }

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

  for (int i = st; i < s.length(); i++) {
    if (idx <= i)
      break;
    if (s[i] == 'W') {
      for (int j = idx; j > i; j--) {
        if (s[j] == 'R') {
          swap(s[i], s[j]);
          count++;
          idx = j;
          break;
        }
      }
    }
  }
  cout << count << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define debug(arr) cout << ">>" << #arr << ':' << arr << endl;
#define endl "\\n"
#define pb push_back
#define fo(i, n) for (int i = 0; i < n; i++)
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(v) v.begin(), v.end()
int gcd(int a, int b) {
  while (a && b)
    a > b ? a %= b : b %= a;
  return a + b;
}

const int N = 200001;

int main() {
  IOS int n;
  cin >> n;
  string s;
  cin >> s;
  int idx = n - 1;
  int count = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'W') {
      for (int j = idx; j > i; j--) {
        if (s[j] == 'R') {
          swap(s[i], s[j]);
          count++;
          idx = j;
          break;
        }
      }
    }
  }
  cout << count << endl;
}
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;
  vector<int> w;
  vector<int> r;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W') {
      w.push_back(i);
    }
  }
  for (int i = n; i > 0; i--) {
    if (s[i] == 'R') {
      r.push_back(i);
    }
  }
  int ans = 0;
  int M = min(w.size(), r.size());
  for (int i = 0; i < M; i++) {
    if (w[i] < r[i])
      ans++;
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int q[100], ar = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W') {
      q[i] = 0;
    } else {
      q[i] = 1;
      ar++;
    }
  }
  for (int i = 1; i < n; i++) {
    q[i] += q[i - 1];
  }
  cout << max(ar - q[ar - 1], q[n - 1] - q[ar - 1]);
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define speedhack()                                                            \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define form(i, n) for (size_t i = 0; i < size_t(n); ++i)
#define forn(i, n) for (size_t i = 1; i <= size_t(n); ++i)
template <class T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
long long mod(long long a, long long b) { return (a % b + b) % b; }
long long gcd(long long x, long long y) { return __gcd(x, y); }
long long lcm(long long x, long long y) { return x * 1ll * y / gcd(x, y); }

const unsigned long long MAXN = 7777777777777777777;
const long long SIZE = 3e5 + 5;
const long long MOD = 1e9 + 7;

int N, res;
string s;

int main() {
  speedhack()

          cin >>
      N;

  cin >> s;

  for (int i = N - 1, j = 0; i >= 0; i--) {
    if (s[i] == 'R') {
      while (s[j] == 'R' && j < N)
        ++j;
      if (j < i) {
        swap(s[j++], s[i]);
        ++res;
      } else
        break;
    }
  }

  cout << res << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define speedhack()                                                            \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define form(i, n) for (size_t i = 0; i < size_t(n); ++i)
#define forn(i, n) for (size_t i = 1; i <= size_t(n); ++i)
template <class T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
long long mod(long long a, long long b) { return (a % b + b) % b; }
long long gcd(long long x, long long y) { return __gcd(x, y); }
long long lcm(long long x, long long y) { return x * 1ll * y / gcd(x, y); }

const unsigned long long MAXN = 7777777777777777777;
const long long SIZE = 3e5 + 5;
const long long MOD = 1e9 + 7;

int N, res, i, j;
string s;

int main() {
  speedhack()

          cin >>
      N;

  cin >> s;

  j = N - 1;

  while (i < j && j > 0 && i < N - 1) {
    if (s[j] == 'R') {
      while (s[i] != 'W')
        ++i;
      if (i < j) {
        swap(s[j--], s[i++]);
        ++res;
      } else
        break;
    } else
      --j;
  }

  cout << res << endl;

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int i = 0, j = N - 1, count = 0;
  string S1, S2;
  bool flag = true;
  cin >> S1;
  for (int i = 0; i < N; i++) {
    if (S1.at(i) == 'W')
      S1.at(i) = '0';
    if (S1.at(i) == 'R')
      S1.at(i) = '1';
  }
  S2 = S1;

  for (int i = 0; i < N - 1; i++) {
    if (S1.at(i) != S1.at(i + 1))
      flag = false;
  }
  if (flag) {
    cout << '0' << endl;
    return 0;
  }

  while (S1 <= S2) {

    S1 = S2;
    if (S2.at(i) == '0' && S2.at(j) == '1') {
      swap(S2.at(i), S2.at(j));
      i++;
      j--;
      if (S1 <= S2)
        count++;
    } else if (S2.at(i) != '0' && S2.at(j) == '1') {
      i++;
    } else if (S2.at(i) == '0' && S2.at(j) != '1') {
      j--;
    } else if (S2.at(i) != '0' && S2.at(j) != '1') {
      i++;
      j--;
    }
  }
  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int i = 0, j = N - 1, count = 0;
  string S1, S2;
  bool flag = true;
  cin >> S1;
  for (int i = 0; i < S1.size(); i++) {
    if (S1.at(i) == 'W')
      S1.at(i) = '0';
    if (S1.at(i) == 'R')
      S1.at(i) = '1';
  }
  S2 = S1;

  for (int i = 0; i < N - 1; i++) {
    if (S1.at(i) != S1.at(i + 1))
      flag = false;
  }
  if (flag) {
    cout << '0' << endl;
    return 0;
  }

  while (S1 <= S2) {

    S1 = S2;
    if (S2.at(i) == '0' && S2.at(j) == '1') {
      swap(S2.at(i), S2.at(j));
      i++;
      j--;
      if (S1 <= S2)
        count++;
    } else if (S2.at(i) != '0' && S2.at(j) == '1') {
      i++;
    } else if (S2.at(i) == '0' && S2.at(j) != '1') {
      j--;
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define eps 0.000001;
using namespace std;
typedef long long ll;
const int inf = 0xfff;
const int maxn = 200005;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
struct node {
  int x, y, d;
  node() { x = y = d = 0; };
};
int main() {
  int n;
  cin >> n;
  string maze;
  cin >> maze;
  int i = 0, j = n - 1;
  int cnt = 0;
  for (int l = 0, r = n - 1; l < r;) {
    if (maze[l] == 'W') {
      if (maze[r] == 'R') {
        swap(maze[l], maze[r]);
        cnt++;
        l++;
      }
      r--;
    } else {
      l++;
    }
  }
  cout << cnt << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define eps 0.000001;
using namespace std;
typedef long long ll;
const int inf = 0xfff;
const int maxn = 200005;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
struct node {
  int x, y, d;
  node() { x = y = d = 0; };
};
int main() {
  int n;
  cin >> n;
  string maze;
  cin >> maze;
  int i = 0, j = n - 1;
  int cnt = 0;
  while (i < j) {
    while (maze[i] != 'W')
      i++;
    while (maze[j] != 'R')
      j--;
    swap(maze[i], maze[j]);
    i++;
    if (i > j)
      cnt++;
  }
  cout << cnt << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long long MOD = 1e9 + 7;
int MAX_INT = 1e9;

long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

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

  int N;
  cin >> N;
  vector<char> c(N), d(N);
  for (int i = 0; i < N; i++) {
    cin >> c[i];
  }
  int red = 0, white = 0;
  for (int i = 0; i < N; i++) {
    red += (c[i] == 'R');
  }
  for (int i = 0; i < red; i++) {
    white += (c[i] == 'W');
  }
  cout << white << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

long long MOD = 1e9 + 7;
int MAX_INT = 1e9;

long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

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

  int N;
  cin >> N;
  char c[200000];
  vector<int> width;
  int cnt_r = 0;
  for (int i = 0; i < N; i++) {
    cin >> c[i];

    if (c[i] == 'R') {
      cnt_r++;
    }
  }

  int cnt_w = 0;
  int ans = 0;
  int r_pos = N - 1;
  for (int i = 0; i < N; i++) {
    if (c[i] == 'W') {
      cnt_w++;
    } else {
      if (cnt_w == 0) {
        continue;
      }
      if (cnt_w < cnt_r) {
        for (int j = 0; j < N; j++) {
          if (c[r_pos] == 'R') {
            c[r_pos] == 'W';
            cnt_w--;
            cnt_r--;
            r_pos--;
            ans++;
          }
          if (cnt_w <= 0) {
            break;
          }
        }
        cnt_r--;
      } else {
        ans += cnt_r;
        break;
      }
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define P pair<ll, ll>
#define Graph vector<vector<P>>
#define fi first
#define se second
constexpr ll mod = 1000000007;
constexpr ll INF = (1ll << 60);
constexpr double pi = 3.14159265358979323846;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  deque<ll> white, red;

  bool allred = true, allwhite = true;
  rep(i, (ll)(s.size())) {
    if (s[i] == 'W')
      allred = false;
    if (s[i] == 'R')
      allwhite = false;
  }
  if (allred || allwhite) {
    cout << 0 << endl;
    return 0;
  }

  ll ans = 0;

  rep(i, (ll)(s.size())) {
    if (s[i] == 'W') {
      white.push_back(i);
    } else {
      red.push_back(i);
    }
  }

  while (1) { // 交換
    ll noww = white.front();
    ll nowr = red.back();

    white.pop_front();
    red.pop_back();

    if (noww >= nowr)
      break;

    white.push_back(nowr);
    red.push_front(noww);
    ans++;
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define P pair<ll, ll>
#define Graph vector<vector<P>>
#define fi first
#define se second
constexpr ll mod = 1000000007;
constexpr ll INF = (1ll << 60);
constexpr double pi = 3.14159265358979323846;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  deque<ll> white, red;

  bool allred = true;
  rep(i, (ll)(s.size())) {
    if (s[i] == 'W')
      allred = false;
  }
  if (allred) {
    cout << 0 << endl;
    return 0;
  }

  ll maxwhite = 0, ans = 0;
  rep(i, (ll)(s.size())) {
    if (s[i] == 'R')
      maxwhite = i;
  }

  rep(i, (ll)(s.size())) {
    if (s[i] == 'W') {
      white.push_back(i);
    } else {
      red.push_back(i);
    }
  }

  while (1) { // 交換
    ll noww = white.front();
    ll nowr = red.back();

    white.pop_front();
    red.pop_back();

    if (noww >= nowr)
      break;

    white.push_back(nowr);
    red.push_front(noww);
    ans++;
  }

  cout << ans << endl;

  return 0;
}