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

#include <bits/stdc++.h>
#define INF 1e18
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define arr array
#define _lower_bound(a, x) lower_bound(a.begin(), a.end(), x) - a.begin()
#define _upper_bound(a, x) upper_bound(a.begin(), a.end(), x) - a.begin()
#define _debug(x) cout << #x << " = " << x << endl;
#define _sort(x) sort(x.begin(), x.end())
#define _rsort(x) sort(x.rbegin(), x.rend())
#define _reverse(x) reverse(x.begin(), x.end())
typedef long long ll;
using namespace std;

void solve() {
  int n, cnt = 0, l = 0;
  cin >> n;
  string s;
  cin >> s;
  for (int i = n - 1; i >= 1; --i) {
    if (s[i] == 'R') {
      bool flag = false;
      for (int j = l; j < i; ++j) {
        if (s[j] == 'W') {
          l = j;
          flag = true;
          swap(s[i], s[j]);
          ++cnt;
          break;
        }
      }
      if (!flag)
        break;
    }
  }
  // for (int i = 1; i < n; ++i) {
  // 	if (s[i] == 'R' && s[i - 1] == 'W') {
  // 		bool flag = false;
  // 		for (int k = 0; k < i; ++k) {
  // 			if (s[k] == 'W') {
  // 				flag = true;
  // 				++cnt;
  // 				swap(s[i], s[k]);
  // 				break;
  // 			}
  // 		}
  // 		if (!flag) {
  // 			s[i - 1] = 'R';
  // 			++cnt;
  // 		}
  // 	}
  // }
  cout << cnt << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);

  // int t; cin >> t;
  // while(t--)
  solve();
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 1e18
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define arr array
#define _lower_bound(a, x) lower_bound(a.begin(), a.end(), x) - a.begin()
#define _upper_bound(a, x) upper_bound(a.begin(), a.end(), x) - a.begin()
#define _debug(x) cout << #x << " = " << x << endl;
#define _sort(x) sort(x.begin(), x.end())
#define _rsort(x) sort(x.rbegin(), x.rend())
#define _reverse(x) reverse(x.begin(), x.end())
typedef long long ll;
using namespace std;

