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

#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
// using namespace boost::multiprecision;
typedef long long int ll;
typedef long double ld;
#define PI 3.141592653589793
#define MOD 1000000007
#define ALL(obj) (obj).begin(), (obj).end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 56;
bool pairCompare(const pair<double, ll> &firstElof,
                 const pair<double, ll> &secondElof) {
  return firstElof.first < secondElof.first;
}
bool pairCompareSecond(const pair<double, ll> &firstElof,
                       const pair<double, ll> &secondElof) {
  return firstElof.second < secondElof.second;
}
// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

struct edge { // グラフに使うヤツ
  ll from, to, cost;
};

typedef vector<vector<edge>> G;
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
using namespace std;
// using namespace boost::multiprecision;
typedef long long int ll;
typedef long double ld;
#define PI 3.141592653589793
#define MOD 1000000007
#define ALL(obj) (obj).begin(), (obj).end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 56;
bool pairCompare(const pair<double, ll> &firstElof,
                 const pair<double, ll> &secondElof) {
  return firstElof.first < secondElof.first;
}
bool pairCompareSecond(const pair<double, ll> &firstElof,
                       const pair<double, ll> &secondElof) {
  return firstElof.second < secondElof.second;
}
// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

struct edge { // グラフに使うヤツ
  ll from, to, cost;
};

