alexjercan/bugnet huggingface dataset:
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;
using vi = vector<int>;
using vvi = vector<vi>;

int main() {
  ll n;
  cin >> n;

  string c;
  cin >> c;

  bool sorted = false;
  int check = n - 1;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (c.at(i) == 'W') {
      for (int j = check; j >= 0; j--) {
        if (j <= i) {
          sorted = true;
          break;
        }
        if (c.at(j) == 'R') {
          c.at(i) = 'R';
          c.at(j) = 'W';
          ans++;
          check = j - 1;
          break;
        }
      }
    }
    if (sorted)
      break;
  }

  cout << ans << endl;
  return 0;
}

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;
using vi = vector<int>;
using vvi = vector<vi>;

int main() {
  ll n;
  cin >> n;

  string c;
  cin >> c;

  bool sorted = false;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (c.at(i) == 'W') {
      for (int j = n - 1; j >= 0; j--) {
        if (j <= i) {
          sorted = true;
          break;
        }
        if (c.at(j) == 'R') {
          c.at(i) = 'R';
          c.at(j) = 'W';
          ans++;
          break;
        }
      }
    }
    if (sorted)
      break;
  }

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

#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  string str;
  int N, i, j, count = 0;
  cin >> N >> str;
  i = 0;
  j = N - 1;
  while (i < j) {
    if (str[i] == 'W' && str[j] == 'R') {
      count += 1;
      i += 1;
      j -= 1;
    } else if (str[i] == 'W' && str[j] == 'W') {
      j -= 1;
    } else if (str[i] == 'R' && str[j] == 'R') {
      i += 1;
    } else {
      i += 1;
      j -= 1;
    }
  }
  cout << count << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {
  string str;
  int N, i, j, count = 0;
  cin >> N >> str;
  i = 0;
  j = N - 1;
  while (i < j) {
    if (str[i] == 'W' && str[j] == 'R') {
      count += 1;
      i += 1;
      j -= 1;
    } else if (str[i] == 'W' && str[j] == 'W') {
      j -= 1;
    } else if (str[i] == 'R' && str[j] == 'R') {
      i += 1;
    } else {
      i -= 1;
      j -= 1;
    }
  }
  cout << count << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 1000000007;
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define REP2(i, x, n) for (ll i = x; i < (n); i++)
const long long INF = numeric_limits<long long>::max();
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ll N;
  cin >> N;
  string stones;
  cin >> stones;

#ifdef SOURCE_TEST
  stones = "";
  for (ll i = 0; i < 200000; ++i) {
    stones += (i % 2) ? "W" : "R";
  }
#endif

  ll count = 0;
  ll wIndex = stones.find('W');
  ll rIndex = stones.find_last_of("R");

  if (wIndex != string::npos && rIndex != string::npos) {
    while (wIndex < rIndex) {
      swap(stones[wIndex], stones[rIndex]);
      ll w = wIndex;
      ll r = rIndex;
      wIndex = stones.find('W', w);
      rIndex = stones.find_last_of("R", r);
      count++;
    }
  }

#ifdef SOURCE_TEST
  cout << stones << endl;
  cout << wIndex << ":" << rIndex << endl;
#endif
  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 1000000007;
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define REP2(i, x, n) for (ll i = x; i < (n); i++)
const long long INF = numeric_limits<long long>::max();
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ll N;
  cin >> N;
  string stones;
  cin >> stones;

  ll count = 0;
  ll wIndex = stones.find('W');
  ll rIndex = stones.find_last_of("R");

  if (wIndex != string::npos && rIndex != string::npos) {
    while (wIndex < rIndex) {
      swap(stones[wIndex], stones[rIndex]);
      wIndex = stones.find('W');
      rIndex = stones.find_last_of("R");
      count++;
    }
  }

#ifdef SOURCE_TEST
  cout << stones << endl;
  cout << wIndex << ":" << rIndex << endl;
#endif
  cout << count << endl;

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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define rep0(i, n) for (i = 0; i < n; i++)
#define rep(s, i, n) for (i = s; i < n; i++)
using namespace std;

char c[200000];

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

  rep0(i, N) { cin >> c[i]; }

  int w = 0, r = 0;
  int count = 0;

  while (true) {
    rep(w, w, N) {
      if (c[w] == 'W')
        break;
    }
    rep(r, r, N) {
      if (c[N - r - 1] == 'R')
        break;
    }
    if (w == N - r)
      break;
    else {
      c[w] = 'R';
      c[N - r - 1] = 'W';
      count++;
    }
  }

  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define rep0(i, n) for (i = 0; i < n; i++)
#define rep(s, i, n) for (i = s; i < n; i++)
using namespace std;

char c[200000];

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

  rep0(i, N) { cin >> c[i]; }

  int w, r;
  int count = 0;

  while (true) {
    rep0(w, N) {
      if (c[w] == 'W')
        break;
    }
    rep0(r, N) {
      if (c[N - r - 1] == 'R')
        break;
    }
    if (w == N - r)
      break;
    else {
      c[w] = 'R';
      c[N - r - 1] = 'W';
      count++;
    }
  }

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

  vector<char> c(N);
  int cnt = 0, r = 0;
  for (int i = 0; i < N; i++) {
    cin >> c.at(i);
    if (c.at(i) == 'R') {
      r++;
    }
  }
  for (int j = 0; j < r; j++) {
    if (c.at(j) == 'W') {
      cnt++;
    }
  }

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


Error: TLE

Fixed submission:

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

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

  string c;
  cin >> c;

  int cnt = 0;
  for (int j = 0; j < (c.size() - cnt); j++) {
    if (c.at(j) == 'W') {
      for (int i = c.size() - (cnt + 1); i > j; i--) {
        if (c.at(i) == 'R') {
          swap(c.at(j), c.at(i));
          cnt++;
          break;
        }
      }
    }
  }

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

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

void solve(int N, string c) {
  int i = 0, j = N - 1;
  int ans = 0;
  // WR をなくす
  // →左から W を探して、右から R を探して、入れ替える
  while (1) {
    while (i < N - 1 && c[i] == 'R') {
      i++;
    }
    if (i == N - 1) {
      break;
    }
    while (j > 0 && c[j] == 'W') {
      j--;
    }
    if (j == 0) {
      break;
    }
    if (i < j) {
      ans++;
      i++;
      j--;
    } else {
      break;
    }
  }
  cout << ans << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  // Failed to predict input format
  int N;
  cin >> N;
  string c;
  cin >> c;
  solve(N, c);
  return 0;
}


Error: 0

Fixed submission:

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

void solve(int N, string c) {
  int i = 0, j = N - 1;
  int ans = 0;
  // WR をなくす
  // →左から W を探して、右から R を探して、入れ替える
  while (1) {
    while (i < N - 1 && c[i] == 'R') {
      i++;
    }
    if (i == N - 1) {
      break;
    }
    if (j > 0 && c[j] == 'W') {
      j--;
    }
    if (j == 0) {
      break;
    }
    if (i < j) {
      assert(c[i] == 'W');
      assert(c[j] == 'R');
      ans++;
      i++;
      j--;
    } else {
      break;
    }
  }
  cout << ans << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips:
// You use the default template now. You can remove this line by using your
// custom template)
int main() {
  // Failed to predict input format
  int N;
  cin >> N;
  string c;
  cin >> c;
  solve(N, c);
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  string C;
  cin >> N >> C;
  int c[N + 1];
  int ans;
  int count_r = 0; // 白石の数
  int count = 0;
  // 1=白石、0=赤石
  for (int i = 1; i <= N; i++) {
    if (C.substr(i - 1, 1) == "R") {
      c[i] = 0;
      count_r += 1;
    } else {
      c[i] = 1;
    }
  }
  for (int i = 1; i <= count_r; i++) {
    if (c[i] == 1) {
      count += 1;
    }
  }

  ans = count;
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int N;
  string C;
  cin >> N >> C;
  int c[N + 1];
  int c2[N + 1];
  int count_w = 0;
  for (int i = 1; i <= N; i++) {
    if (C.substr(i - 1, 1) == "W") {
      c[i] = 0;
      c2[i] = 0;
      count_w = count_w + 1;
    } else {
      c[i] = 1;
      c2[i] = 1;
    }
  }
  int ans;
  int c_x = 0;
  int c_r = 0;

  for (int i = 1; i <= N; i++) {
    if (c[i] == 0) {
      for (int j = N; j > i; j--) {
        if (c[j] == 1) {
          c[i] = 1;
          c[j] = 0;
          c_x = c_x + 1;
          break;
        }
      }
    }
  }
  if (count_w >= c_x) {
    ans = c_x;
  } else {
    int r_last;
    for (int i = N; i >= 1; i--) {
      if (c2[i] == 1) {
        r_last = i;
        break;
      }
    }
    for (int i = 1; i <= r_last - 1; i++) {
      if (c2[i] == 0) {
        c_r = c_r + 1;
      }
    }

    if (c_r >= c_x) {
      ans = c_x;
    } else {
      ans = c_r;
    }
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
/**
 *超时有可能是输入的问题
 *注意边界
 *仔细读题
 *没思路试试逆向思维
 **加油，奥利给！！！
 **/
map<int, int> p;
/*struct knd{
        int x,y,z;
}s[200000];*/
int a[200000];
int main() {
  int m, n, i, j, t, k, w, e;
  cin >> n;
  int s = 0;
  char l[200000];
  for (i = 1; i <= n; i++) {
    cin >> l[i];
    if (l[i] == 'R')
      s++;
  }
  w = s;
  for (i = 1; i <= w; i++) {
    if (l[i] == 'R')
      s--;
  }
  cout << s;
}

Error: WA

Fixed submission:


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
/**
 *超时有可能是输入的问题
 *注意边界
 *仔细读题
 *没思路试试逆向思维
 **加油，奥利给！！！
 **/
map<int, int> p;
/*struct knd{
        int x,y,z;
}s[200000];*/
int a[200000];
int main() {
  int m, n, i, j, t, k, w, e;
  cin >> n;
  int s = 0;
  char l[10000];
  for (i = 1; i <= n; i++) {
    cin >> l[i];
    if (l[i] == 'R')
      s++;
  }
  w = s;
  for (i = 1; i <= w; i++) {
    if (l[i] == 'R')
      s--;
  }
  cout << s;
}
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 REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  string c;
  cin >> c;
  int l = 0, r = c.size() - 1;
  int ans = 0;
  while (true) {
    if (l >= r)
      break;
    if (c[l] == 'W') {
      while (c[r] != 'R' && r != l)
        r--;
      if (c[r] == 'R') {
        c[l] = 'R';
        c[r] = 'W';
        ans++;
      }
    }
    l++;
  }
  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 REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  string c;
  cin >> c;
  int l = 0, r = c.size() - 1;
  int ans = 0;
  while (true) {
    if (l == r)
      break;
    if (c[l] == 'W') {
      while (c[r] != 'R' && r != l)
        r--;
      c[l] = 'R';
      c[r] = 'W';
      ans++;
    }
    l++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int func(int n, int c[], int r_num) {
  int re = 0;
  for (int i = 0; i < r_num; i++) {
    if (c[i] != 1)
      re++;
  }
  return re;
}

int main(void) {
  int n;
  cin >> n;
  int c[n];
  int r_num = 0;
  char color;
  for (int i = 0; i < n; i++) {
    cin >> color;
    if (color == 'R') {
      c[i] = 1;
      r_num++;
    } else if (color == 'W')
      c[i] = 2;
  }
  int ans = func(n, c, r_num);
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int func(int n, int c[], int r_num) {
  int re = 0;
  for (int i = 0; i < r_num; i++) {
    if (c[i] != 1)
      re++;
  }
  return re;
}

int main(void) {
  int n;
  cin >> n;
  int c[n];
  int r_num = 0;
  char color;
  for (int i = 0; i < n; i++) {
    cin >> color;
    if (color == 'R') {
      c[i] = 1;
      r_num++;
    } else if (color == 'W')
      c[i] = 2;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = func(n, c, r_num);
  }
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  // your code goes
  long int n;
  cin >> n;
  string s;
  cin >> s;
  long int l = 0, r = n - 1;
  long int ans = 0;
  while (1) {
    while (r >= 0 && s[r] != 'R')
      r--;
    while (l < n && s[l] != 'W')
      l++;
    if (l < r) {
      ans++;
      swap(s[l], s[r]);
    } else
      break;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  // your code goes
  long int n;
  cin >> n;
  string s;
  cin >> s;
  long int l = 0, r = n - 1;
  long int ans = 0;
  while (1) {
    while (s[r] != 'R')
      r--;
    while (s[l] != 'W')
      l++;
    if (l < r) {
      ans++;
      swap(s[l], s[r]);
    } else
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
using namespace std;
#define MAX 200000
string str;

int main(void) {
  int N, cnt;
  cnt = 0;

  cin >> N;
  cin >> str;

  int i = 0;
  int j = N - 1;
  while (i < j) {
    while (str[i] == 'R')
      i++;
    while (str[j] == 'W')
      j--;

    if (str[i] == 'W' && str[j] == 'R' && i < j)
      cnt++;
    i++;
    j--;
  }

  cout << cnt << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
#define MAX 200000
string str;

int main(void) {
  int N, cnt;
  cnt = 0;

  cin >> N;
  cin >> str;

  int i = 0;
  int j = N - 1;
  while (i < j) {
    while (str[i] = 'R')
      i++;
    while (str[j] = 'W')
      j--;

    if (str[i] == 'W' && str[j] == 'R' && i < j)
      cnt++;
    i++;
    j--;
  }

  cout << cnt << endl;

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

#include <bits/stdc++.h>

using namespace std;

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

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

  int r = 0, w = 0, swap = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'W') {
      ++w;
    } else {
      ++r;
    }
  }
  int ans = min(r, w);

  for (int i = 0; i < r; ++i) {
    if (s[i] == 'W') {
      ++swap;
    }
  }
  ans = min(ans, swap);

  cout << ans << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

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

  int r = 0, w = 0, swap = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'W') {
      ++w;
    } else {
      ++r;
    }
  }
  int ans = min(r, w);

  for (int i = 0; i < n; ++i) {
    if (s[i] == 'W') {
      for (int j = n - 1; j > i; --j) {
        if (s[j] == 'R') {
          s[j] = 'W';
          ++swap;
          break;
        }
      }
    }
  }
  ans = min(ans, swap);

  cout << ans << '\\n';

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

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mt make_tuple
#define MOD 1000000007
#define fo(i, a, b) for (i = a; i < b; i++)
#define foe(i, a, b) for (i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<long long int>
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<long long int, long long int>>
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
using namespace std;
const int inf = 1e9 + 5;
const ll inf64 = 1e18 + 5;

int main() {
  boost;
  int n, i;
  cin >> n;
  string s;
  cin >> s;

  int r = 0, w = 0;
  for (char c : s) {
    if (c == 'R')
      r++;
    else
      w++;
  }

  int totr = 0, totw = 0;
  fo(i, 0, r) {
    if (s[i] == 'R')
      totr++;
  }
  for (i = n - 1; i >= n - w; i--) {
    if (s[i] == 'W')
      totw++;
  }
  cout << min(r - totr, w - totw);
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mt make_tuple
#define MOD 1000000007
#define fo(i, a, b) for (i = a; i < b; i++)
#define foe(i, a, b) for (i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<long long int>
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<long long int, long long int>>
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
using namespace std;
const int inf = 1e9 + 5;
const ll inf64 = 1e18 + 5;

int main() {
  boost;
  int n, i;
  cin >> n;
  string s;
  cin >> s;

  int r = 0, w = 0;
  for (char c : s) {
    if (c == 'R')
      r++;
    else
      w++;
  }

  int totr = 0, totw = 0;
  fo(i, 0, r) {
    if (s[i] == 'R')
      totr++;
  }
  for (i = s.size() - 1; i >= s.size() - w; i--) {
    if (s[i] == 'W')
      totw++;
  }
  cout << min(r - totr, w - totw);
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

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

  string Line = "";
  cin >> Line;

  int l, r;
  l = 0, r = N - 1;
  int cnt = 0;
  bool f = true;
  while (l <= r && f) {
    for (int i = l; i <= r; ++i) {
      if (Line.at(i) == 'W') {
        l = i;
        break;
      }
      if (i == r) {
        l = i;
        f = false;
      }
    }
    for (int i = r; i >= l; --i) {
      if (Line.at(i) == 'R') {
        r = i;
        break;
      }
      if (i == l) {
        r = i;
        f = false;
      }
    }
    if (l < r) {
      cnt++;
      l++;
      r--;
    }
  }

  cout << cnt << endl;
}


Error: 0

Fixed submission:

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

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

  string Line = "";
  cin >> Line;

  int l, r;
  l = 0, r = N - 1;
  int cnt = 0;
  while (l <= r) {
    for (int i = l; i <= r; ++i) {
      if (Line.at(i) == 'W') {
        l = i;
        break;
      }
      if (i == r) {
        l = r + 1;
      }
    }
    for (int i = r; i >= l; --i) {
      if (Line.at(i) == 'R') {
        r = i;
        break;
      }
      if (i == l) {
        l = l - 1;
      }
    }
    if (l < r) {
      cnt++;
      l++;
      r--;
    } else if (l == r) {
      cnt++;
      break;
    }
  }

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

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

#define ll long long

const int N = 1e6 + 4;

char c[N];

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  ll int n;

  cin >> n;

  int cnt = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> c[i];
    if (c[i] == 'W')
      cnt++;
  }

  int ans = cnt;
  //	cout << "current ans: " << ans << "\\n";
  //	cout << "\\n";
  for (int i = n; i > n - cnt; --i) {
    if (c[i] == 'W')
      ans--;
    //		cout << "every time white is found : " << ans << "\\n";
  }

  cout << ans << "\\n";

  return 0;
}


Error: 0

Fixed submission:

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

#define ll long long

ll int N = 1e5 + 4;

// void swap()

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  ll int k;

  cin >> k;

  char w = 'w';
  char r = 'r';

  ll int arr[N];
  ll int cnt = 0;
  for (int i = 0; i < k; ++i) {
    char alp;
    cin >> alp;

    arr[i] = alp;
  }

  for (int i = 0; i < k; ++i) {
    if (arr[i] != arr[i + 1]) {

      cnt++;
    }
  }
  cout << cnt << "\\n";

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

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

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

  priority_queue<int> red;
  priority_queue<int, vector<int>, greater<int>> white;
  for (int i = 0; i < N; i++) {
    if (S[i] == 'R') {
      red.push(i);
    } else {
      white.push(i);
    }
  }

  int min_index_white = N;
  int max_index_red = -1;
  int count = 0;
  while (!white.empty()) {

    if (red.empty()) {
      break;
    }

    min_index_white = white.top();
    max_index_red = red.top();

    if (min_index_white > max_index_red) {
      break;
    }

    white.pop();
    red.pop();
    count++;
  }

  cout << count << endl;
  return 0;
}

Error: 0

Fixed submission:

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

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

  priority_queue<int> red;
  priority_queue<int, vector<int>, greater<int>> white;
  for (int i = 0; i < N; i++) {
    if (S[i] == 'R') {
      red.push(i);
    } else {
      white.push(i);
    }
  }

  int min_index_white = N;
  int max_index_red = -1;
  int count = 0;
  while (!white.empty()) {
    min_index_white = white.top();
    max_index_red = red.top();

    if (min_index_white > max_index_red) {
      break;
    }

    white.pop();
    red.pop();
    count++;
  }

  cout << count << endl;
  return 0;
}