void solve() {
  int n, cnt = 0;
  cin >> n;
  string s;
  cin >> s;
  for (int i = n - 1; i >= 1; --i) {
    if (s[i] == 'R') {
      bool flag = false;
      for (int j = 0; j < i; ++j) {
        if (s[j] == 'W') {
          flag = true;
          swap(s[i], s[j]);
          ++cnt;
          break;
        }
      }
      if (!flag)
        break;
    }
  }
  // for (int i = 1; i < n; ++i) {
  // 	if (s[i] == 'R' && s[i - 1] == 'W') {
  // 		bool flag = false;
  // 		for (int k = 0; k < i; ++k) {
  // 			if (s[k] == 'W') {
  // 				flag = true;
  // 				++cnt;
  // 				swap(s[i], s[k]);
  // 				break;
  // 			}
  // 		}
  // 		if (!flag) {
  // 			s[i - 1] = 'R';
  // 			++cnt;
  // 		}
  // 	}
  // }
  cout << cnt << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);

  // int t; cin >> t;
  // while(t--)
  solve();
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF long long int(1e18 + 1)
#define rep(i, n, m) for (int i = n; i < m; i++)

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<char> a(n);
  vector<int> w, r;
  rep(i, 0, n) {
    cin >> a[i];
    if (a[i] == 'W')
      w.push_back(i);
    else
      r.push_back(i);
  }
  reverse(r.begin(), r.end());
  int ans = 0;
  int W, R;
  W = w.size();
  R = r.size();
  if (W && R) {
    for (int i = 0; i < W && i < R; i++) {
      if (w[i] < r[i])
        ans++;
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF long long int(1e18 + 1)
#define rep(i, n, m) for (int i = n; i < m; i++)

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<char> a(n);
  vector<int> w, r;
  rep(i, 0, n) {
    cin >> a[i];
    if (a[i] == 'W')
      w.push_back(i);
    else
      r.push_back(i);
  }
  reverse(r.begin(), r.end());
  int ans = 0;
  int W, R;
  W = w.size();
  R = r.size();
  if (W && R) {
    for (int i = 0; i < W || i < R; i++) {
      if (w[i] < r[i])
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<char> c(n);
  int red = 0;
  for (int i = 0; i < n; i++) {
    cin >> c.at(i);
    if (c.at(i) == 'R')
      red++;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (c.at(i) == 'W' && red != 0) {
      ans++;
      red--;
    } else if (c.at(i) == 'R' && red != 0)
      red--;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<char> c(n);
  int tmp = 0;
  int ans = 0;
  int white = 0;
  for (int i = 0; i < n; i++) {
    cin >> c.at(i);
    if (c.at(i) == 'W')
      white++;
  }
  for (int i = n - 1; i >= tmp; i--) {
    if (c.at(i) == 'R') {
      for (int j = tmp; j < i; j++) {
        if (c.at(j) == 'W') {
          ans++;
          c.at(j) = 'R';
          c.at(i) = 'W';
          tmp = j + 1;
          break;
        }
      }
    }
  }
  cout << min(ans, white) << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define DEBUG 0

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

  vector<char> c(N);

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

  int counter = 0;

  int reverse_count = N - 1;

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

          break;

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

  cout << counter << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define DEBUG 0

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

  vector<char> c(N);

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

  int counter = 0;

  int reverse_count = N - 1;

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

          break;

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

  cout << counter << endl;

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

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


Error: 0

Fixed submission:

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

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

int main(void) {
  int n;
  string c;
  cin >> n;
  cin >> c;

  int i = 0;
  int j = 0;
  int count = 0;

  for (i = 0; i < n; i++) {
    if (c[i] == 'R')
      j++;
  }
  for (i = 0; i < j; i++) {
    if (c[i] == 'W')
      count++;
  }
  cout << count << endl;
}

Error: 0

Fixed submission:

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

int main(void) {
  int n;
  string c;
  cin >> n;
  cin >> c;

  int i = 0;
  int j = n - 1;
  int count = 0;
  char x = c[i];
  char y = c[j];

  while (i < j) {
    while (x != 'W') {
      x = c[i + 1];
      i++;
    }
    while (y != 'R') {
      y = c[j + 1];
      if (y == 'W')
        j--;
    }
    if (i >= j)
      break;
    count++;
    i++;
    j--;
    x = c[i];
    y = c[j];
  }
  cout << count << 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 < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int l = 0;
  int r = n - 1;
  int ans = 0;
  for (;;) {
    while (l < n - 1 && s[l] != 'W')
      l++;
    if (l >= r)
      break;
    while (r > 0 && s[r] != 'R')
      r--;
    if (l >= r)
      break;
    ans++;
    swap(s[l], s[r]);
  }
  cout << ans << endl;
}

int main() {
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int l = 0;
  int r = n - 1;
  int ans = 0;
  for (;;) {
    while (l < n - 1 && s[l] != 'W')
      l++;
    if (l == r)
      break;
    while (r > 0 && s[r] != 'R')
      r--;
    if (l == r)
      break;
    ans++;
    l++, r--;
  }
  cout << ans << endl;
}

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

#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define FOR(i, b, n) for (ll i = b; i < n; ++i)

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  // int 3E4  long 2E9  ll 9E18
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;
  string c;
  cin >> c;

  const char W('W'), R('R');

  ll ans(0);
  ll l(0), r(N - 1);
  while (l < r) {
    while (c[l] != W && l < N - 1)
      l++;
    while (c[r] != R && r > 0)
      r--;
    if (l < r && c[l] == W && c[r] == R) {
      ans++;
      l++;
      r--;
    }
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define FOR(i, b, n) for (ll i = b; i < n; ++i)

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  // int 3E4  long 2E9  ll 9E18
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;
  string c;
  cin >> c;

  const char W('W'), R('R');

  ll ans(0);
  ll l(0), r(N - 1);
  while (l < r) {
    // printf("%lld %lld\\n", l, r);
    while (c[l] != W)
      l++;
    while (c[r] != R)
      r--;
    if (l < r && c[l] == W && c[r] == R) {
      ans++;
      l++;
      r--;
    }
  }
  cout << ans << endl;

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

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

int main() {
  ll n;
  string s;
  cin >> n >> s;
  ll red = 0, white = 0;

  for (ll i = 0; i < n; i++) {
    if (s[i] == 'R')
      red++;
  }

  ll ans = red;
  for (ll i = 0; i < n; i++) {
    if (s[i] == 'R') {
      red--;
    } else {
      white++;
    }
    ll now = max(red, white);
    ans = min(ans, now);
  }

  cout << ans << endl;

  return (0);
}

Error: TLE

Fixed submission:

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

bool finished(string s, ll red) {
  ll num = 0;
  for (ll i = 0; i < red; i++) {
    if (s[i] == 'R')
      num++;
  }

  if (num == red)
    return true;
  return false;
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll ans = 0;
  ll red = 0;
  for (ll i = 0; i < s.size(); i++) {
    if (s[i] == 'R') {
      red++;
    }
  }

  if (finished(s, red)) {
    cout << 0 << endl;
    return (0);
  }

  for (ll i = s.size() - 1; i >= 0; i--) {
    if (s[i] == 'R') {
      for (ll j = 0; j <= i; j++) {
        if (j == i) {
          s[i] = 'R';
          red--;
          ans++;
          break;
        }
        if (s[j] == 'W') {
          s[j] = 'R';
          s[i] = 'W';
          ans++;
          break;
        }
      }
    }
    if (finished(s, red))
      break;
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) ::std::begin(x), ::std::end(x)
using namespace std;

int64_t solve(const int N, string c) {
  char RED = 'R';
  char WHITE = 'W';
  int r = N - 1;
  int count = 0;

  REP(l, N) {
    if (c.at(l) == RED)
      continue;

    while (c.at(r) == WHITE && r > 0)
      --r;

    if (l >= r)
      break;

    c.at(l) = RED;
    c.at(r) = WHITE;
    ++count;
  }

  return count;
}

// generated by online-judge-template-generator v4.4.0
// (https://github.com/kmyk/online-judge-template-generator)
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  constexpr char endl = '\\n';
  int N;
  cin >> N;
  string c;
  cin >> c;
  auto ans = solve(N, c);
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) ::std::begin(x), ::std::end(x)
using namespace std;

int64_t solve(const int N, string c) {
  char RED = 'R';
  char WHITE = 'W';
  int r = N - 1;
  int count = 0;

  REP(l, N) {
    if (c.at(l) == RED)
      continue;

    while (c.at(r) == WHITE && r > -1)
      --r;

    if (l >= r)
      break;

    c.at(l) = RED;
    c.at(r) = WHITE;
    ++count;
  }

  return count;
}

// generated by online-judge-template-generator v4.4.0
// (https://github.com/kmyk/online-judge-template-generator)
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  constexpr char endl = '\\n';
  int N;
  cin >> N;
  string c;
  cin >> c;
  auto ans = solve(N, c);
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define FastIO                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
typedef long int li;
typedef long long ll;
typedef long double ld;

int main() {
  FastIO;
  int n, k = 0;
  string s;
  cin >> n >> s;
  vector<int> w;
  vector<int> r;
  for (int i = 0; i < n; i++)
    if (s[i] == 'W')
      w.push_back(i);
    else
      r.push_back(i);
  sort(w.begin(), w.end());
  sort(r.rbegin(), r.rend());
  if (w.size()) {
    if (w.size() >= r.size()) {
      for (int i = 0; i < r.size(); i++)
        if (w[i] < r[i])
          k++;
    } else {
      for (int i = 0; i < w.size(); i++)
        if (w[i] < r[i])
          k++;
    }
    cout << k << "\\n";
  } else
    cout << "0\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FastIO                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
typedef long int li;
typedef long long ll;
typedef long double ld;

int getLast(string s, char c) {
  int k = -1;
  while (s.find(c, k + 1) != -1) {
    k = s.find(c, k + 1);
  }
  return k;
}

int main() {
  int n, k = 0;
  string s;
  cin >> n >> s;
  while (s.find("WR") != -1) {
    int l = s.find('W');
    int r = getLast(s, 'R');
    swap(s[l], s[r]);
    k++;
  }
  cout << k << "\\n";
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int n;
  cin >> n;
  vector<char> c(n);
  priority_queue<int> que;
  rep(i, n) {
    cin >> c[i];
    if (c[i] == 'R')
      que.push(i);
  }
  ll ans = 0;
  rep(i, n) {
    if (c[i] == 'W') {
      if (que.empty())
        break;
      int num = que.top();
      que.pop();
      if (num > i) {
        swap(c[i], c[que.top()]);
        ++ans;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
ll mod = 1e9 + 7;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int n;
  cin >> n;
  vector<char> c(n);
  rep(i, n) cin >> c[i];
  ll ans = 0;
  rep(i, n) {
    if (c[i] == 'W') {
      int idx = n - 1;
      while (1) {
        if (idx == i)
          break;
        if (c[idx] == 'R') {
          swap(c[i], c[idx]);
          ++ans;
          break;
        }
        --idx;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
int n, cnt, ans;
string s;
int main() {
  scanf("%d", &n);
  cin >> s;
  for (int i = 0; i < n; i++)
    if (s[i] == 'W')
      cnt++;
  for (int i = n - cnt; i < n; i++)
    if (s[i] != 'W')
      ans++;
  printf("%d\\n", ans);
  return 0;
}

Error: WA

Fixed submission:

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

int n, cnt, answer;
string s;

int main() {
  cin >> n;
  getline(cin, s);
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W') {
      cnt++;
    }
  }
  for (int i = n - cnt; i < n; i++) {
    if (s[i] != 'W') {
      answer++;
    }
  }
  cout << answer;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll INF = 1e18;
// const ll MOD = 1e9+7;
// const double pi = 3.14159265358979;
using vl = vector<ll>;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (ll i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (ll i = ((ll)(n)-1); i >= 0; --i)
#define rreps(i, n) for (ll i = ((ll)(n)); i > 0; --i)
#define all(x) (x).begin(), (x).end()

ll n;
ll Main() {
  cin >> n;
  string c;
  ll cnt = 0;
  cin >> c;
  ll ptrL = -1;
  rep(i, n) {
    ptrL = i;
    ll ptrR = n - cnt - 1;
    if (c[i] == 'W') {
      while (ptrR >= 0) {
        if (ptrL >= ptrR) {
          cout << cnt << endl;
          return 0;
        }
        if (c[ptrR] == 'R') {
          c[ptrR] = 'W';
          c[i] = 'R';
          cnt++;
          break;
        } else {
          ptrR--;
        }
      }
    }
  }

  cout << cnt << endl;
  return 0;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  Main();
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll INF = 1e18;
// const ll MOD = 1e9+7;
// const double pi = 3.14159265358979;
using vl = vector<ll>;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (ll i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (ll i = ((ll)(n)-1); i >= 0; --i)
#define rreps(i, n) for (ll i = ((ll)(n)); i > 0; --i)
#define all(x) (x).begin(), (x).end()

ll n;
ll Main() {
  cin >> n;
  string c;
  ll cnt = 0;
  cin >> c;
  ll ptrL = -1;
  rep(i, n) {
    ptrL = i;
    ll ptrR = n - 1;
    if (c[i] == 'W') {
      while (ptrR >= 0) {
        if (ptrL == ptrR) {
          cout << cnt << endl;
          return 0;
        }
        if (c[ptrR] == 'R') {
          c[ptrR] = 'W';
          c[i] = 'R';
          cnt++;
          break;
        } else {
          ptrR--;
        }
      }
    }
  }

  cout << cnt << endl;
  return 0;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  Main();
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using str = string;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
const double PI = 3.141592653589793238;
const ll mod = 1000000007;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define REP(i, m, n) for (int i = m; i < (int)n; i++)
#define all(a) (a).begin(), (a).end()
#define allg(a) (a).begin(), (a).end(), greator<>()
#define d20 std::setprecision(20)
#define veci vector<int>
#define vecll vector<long long int>
#define vecb vector<bool>
#define vecd vector<double>
template <typename T> T lcm(T a, T b) { return a / __gcd(a, b) * b; }

int main() {
  int n, ans = 0;
  str s;
  cin >> n >> s;
  int left = 0, right = s.size() - 1;
  while (left < right && right >= 0 && left < s.size()) {
    if (s[left] == 'W') {
      while (s[right] == 'W' && right >= 0) {
        right--;
      }
      // cout << "left = "<< left << " right = " << right << endl;
      if (left < right) {
        ans++;
      }
      right--;
    }
    left++;
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using str = string;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
const double PI = 3.141592653589793238;
const ll mod = 1000000007;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define REP(i, m, n) for (int i = m; i < (int)n; i++)
#define all(a) (a).begin(), (a).end()
#define allg(a) (a).begin(), (a).end(), greator<>()
#define d20 std::setprecision(20)
#define veci vector<int>
#define vecll vector<long long int>
#define vecb vector<bool>
#define vecd vector<double>
template <typename T> T lcm(T a, T b) { return a / __gcd(a, b) * b; }

int main() {
  int n, ans = 0;
  str s;
  cin >> n >> s;
  size_t left = 0, right = s.size() - 1;
  while (left < right) {
    if (s[left] == 'W') {
      while (s[right] == 'W' && right >= 0) {
        right--;
      }
      // cout << "left = "<< left << " right = " << right << endl;
      if (left < right) {
        ans++;
      }
      right--;
    }
    left++;
  }
  cout << ans << endl;

  return 0;
}