typedef vector<vector<edge>> G;
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return (b);
  else
    return (gcd(b, a % b));
}

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

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, i, ans = 0, it_l, it_r;
  cin >> n;
  vector<char> c(n);
  for (i = 0; i < n; ++i)
    cin >> c.at(i);
  it_l = 0;
  it_r = n - 1;
  while (it_l < it_r) {
    if (c.at(it_l) == 'W') {
      while (c.at(it_r) != 'R') {
        --it_r;
        if (it_l == it_r)
          break;
      }
      if (it_l < it_r) {
        ++ans;
        --it_r;
      }
    }
    ++it_l;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, i, ans = 0, it_l, it_r;
  cin >> n;
  vector<char> c(n);
  for (i = 0; i < n; ++i)
    cin >> c.at(i);
  it_l = 0;
  it_r = n - 1;
  while (it_l < it_r) {
    if (c.at(it_l) == 'W') {
      while (c.at(it_r) != 'R')
        --it_r;
      if (it_l < it_r) {
        ++ans;
        --it_r;
      }
    }
    ++it_l;
  }
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  /* Input */
  int N;
  cin >> N;

  string s;
  cin >> s;

  /* Main Logic */
  int trial = 0;
  int r2wCnt = 0, w2rCnt = 0;

  /* とりあえず全部Wの文字列と比較 */
  for (int i = 0; i < N; i++) {
    if (s[i] == 'R')
      r2wCnt++;
  }
  trial = r2wCnt;

  /* 左からWをRに変化 */
  for (int i = 0; i < N; i++) {
    if (s[i] == 'R')
      r2wCnt--;
    else
      w2rCnt++;
    trial = min(trial, max(r2wCnt, w2rCnt));
  }

  /* Output */
  cout << trial << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {

  /* Input */
  int N;
  cin >> N;

  string initialRow;
  cin >> initialRow;

  /* Main Logic */
  int trial = 0;

  string finalRow = "";
  /* 全部に白石をセット */
  for (int i = 0; i < N; i++) {
    finalRow = finalRow + "W";
  }

  /* 仕切りを動かす */
  for (int i = 0; i < N; i++) {
    finalRow.replace(i, 1, "R");
    /* finalRowとinitialRowを一文字ずつ比較*/
    int r2wCnt = 0, w2rCnt = 0;
    for (int i = 0; i < N; i++) {
      if (initialRow[i] == 'R' && finalRow[i] == 'W')
        r2wCnt++;
      if (initialRow[i] == 'W' && finalRow[i] == 'R')
        w2rCnt++;
    }

    if (i == 0) {
      trial = max(r2wCnt, w2rCnt);
    } else {
      if (trial > max(r2wCnt, w2rCnt))
        trial = max(r2wCnt, w2rCnt);
    }

    // cout << i << ": " << max(r2wCnt, w2rCnt) << endl;
    // cout << initialRow << " -> " << finalRow << endl;
    // cout << "----------" << endl;
  }

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

#ifdef _MSC_VER
#include <__msvc_all_public_headers.hpp>
#undef min
#undef max
#else
#define NDEBUG
#include <bits/stdc++.h>
#endif

using namespace std;
using large = long long;

int main() {
  int n;
  string s;
  cin >> n >> s;

  auto x1 = (int)count(s.begin(), s.end(), 'R');
  auto x2 = (int)count(s.begin(), s.end(), 'W');

  auto i = 0;
  auto j = n - 1;
  while (i < n && s[i] == 'R') // skip left 'R'
    ++i;
  while (j >= 0 && s[j] == 'W') // skip right 'W'
    --j;

  auto x3 = 0;
  while (i < j) {
    x3++;
    ++i, --j;
    while (i < n && s[i] == 'R')
      ++i;
    while (j >= 0 && s[j] == 'W')
      --j;
  }

  cout << min({x1, x2, x3}) << endl;
  return 0;
}

Error: 0

Fixed submission:

#ifdef _MSC_VER
#include <__msvc_all_public_headers.hpp>
#undef min
#undef max
#else
#define NDEBUG
#include <bits/stdc++.h>
#endif

using namespace std;
using large = long long;

int main() {
  int n;
  string s;
  cin >> n >> s;

  auto pos = s.find('W'); // skip 'R' on the left
  s = s.substr(pos);
  pos = s.find_last_of("R"); // skip 'W' on the right

  auto x1 = (int)count(s.begin(), s.end(), 'R');
  auto x2 = (int)count(s.begin(), s.end(), 'W');

  auto i = 0;
  auto j = int(s.length()) - 1;
  auto x3 = 0;
  while (i < j) {
    x3++;
    pos = s.find('W', i + 1);
    if (pos == s.npos)
      break;
    i = pos;
    pos = s.find_last_of('R', j - 1);
    if (pos == s.npos)
      break;
    j = pos;
  }

  cout << min({x1, x2, x3}) << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long LL;
char str[200005];
int main() {
  int n;
  cin >> n >> str;
  int fsr = n - 1;
  int fsw = 0;
  int cnt = 0;
  while (fsr > fsw && fsr >= 0 && fsw < n) {
    while (fsr > 0 && str[fsr] != 'R')
      fsr--;
    while (fsw < n && str[fsw] != 'W')
      fsw++;
    // cout << fsr << ' ' << fsw << endl;
    if (fsr > fsw && fsr >= 0 && fsw < n) {
      swap(str[fsr], str[fsw]);
      cnt++;
    }
  }
  cout << cnt;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
typedef long long LL;
char str[200005];
int main() {
  int n;
  cin >> n >> str;
  int fsr = n - 1;
  int fsw = 0;
  int cnt = 0;
  while (fsr > fsw && fsr >= 0 && fsw < n) {
    while (str[fsr] != 'R')
      fsr--;
    while (str[fsw] != 'W')
      fsw++;
    // cout << fsr << ' ' << fsw << endl;
    if (fsr > fsw && fsr >= 0 && fsw < n) {
      swap(str[fsr], str[fsw]);
      cnt++;
    }
  }
  cout << cnt;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <unordered_set>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
using P = pair<ll, ll>;
using V = vector<ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rep3(i, m, n) for (ll i = m; i <= (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define MOD 1000000007
#define INF 1000000000
#define LLINF 1e18

void solve_abc_d() {
  ll n, m;
  // ll a, b;
  ll x, y, z;
  // ll h, w;
  ll k;
  string s, rs;
  ll l_ans = 0;

  cin >> n;
  cin >> s;

  rs = s;
  reverse(ALL(rs));

  V r, w;
  rep(i, n) {
    if (s[i] == 'W') {
      w.push_back(i);
    }
  }
  rep(i, n) {
    if (rs[i] == 'R') {
      r.push_back(i);
    }
  }

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

  auto itr_w = w.begin();
  auto itr_r = r.begin();
  rep(i, n) {
    if (s[i] == 'W') {
      if (*itr_w > n - 1 - *itr_r) {
        break;
      }
      itr_w++;
      itr_r++;
      l_ans++;
    }
  }

  cout << l_ans << "\\n";

  return;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed;
  cout << setprecision(15);

  solve_abc_d();

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_set>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
using P = pair<ll, ll>;
using V = vector<ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rep3(i, m, n) for (ll i = m; i <= (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define MOD 1000000007
#define INF 1000000000
#define LLINF 1e18

void solve_abc_d() {
  ll n, m;
  // ll a, b;
  ll x, y, z;
  // ll h, w;
  ll k;
  string s, rs;
  ll l_ans = 0;

  cin >> n;
  cin >> s;

  rs = s;
  reverse(ALL(rs));

  V r, w;
  rep(i, n) {
    if (s[i] == 'W') {
      w.push_back(i);
    }
  }
  rep(i, n) {
    if (rs[i] == 'R') {
      r.push_back(i);
    }
  }

  if (n == 1) {
    cout << 0 << "\\n";
    return;
  }

  auto itr_w = w.begin();
  auto itr_r = r.begin();
  rep(i, n) {
    if (s[i] == 'W') {
      if (*itr_w > n - 1 - *itr_r) {
        break;
      }
      itr_w++;
      itr_r++;
      l_ans++;
    }
  }

  cout << l_ans << "\\n";

  return;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed;
  cout << setprecision(15);

  solve_abc_d();

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

#include <iostream>
#include <string>
using namespace std;
int main(void) {
  long long int n, i, ans = 0, j, temp;
  string stone;
  cin >> n;
  cin >> stone;
  temp = n - 1;
  i = 0;
  j = n - 1;
  for (i = 0; i < temp; i++) {
    // cout << 'i' << i << '\\n';
    if (stone[i] == 'W') {
      for (j = temp; j >= 0; j--) {
        // cout << 'j' << j << '\\n';
        if (stone[j] == 'R') {
          if (i < j) {
            ans++;
            temp = j - 1;
          }
          break;
        }
      }
    }
    if (i > j) {
      break;
    }
    // cout << i << temp << '\\n';
  }
  cout << ans << '\\n';
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main(void) {
  long long int n, i, ans = 0, j, temp;
  string stone;
  cin >> n;
  cin >> stone;
  temp = n - 1;
  for (i = 0; i < temp; i++) {
    // cout << 'i' << i << '\\n';
    if (stone[i] == 'W') {
      for (j = temp;; j--) {
        // cout << 'j' << j << '\\n';
        if (stone[j] == 'R') {
          ans++;
          temp = j - 1;
          break;
        }
      }
    }
    if (i > j) {
      break;
    }
    // cout << i << temp << '\\n';
  }
  cout << ans << '\\n';
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define endl "\\n"
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  //    cin.ignore() ;
  string str;
  cin >> str;
  vector<int> w;
  vector<int> r;
  for (int i = 0; i < n; i++) {
    if (str[i] == 'W')
      w.push_back(i);
    else
      r.push_back(i);
  }
  int wInd = 0, rInd = r.size() - 1;
  int cnt = 0;
  while ((wInd < w.size()) && (rInd >= 0) && (w[wInd] < r[rInd])) {
    cnt++;
    wInd++;
    rInd--;
  }
  cout << cnt << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define endl "\\n"
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  cin.ignore();
  string str;
  cin >> str;
  vector<int> w;
  vector<int> r;
  for (int i = 0; i < str.size(); i++) {
    if (str[i] == 'W')
      w.push_back(i);
    else
      r.push_back(i);
  }
  int wInd = 0, rInd = r.size();
  int cnt = 0;
  while ((wInd < w.size()) && (rInd >= 0) && (w[wInd] < r[rInd])) {
    cnt++;
    wInd++;
    rInd--;
  }
  cout << cnt << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  string s;
  int n;
  cin >> n >> s;
  int red = 0;
  for (int i = 0; i < n; i++) {
    if (s.at(i) == 'R') {
      red++;
    }
  }
  int ans = 0;
  for (int i = 0; i < red; i++) {
    if (s.at(i) == 'W') {
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  string s;
  int n;
  cin >> n >> s;
  int right = -1, left = n, ans = 0;
  while (true) {
    for (int i = 0; i < n; i++) {
      if (s.at(i) == 'R') {
        right = i;
      }
    }
    for (int i = 0; i < n; i++) {
      if (s.at(n - 1 - i) == 'W') {
        left = n - 1 - i;
      }
    }
    if (right < left) {
      cout << ans << endl;
      return 0;
    }
    swap(s.at(right), s.at(left));
    ans++;
  }
}
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++)

int main() {
  int n;
  cin >> n;
  string str;
  cin >> str;

  int ans = 0;
  int rI = 0;
  for (int i = str.size() - 1; i > 0; i--) {
    char temp = str.at(i);
    if (temp == 'R') {
      for (int j = rI; j < i; j++) {
        if (str.at(j) == 'W') {
          str.at(i) = 'W';
          str.at(j) = 'R';
          ans++;
          break;
        } else {
          rI = j;
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  long long n;
  cin >> n;
  string str;
  cin >> str;

  long long ans = 0;
  for (int i = str.size() - 1; i > 0; i--) {
    char temp = str.at(i);
    if (temp == 'R') {
      for (int j = ans; j < i; j++) {
        if (str.at(j) == 'W') {
          str.at(i) = 'W';
          str.at(j) = 'R';
          ans++;
          break;
        }
      }
    }
  }
  cout << ans << endl;

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

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long N;
  cin >> N;
  std::string S;
  cin >> S;

  long long ans = 0;
  long long J = 0;
  for (long long i = 0; i < N; ++i) {
    if (S[i] == 'W') {
      for (long long j = J; j < N; ++j) {
        if (i + j >= N) {
          J = j;
          break;
        }
        if (S[N - 1 - j] == 'R') {
          ans += 1;
          J = j + 1;
          break;
        }
      }

      if (i + J + 1 > N) {
        break;
      }
    }
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  long long N;
  cin >> N;
  std::string S;
  cin >> S;

  long long ans = 0;
  long long J = 0;
  for (long long i = 0; i < N; ++i) {
    if (S[i] == 'W') {
      for (long long j = J; j < N; ++j) {
        if (i + j >= N) {
          break;
        }
        if (S[N - 1 - j] == 'R') {
          ans += 1;
          J = j + 1;
          break;
        }
      }

      if (i + J + 1 > N) {
        break;
      }
    }
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>

using namespace std;

int main() {

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

  set<int> r, w;

  for (int i = 0; i < n; ++i) {
    if (s[i] == 'R')
      r.insert(i);
    else
      w.insert(i);
  }

  int ans = 0;
  while (w.size() > 0 and r.size() > 0 and *w.begin() < *r.rbegin()) {
    ans++;
    int x = *w.begin();
    int y = *r.rbegin();
    w.erase(x);
    r.erase(y);
    w.insert(y);
    r.insert(x);
  }

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {

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

  set<int> r, w;

  for (int i = 0; i < n; ++i) {
    if (s[i] == 'R')
      r.insert(i);
    else
      w.insert(i);
  }

  int ans = 0;
  while (*w.begin() < *r.rbegin() and w.size() and r.size()) {
    ans++;
    int x = *w.begin();
    int y = *r.rbegin();
    w.erase(x);
    r.erase(y);
    w.insert(y);
    r.insert(x);
  }
  cout << ans << "\\n";
  return 0;
}
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 pii = pair<int, int>;
ll MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int count_R = 0;
  int tmp_W = 0;
  rep(i, n) if (s[i] == 'R') count_R++;
  rep(i, count_R) {
    if (s[i] == 'W')
      tmp_W++;
  }
  cout << tmp_W;
}

Error: WA

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 pii = pair<int, int>;
ll MOD = 1000000007;

int main() {
  int n;
  cin >> n;
  int count_W = 0;
  int count_R = 0;
  string C;
  rep(i, n) {
    cin >> C[i];
    if (C[i] == 'W')
      count_W++;
    else
      count_R++;
  }
  // cout << "R" << count_R << endl;
  // cout << "W" << count_W << endl;

  int tmp_R = 0;
  int tmp_W = 0;
  for (int i = n - 1; i > 0; i--) {
    if (C[i] == 'W')
      tmp_W++;
    else
      tmp_R++;
    if (tmp_W == count_W - 1)
      break;
    if (tmp_R == count_R)
      break;
  }

  int ans = min(count_R, min(count_W, tmp_R));
  cout << ans;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int change(string col, int i, int j) {
  if (i >= j) {
    return 0;
  }
  if (col[i] == 'W') {
    if (col[j] == 'R') {
      return 1 + change(col, i + 1, j - 1);
    } else {
      return change(col, i, j - 1);
    }
  } else {
    if (col[j] == 'R') {
      return change(col, i + 1, j);
    } else {
      return change(col, i + 1, j - 1);
    }
  }
}

int main() {
  int n, red = 0, whi = 0;
  int col[200000] = {};
  cin >> n;
  for (int i = 0; i < n; i++) {
    char tmp = 'A';
    while (tmp != 'W' && tmp != 'R') {
      scanf("%c", &tmp);
    }
    if (tmp == 'W') {
      col[i] = 1;
      whi++;
    }
  }
  int ans = 0;
  red = n - whi;
  if (red > whi) {
    for (int i = 0; i < whi; i++) {
      if (col[n - i - 1] == 0) {
        ans++;
      }
    }
  } else {
    for (int i = 0; i < red; i++) {
      if (col[i] == 1) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int change(string col, int i, int j) {
  if (i >= j) {
    return 0;
  }
  if (col[i] == 'W') {
    if (col[j] == 'R') {
      return 1 + change(col, i + 1, j - 1);
    } else {
      return change(col, i, j - 1);
    }
  } else {
    if (col[j] == 'R') {
      return change(col, i + 1, j);
    } else {
      return change(col, i + 1, j - 1);
    }
  }
}

int main() {
  int n;
  string col;
  cin >> n;
  cin >> col;
  cout << change(col, 0, col.size() - 1) << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

// g++ -std=c++17 -Wshadow -Wall -g -fsanitize=address -fsanitize=undefined
// -D_GLIBCXX_DEBUG -o

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      v.pb(i);
  }
  int ans = 0;
  int i = 0;
  while (i < n - 1) {
    if (s[i] == 'W') {
      if (!v.empty() && v.back() > i) {
        swap(s[i], s[v.back()]);
        v.pop_back();
        ans++;
      } else if (s[i + 1] == 'R') {
        s[i] = 'R';
        ans++;
      }
    }
    i++;
  }
  cout << ans << "\\n";
  return 0;
}


Error: 0

Fixed submission:

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

// g++ -std=c++17 -Wshadow -Wall -g -fsanitize=address -fsanitize=undefined
// -D_GLIBCXX_DEBUG -o

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      v.pb(i);
  }
  int ans = 0;
  int i = 0;
  while (i < n - 1) {
    if (s[i] == 'W') {
      if (v.back() > i) {
        swap(s[i], s[v.back()]);
        v.pop_back();
        ans++;
      } else if (s[i + 1] == 'R') {
        s[i] = 'R';
        ans++;
      }
    }
    i++;
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

int main() {
  bool swapFlag = false;
  int N, redNum = 0, sum = 0, ans = 0;
  cin >> N;
  string c;
  cin >> c;
  for (int i = 0; i < N; i++) {
    if (c[i] == 'R') {
      redNum++;
    }
  }
  for (int i = 0; i < redNum; i++) {
    if (c[i] == 'R') {
      sum++;
    }
  }
  ans = redNum - sum;
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  bool swapFlag = false;
  int N, redNum = 0, whiteNum = 0, insertNum, ans = 0;
  cin >> N;
  string c;
  cin >> c;
  for (int i = 0; i < N; i++) {
    if (c[i] == 'R') {
      redNum++;
    }
  }
  for (int i = N - 1; i >= 0; i--) {
    if (c[i] == 'R' && redNum <= i) {
      for (int j = 0; j < N; j++) {
        if (c[j] != 'R') {
          c[i] = 'W';
          c[j] = 'R';
          ans++;
          swapFlag = true;
          break;
        }
      }
      if (!swapFlag)
        ans++;
    }
    if (ans >= redNum)
      break;
    swapFlag = false;
  }
  cout << ans << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  vector<int> w, r;
  int n, counter = 0, i1 = 0, j1 = 0;
  string s;
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W') {
      w.push_back(i);
    } else {
      r.push_back(i);
    }
  }
  reverse(r.begin(), r.end());
  int rInd = 0, wInd = 0;
  while (wInd < w.size() && rInd < r.size() && w[wInd] < r[rInd]) {
    wInd++;
    rInd++;
    counter++;
  }
  cout << counter;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  int n, counter = 0;
  string s;
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    for (int j = n - 1; j > i; j--) {
      if ((s[j] == 'R') && (s[i] == 'W')) {
        swap(s[i], s[j]);
        counter++;
        break;
      }
    }
  }
  cout << counter;
}