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


#include <iostream>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#include <algorithm>
#include <array>
#include <cinttypes>
#include <cstdio>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <stdio.h>
#include <type_traits>

int main() {
  int N;
  cin >> N;
  std::vector<int> table;
  table.resize(N);

  for (int i = 0; i < N; ++i) {
    char c;
    cin >> c;
    if (c == 'W') {
      table[i] = 1;
    }
  }
  // r = 0
  // w = 1
  int r = 0;
  int last = N - 1;
  for (int i = 0; i < N; ++i) {
    if (table[i] == 0) {
      continue;
    }
    bool b = false;
    for (int j = last; i < j; --j) {
      if (table[j] == 0) {
        table[j] = 1;
        table[i] = 0;
        b = true;
        last = j;
        ++r;
        break;
      }
    }
    if (b) {
      continue;
    }

    break;
  }

  printf("%d\\n", r);
  return 0;
}


Error: TLE

Fixed submission:


#include <iostream>
#include <list>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#include <algorithm>
#include <array>
#include <cinttypes>
#include <cstdio>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <stdio.h>
#include <type_traits>

int main() {
  int N;
  cin >> N;
  std::vector<int> table;
  table.resize(N);

  for (int i = 0; i < N; ++i) {
    char c;
    cin >> c;
    if (c == 'W') {
      table[i] = 1;
    }
  }
  // r = 0
  // w = 1
  int r = 0;
  int last = N - 1;
  for (int i = 0; i < N; ++i) {
    if (table[i] == 0) {
      continue;
    }
    bool b = false;
    for (int j = last; i < j; --j) {
      if (table[j] == 0) {
        table[j] = 1;
        table[i] = 0;
        b = true;
        last = j;
        ++r;
        break;
      }
    }
    if (b) {
      continue;
    }
  }

  printf("%d\\n", r);
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

// D - Alter Altar
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

#define LL long long
#define Lowbit(X) (X & (-X))
#define Lson (X << 1)
#define Rson (X << 1 | 1)
#define Cl(X, Y) memset((X), (Y), sizeof(X))
#define FOR(i, A, B) for (LL i = A; i <= B; i++)
#define BOR(i, A, B) for (LL i = A; i >= B; i--)
#define FOR_SIDE(i, A) for (LL i = Head[A]; i; i = Next[i])
#define INF 0x7fffffff
using namespace std;
const int MAXN = 2e5 + 10;

int Red, White, Loc[MAXN], Total, Ans = INF;
char Opt;

inline void File() {
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
}

int main() {
  // File();
  scanf("%d", &Total);
  Opt = getchar();
  FOR(i, 1, Total) {
    Opt = getchar();
    if (Opt == 'W')
      Loc[i] = 0, White++;
    else
      Loc[i] = 1, Red++;
  }
  int New = 0, Res = 0;
  FOR(i, 1, Total) if (Loc[i] == 1 && i > Red) New++;
  printf("%d\\n", New);
  // fclose(stdin); fclose(stdout);
  // system("pause");
  return 0;
}

Error: 0

Fixed submission:

// D - Alter Altar
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

#define LL long long
#define Lowbit(X) (X & (-X))
#define Lson (X << 1)
#define Rson (X << 1 | 1)
#define Cl(X, Y) memset((X), (Y), sizeof(X))
#define FOR(i, A, B) for (LL i = A; i <= B; i++)
#define BOR(i, A, B) for (LL i = A; i >= B; i--)
#define FOR_SIDE(i, A) for (LL i = Head[A]; i; i = Next[i])
#define INF 0x7fffffff
using namespace std;
const int MAXN = 1e5 + 10;

int Red, White, Loc[MAXN], Total, Ans = INF;
char Opt;

inline void File() {
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
}

int main() {
  // File();
  scanf("%d", &Total);
  Opt = getchar();
  FOR(i, 1, Total) {
    Opt = getchar();
    if (Opt == 'W')
      Loc[i] = 0, White++;
    else
      Loc[i] = 1, Red++;
  }
  int New = 0, Res = 0;
  FOR(i, 1, Total) if (Loc[i] == 1 && i > Red) New++;
  printf("%d\\n", New);
  // fclose(stdin); fclose(stdout);
  // system("pause");
  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 ans = 0; // 入れ替えた回数を定義
  string s;
  cin >> s;                     // 色の並びの入力
  int back = n - 1;             // 後ろからのカウント数
  for (int i = 0; i < n; i++) { // 前から色を調べていく
    if (s[i] == 'W') { // Wの場合、後ろからRを探して交換する
      for (int j = back; j > i; j--) { // 後ろからRがあるかカウントしていく
        if (s[j] == 'R') { // 後ろから数えてRがあった場合
          s[j] = 'W';      // そのRをWに変換する
          s[i] =
              'R'; // 前からのカウントしたWをRに変換する。上と合わせて石を入れ替えている
          back--;  // 後ろからのカウント数を覚えておく
          ans++;   // 入れ替えた数+1
          break;   // ループ抜ける
        } else {   // 後ろから数えてRがなかった場合
          back--;  // 後ろからのカウントだけ進めておく
          if (back == i)
            break; // 後ろからのカウントが数えている一つ右を越えたらループを抜ける
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

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

int main() {
  int64_t n, ans = 0;
  cin >> n;
  int a = 0, b, rv = -1, wv = -1;
  vector<char> vec(n);

  for (int i = 0; i < n; i++) {
    cin >> vec.at(i);
    if (vec.at(i) == 'W')
      a++;
  }

  if (a == 0 || a == n) {
    cout << 0 << endl;
  } else {
    vector<int> rvec(n - a);
    vector<int> wvec(a);

    for (int i = 0; i < n; i++) {
      if (vec.at(i) == 'W') {
        wv++;
        wvec.at(wv) = i;
      } else {
        rv++;
        rvec.at(rv) = i;
      }
    }

    for (int i = 0; i < a; i++) {
      if (rvec.at(n - a - 1 - i) < wvec.at(i)) {
        break;
      } else {
        b = rvec.at(n - a - 1 - i);
        rvec.at(n - a - 1 - i) = wvec.at(i);
        wvec.at(i) = b;
        ans++;
      }
    }

    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int64_t n, ans = 0;
  cin >> n;
  int a = 0, b, rv = -1, wv = -1;
  vector<char> vec(n);

  for (int i = 0; i < n; i++) {
    cin >> vec.at(i);
    if (vec.at(i) == 'W')
      a++;
  }

  if (a == 0 || a == n) {
    cout << 0 << endl;
  } else {
    vector<int> rvec(n - a);
    vector<int> wvec(a);

    for (int i = 0; i < n; i++) {
      if (vec.at(i) == 'W') {
        wv++;
        wvec.at(wv) = i;
      } else {
        rv++;
        rvec.at(rv) = i;
      }
    }

    while (1) {
      if (rvec.at(n - a - 1) < wvec.at(0)) {
        break;
      } else {
        b = rvec.at(n - a - 1);
        rvec.at(n - a - 1) = wvec.at(0);
        wvec.at(0) = b;
        sort(rvec.begin(), rvec.end());
        sort(wvec.begin(), wvec.end());
        ans++;
      }
    }

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

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

void solve() {
  int n;
  string s;
  cin >> n >> s;

  vector<int> r, w;
  rep(i, n) {
    if (s[i] == 'R') {
      r.push_back(i);
    } else {
      w.push_back(i);
    }
  }

  if (r.size() == 0 || r.size() == n) {
    cout << 0 << endl;
    return;
  }

  int ans = 0;
  int ri = r.size() - 1, wi = 0;
  while (ri >= 0 && wi < w.size() && w[wi] < r[ri]) {
    ans++;
    ri--;
    wi++;
  }
  cout << ans << endl;
}

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


Error: 0

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

void solve() {
  int n;
  string s;
  cin >> n >> s;

  vector<int> r, w;
  rep(i, n) {
    if (s[i] == 'R') {
      r.push_back(i);
    } else {
      w.push_back(i);
    }
  }

  int ans = 0;
  int ri = 0, wi = w.size() - 1;
  while (ri < r.size() && wi >= 0 && w[ri] < r[wi]) {
    ans++;
    ri++;
    wi--;
  }
  cout << ans << endl;
}

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

#include <bits/stdc++.h>
#pragma GCC optimize("O3")

#define fi first
#define se second
#define ep emplace
#define eb emplace_back
#define mp make_pair
#define pi pair<int, int>
#define vi vector<int>
#define vpi vector<pair<int, int>>
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
#define endl '\\n'

#define si(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= (ll)(m); ++i)
#define rep3(i, n, m) for (ll i = n; i >= (ll)(m); --i)

template <class T, class U> bool chmax(T &a, U b) {
  if (a < b)
    a = b;
  return true;
}
template <class T, class U> bool chmin(T &a, U b) {
  if (a > b)
    a = b;
  return true;
}

using ll = long long;
using ld = long double;
using namespace std;

constexpr ll MOD = 1000000007;

signed main() {
  cout << fixed << setprecision(20);
  ios::sync_with_stdio(false);
  cin.tie(0);

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

  int r = 0;
  rep(i, n) if (s[i] == 'R')++ r;

  int ans = 0;
  rep(i, r) if (s[i] == 'W')++ ans;

  cout << ans << endl;

  return (0);
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("O3")

#define fi first
#define se second
#define ep emplace
#define eb emplace_back
#define mp make_pair
#define pi pair<int, int>
#define vi vector<int>
#define vpi vector<pair<int, int>>
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
#define endl '\\n'

#define si(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, n, m) for (ll i = n; i <= (ll)(m); ++i)
#define rep3(i, n, m) for (ll i = n; i >= (ll)(m); --i)

template <class T, class U> bool chmax(T &a, U b) {
  if (a < b)
    a = b;
  return true;
}
template <class T, class U> bool chmin(T &a, U b) {
  if (a > b)
    a = b;
  return true;
}

using ll = long long;
using ld = long double;
using namespace std;

constexpr ll MOD = 1000000007;

signed main() {
  cout << fixed << setprecision(20);
  ios::sync_with_stdio(false);
  cin.tie(0);

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

  int r;
  rep(i, n) if (s[i] == 'R')++ r;

  int ans = 0;
  rep(i, r) if (s[i] == '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 REPR(i, n, m) for (int i = (n); i >= (m); --i)
using namespace std;
using LL = long long;
using LD = long double;
using PLL = pair<long long, long long>;
using PLD = pair<long double, long double>;
using VLL = vector<long long>;
using VLD = vector<long double>;
using VPLL = vector<PLL>;
using VPLD = vector<PLD>;

const int INF = numeric_limits<int>::max();
const unsigned long long int ULLINF =
    numeric_limits<unsigned long long int>::max();

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

  LL n;
  cin >> n;
  map<char, LL> mp;
  vector<char> v(n);
  REP(i, n) cin >> v[i], mp[v[i]]++;
  vector<LL> r_index, w_index;
  REP(i, n) {
    if (v[i] == 'W')
      w_index.push_back(i);
    else if (v[i] == 'R')
      r_index.push_back(i);
  }

  if (mp['R'] == 0 || mp['W'] == 0) {
    cout << 0 << endl;
    return 0;
  }

  LL count = 0;
  sort(w_index.begin(), w_index.end());
  sort(r_index.rbegin(), r_index.rend());

  for (LL i = 0; i < min(mp['R'], mp['W']); i++) {
    // cout << w_index[i] << " " << r_index[i] << endl;
    if (w_index[i] < r_index[i])
      count++;
  }

  cout << count << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n, m) for (int i = (n); i >= (m); --i)
using namespace std;
using LL = long long;
using LD = long double;
using PLL = pair<long long, long long>;
using PLD = pair<long double, long double>;
using VLL = vector<long long>;
using VLD = vector<long double>;
using VPLL = vector<PLL>;
using VPLD = vector<PLD>;

const int INF = numeric_limits<int>::max();
const unsigned long long int ULLINF =
    numeric_limits<unsigned long long int>::max();

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

  LL n;
  cin >> n;
  map<char, LL> mp;
  vector<char> v(n);
  REP(i, n) cin >> v[i], mp[v[i]]++;

  vector<LL> r_index(mp['R'], 0), w_index(mp['W'], 0);
  REP(i, n) {
    if (v[i] == 'W')
      w_index.push_back(i);
    else
      r_index.push_back(i);
  }
  if (mp['R'] == 0 || mp['W'] == 0) {
    cout << 0 << endl;
    return 0;
  }

  LL left = 0, right = v.size() - 1;
  LL ans = 0;
  while (left <= right) {
    if (v[left] == 'W' && v[right] == 'R') {
      ans++;
      left++;
      right--;
    } else if (v[left] == 'R' && v[right] == 'R') {
      left++;
    } else if (v[left] == 'W' && v[right] == 'W') {
      right--;
    }
  }

  cout << ans << endl;

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

#include <algorithm>
#include <iostream>
using namespace std;
int main() {

  long long int n;
  cin >> n;
  string str;
  cin >> str;
  long long int j = 0;
  long long int k = n - 1;
  long long int ans = 0;
  while (j < k) {
    while (j < n && str[j] != 'W') {
      j++;
    }
    while (k >= 0 && str[k] != 'R') {
      k--;
    }
    swap(str[j], str[k]);
    if (j < k)
      ans++;
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {

  long long int n;
  cin >> n;
  string str;
  cin >> str;
  long long int j = 0;
  long long int k = n - 1;
  long long int ans = 0;
  while (j < k) {
    while (j < n && str[j] != 'W') {
      j++;
    }
    while (k >= 0 && str[k] != 'R') {
      k--;
    }
    if (j < k)
      ans++;
  }
  cout << ans << endl;

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

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

int main() {
  int N, ans = 0, r = 0;
  std::cin >> N;
  char c;
  std::string C;
  for (int i = 0; i < N; i++) {
    std::cin >> c;
    C.push_back(c);
    if (c == 'R')
      r++;
  }
  for (int i = 0; i < r; i++)
    if (C[i] == 'W')
      ans++;
  std::cout << ans << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N, ans = 0;
  std::cin >> N;
  char c;
  std::string C;
  C.resize(N);
  for (int i = 0; i < N; i++) {
    std::cin >> c;
    C[i] = c;
  }
  size_t C_size = C.size();
  for (int i = 0; i < C_size; i++) {
    if (C[i] == 'W') {
      for (int j = C_size - 1; j > i; j--) {
        if (C[j] == 'R') {
          C.erase(j, 1);
          C.erase(i, 1);
          C_size = C.size();
          i--;
          ans++;
          break;
        }
      }
    }
  }
  std::cout << ans << std::endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define ll long long
#define sz size()
#define ft front
#define pu push
#define ty empty
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
const int m5 = 100042;
int main(void) {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> r;
  vector<int> w;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W') {
      w.pb(i);
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == 'R') {
      r.pb(i);
    }
  }
  int ans = 0;
  int m = min(r.sz, w.sz);
  for (int i = 0; i < m; i++) {
    if (w[i] < r[i]) {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pii pair<int, int>
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define ll long long
#define sz size()
#define ft front
#define pu push
#define ty empty
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
const int m5 = 100042;
int main(void) {
  int len;
  cin >> len;
  string ip;
  cin >> ip;
  int pre = m5 * 10;
  for (int i = 0; i <= len; i++) {
    string cas = "";
    int r = 0, w = 0;
    int ans = 0;
    for (int j = 0; j < i; j++) {
      cas += 'R';
    }
    for (int j = i; j < len; j++) {
      cas += 'W';
    }
    for (int j = 0; j < len; j++) {
      if (cas[j] == ip[j]) {
        continue;
      } else if (cas[j] == 'W') {
        w++;
      } else if (cas[j] == 'R') {
        r++;
      }
    }
    ans = max(w, r);
    pre = min(ans, pre);
  }
  cout << pre << 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)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> w;
  vector<int> r;
  rep(i, n) {
    char c;
    cin >> c;
    if (c == 'R') {
      r.push_back(i);
    } else {
      w.push_back(i);
    }
  }
  if (w.size() == 0 || r.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  int ans = 0;
  rep(i, w.size()) {
    if (r.size() - 1 - i < 0)
      break;
    if (w[i] > r[r.size() - 1 - i])
      break;
    ans++;
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> w;
  vector<int> r;
  rep(i, n) {
    char c;
    cin >> c;
    if (c == 'R') {
      r.push_back(i);
    } else {
      w.push_back(i);
    }
  }
  if (w.size() == 0 || r.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  int min_w = *min_element(w.begin(), w.end());
  int max_r = *max_element(r.begin(), r.end());
  int ans = 0;

  while (max_r > min_w) {
    int min_w_idx = distance(w.begin(), min_element(w.begin(), w.end()));
    int max_r_idx = distance(r.begin(), max_element(r.begin(), r.end()));

    // swap
    int tmp = r[max_r_idx];
    r[max_r_idx] = w[min_w_idx];
    w[min_w_idx] = tmp;

    min_w = *min_element(w.begin(), w.end());
    max_r = *max_element(r.begin(), r.end());
    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 < (int)(n); i++)
using namespace std;
typedef long long ll;

int main(void) {
  ll n;
  cin >> n;
  vector<ll> a(n);
  ll c = 0;

  rep(i, n) {
    char c;
    cin >> c;
    if (c == 'R')
      a[i] = 0;
    else
      a[i] = 1;
  }
  int f = 0;
  ll x = n - 1;
  // if(a[0]==1)f=1;
  rep(i, n) {
    if (f == 1)
      break;
    if (a[i] == 1) {
      for (int j = x; j > i; j--) {
        if (a[j] == 0) {
          a[i] = 0;
          a[j] = 1;
          x = j;
          c++;
          break;
        }
        if (j == i + 1) {
          f = 1;
        }
      }
    }
  }
  cout << c << endl;
}

Error: TLE

Fixed submission:

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

int main(void) {
  ll n;
  cin >> n;
  vector<ll> a(n);
  ll c = 0;

  rep(i, n) {
    char c;
    cin >> c;
    if (c == 'R')
      a[i] = 0;
    else
      a[i] = 1;
  }
  int f = 0;
  // if(a[0]==1)f=1;
  rep(i, n) {
    if (f == 1)
      break;
    if (a[i] == 1) {
      for (int j = n - 1; j > i; j--) {
        if (a[j] == 0) {
          a[i] = 0;
          a[j] = 1;
          c++;
          break;
        }
        if (j == i + 1) {
          f = 1;
        }
      }
    }
  }
  cout << c << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define pal pair<ll, ll>
#define pad pair<double, double>
#define PB push_back
#define MP make_pair
#define F first
#define S second

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double EPS = 1e-8;

long long MODPOW(long long x, long long n) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = ret * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return ret;
}

template <typename T, typename U> bool chmax(T &max, const U &x) {
  if (max < x) {
    max = x;
    return true;
  }
  return false;
}

template <typename T, typename U> bool chmin(T &min, const U &x) {
  if (min > x) {
    min = x;
    return true;
  }
  return false;
}

int main() {
  ll n, ans = 0;
  string s;
  cin >> n >> s;
  ll left = 0;
  for (ll i = n - 1; i >= left; i--) {
    if (s[i] == 'R') {
      for (ll j = left; j < i; j++) {
        left = j + 1;
        if (s[j] == 'W') {
          ans++;
          break;
        }
      }
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define pal pair<ll, ll>
#define pad pair<double, double>
#define PB push_back
#define MP make_pair
#define F first
#define S second

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const double EPS = 1e-8;

long long MODPOW(long long x, long long n) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1)
      ret = ret * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return ret;
}

template <typename T, typename U> bool chmax(T &max, const U &x) {
  if (max < x) {
    max = x;
    return true;
  }
  return false;
}

template <typename T, typename U> bool chmin(T &min, const U &x) {
  if (min > x) {
    min = x;
    return true;
  }
  return false;
}

int main() {
  ll n, ans = 0;
  string s;
  cin >> n >> s;
  ll left = 0;
  for (ll i = n - 1; i >= left; i--) {
    if (s[i] == 'R') {
      for (ll j = left; j < i; j++) {
        if (s[j] == 'W') {
          ans++;
          left = j + 1;
          break;
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  string S;
  cin >> S;
  int res = 0;
  int l = 0, r = (int)S.size() - 1;
  while (l < r) {
    while (l < N && S[l] != 'W') {
      l++;
    }
    while (r >= 0 && S[r] != 'R') {
      r--;
    }
    if (l < r)
      res++;
    l++, r--;
  }
  cout << res << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  string S;
  cin >> S;
  int res = 0;
  int l = 0, r = (int)S.size() - 1;
  while (l < r) {
    while (S[l] != 'W') {
      l++;
    }
    while (S[r] != 'R') {
      r--;
    }
    if (l < r)
      res++;
    l++, r--;
  }
  cout << res << '\\n';
  return 0;
}