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

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, count = 0, min = 200000;
  string s;
  cin >> n;
  vector<int> v;
  char pc = 'F';

  rep(i, n) {
    char c;
    cin >> c;
    if (pc == c || pc == 'F') {
      count++;
    } else {
      if (v.size() > 0 || c == 'R')
        v.push_back(count);
      count = 1;
    }
    pc = c;
  }
  v.push_back(count);
  if (v.size() % 2 == 1)
    v.pop_back();
  if (v.size() == 0) {
    cout << 0;
    return 0;
  }

  rep(i, v.size() / 2 + 1) {
    int wc = 0, rc = 0;
    rep(j, v.size() / 2) {
      if (j < i)
        wc += v[2 * j];
      else
        rc += v[2 * j + 1];
    }
    if (min > max(wc, rc))
      min = max(wc, rc);
  }
  // rep(i,v.size())cout <<v[i];
  cout << min;
}

Error: WA

Fixed submission:

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, count = 0, min = 200000;
  string s;
  cin >> n;
  vector<int> v;
  char pc = 'F';

  rep(i, n) {
    char c;
    cin >> c;
    if (pc == c || pc == 'F') {
      count++;
    } else {
      if (v.size() > 0 || c == 'R')
        v.push_back(count);
      count = 1;
    }
    pc = c;
  }
  v.push_back(count);
  if (v.size() % 2 == 1)
    v.pop_back();
  if (v.size() == 0) {
    cout << 0;
    return 0;
  }

  rep(i, v.size() / 2) {
    int wc = 0, rc = 0;
    rep(j, v.size()) {
      if (j < i * 2)
        wc += v[2 * j];
      else
        rc += v[2 * j + 1];
    }
    if (min > max(wc, rc))
      min = max(wc, rc);
  }
  // rep(i,v.size())cout <<v[i];
  cout << endl << min;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

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

  int left = 0, right = n - 1;
  int cnt = 0;
  while (1) {
    while (s[left] != 'W') {
      left++;
      if (left > n - 1) {
        break;
      }
    }
    while (s[right] != 'R') {
      right--;
      if (right < 0) {
        break;
      }
    }
    if (left >= right) {
      break;
    } else {
      cnt++;
      left++;
      right--;
      if (left > n - 1 || right < 0) {
        break;
      }
    }
  }

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


Error: 0

Fixed submission:

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

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

  int left = 0, right = n - 1;
  int cnt = 0;
  while (1) {
    while (s[left] != 'W') {
      left++;
    }
    while (s[right] != 'R') {
      right--;
    }
    if (left >= right) {
      break;
    } else {
      cnt++;
      left++;
      right--;
      if (left > n - 1 || right < 0) {
        break;
      }
    }
  }

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define INF 2000000007
#define LINF 100000000000000007
#define MOD 1000000007
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n - 1; i >= 0; i--)
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \\
  {}
#define ARDEB(i, X)                                                            \\
  {}
#define END                                                                    \\
  {}
#endif
// typedef long long int ll;
typedef pair<int, int> P;
struct edge {
  int to, cost;
};
int ceil2(int a, int b) {
  if (a % b) {
    return a / b + 1;
  } else {
    return a / b;
  }
}
int n, k, ans;
string s;
signed main() {
  cin >> n >> s;
  int j = n - 1;
  rep(i, n) {
    if (s[i] == 'W') {
      while (j >= 0 && s[j] != 'R') {
        j--;
      }
      if (i >= j)
        break;
      swap(s[i], s[j]);
      j--;
      ans++;
    }
  }
  // cout<<s<<endl;
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define INF 2000000007
#define LINF 100000000000000007
#define MOD 1000000007
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n - 1; i >= 0; i--)
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \\
  {}
#define ARDEB(i, X)                                                            \\
  {}
#define END                                                                    \\
  {}
#endif
// typedef long long int ll;
typedef pair<int, int> P;
struct edge {
  int to, cost;
};
int ceil2(int a, int b) {
  if (a % b) {
    return a / b + 1;
  } else {
    return a / b;
  }
}
int n, k, ans;
string s;
signed main() {
  cin >> n >> s;
  int j = n - 1;
  rep(i, n) {
    if (s[i] == 'W') {
      while (s[j] != 'R') {
        j--;
      }
      if (i >= j)
        break;
      swap(s[i], s[j]);
      j--;
      ans++;
    }
  }
  // cout<<s<<endl;
  cout << ans << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <deque>
using namespace std;
int len, ans;
char ch[200005];
deque<int> v;
int main() {
  scanf("%d%s", &len, ch);
  for (int i = 0; i < len; ++i) {
    if (ch[i] == 'R')
      v.push_back(i);
  }
  for (int i = 0; i < len; ++i) {
    if (v.empty())
      break;
    if (v[0] < i)
      v.pop_front();
    if (v.empty())
      break;
    if (ch[i] == 'W') {
      v.pop_back();
      ++ans;
    }
    if (v.empty())
      break;
  }
  printf("%d\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <deque>
using namespace std;
int len, ans;
char ch[200005];
deque<int> v;
int main() {
  scanf("%d%s", &len, ch);
  for (int i = 0; i < len; ++i) {
    if (ch[i] == 'R')
      v.push_back(i);
  }
  for (int i = 0; i < len; ++i) {
    if (ch[i] == 'W') {
      v.pop_back();
      ++ans;
    }
    if (v.empty())
      break;
    if (v[0] < i)
      v.pop_front();
    if (v.empty())
      break;
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N;
  cin >> N;
  string Iro;
  cin >> Iro;
  int Ans = 0;
  int Red = 0;

  for (int i = 0; i < N; i++) {
    if (Iro.at(i) == 'R') {
      Red++;
    }
  }

  if (Red != 0) {
    Red--;
    for (int i = 0; i <= Red; i++) {
      if (Iro.at(i) == 'W') {
        Ans++;
      }
    }
  }
  cout << Ans;
}

Error: WA

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  string Iro;
  cin >> Iro;
  int Ans = 0;
  int Red = 0;

  for (int i = 0; i < N; i++) {
    if (Iro.at(i) == 'R') {
      Red++;
    }
  }

  if (Red != 0) {
    for (int i = 0; i <= Red; i++) {
      if (Iro.at(i) == 'W') {
        Ans++;
      }
    }
  }
  cout << Ans;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <stack>
using namespace std;

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

  queue<int> white;
  stack<int> red;
  for (int i = 0; i < N; i++) {
    char tmp;
    cin >> tmp;
    if (tmp == 'R') {
      red.push(i);
    } else {
      white.push(i);
    }
  }

  int swaps = 0;
  while (true) {
    if (white.empty() || red.empty()) {
      break;
    }
    if (white.front() < red.top()) {
      swaps++;
      white.pop();
      red.pop();
    } else {
      break;
    }
  }
  cout << swaps << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

// void Swap(int point1,int point2,int c[]){
//     int tmp = c[point1];
//     c[point1] = c[point2];
// }

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

  char c[N];
  for (int i = 0; i < N; i++) {
    cin >> c[i];
  }

  int lastPoint = N, swaps = 0;
  for (int i = 0; i < N; i++) {
    if (c[i] == 'W') {
      for (int end = lastPoint; end > i; end--) {
        if (c[end] == 'R') {
          swaps++;
          lastPoint = end - 1;
          break;
        }
      }
    }
  }

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

// 解説参照1
#include <bits/stdc++.h>
using namespace std;

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

  //  int ans = 0;
  int Wtotal = 0;
  int Rtotal = 0;
  vector<int> white(n);
  for (int i = 0; i < n; i++) {
    if (WorR.at(i) == 'W') {
      Wtotal++;
      white.at(i) = 1; // 1:white 0:red
    } else {
      Rtotal++;
    }
  }

  // 仕切りの左の白の個数W、右の赤の個数Rをともにゼロにする
  int W = 0;
  int R = Rtotal;
  int shikiri = 0;
  int ans = n;
  for (int i = 0; i < n; i++) {
    // 仕切りを1つずつずらしながらW、Rを1ずつ増減させる
    if (white.at(shikiri) == 1 and Rtotal != 0) {
      W++;
    } else {
      R--;
    }
    // W<=R->W回入れ替え後、R-W個の赤を白に変換->合計R回
    // W>R ->R回入れ替え後、W-R個の白を赤に変換->合計W回
    // 仕切り固定の場合、少なくともmax(R,W)の操作が必要?
    // 各仕切りでの回数の最小値を採用
    if (ans >= max(R, W)) {
      ans = max(R, W);
    }
    shikiri++;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

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

  int ans = 0;
  int wcount = 0;
  int rcount = 0;
  vector<int> white(n);
  for (int i = 0; i < n; i++) {
    if (WorR.at(i) == 'W') {
      wcount++;
      white.at(i) = 1; // 1:white 0:red
    } else {
      rcount++;
    }
  }

  int maxCount = min(wcount, rcount);
  int k = n;
  int kk = 0;
  for (int i = kk; i < k; i++) {
    if (white.at(i) == 1) {
      kk = i;
      for (int j = k - 1; j >= i + 1; j--) {
        if (white.at(j) == 0) {
          white.at(i) = 0;
          white.at(j) = 1;
          ans++;
          k = j;
          break;
        }
      }
    }
  }
  cout << min(ans, maxCount) << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e15;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
// __uint128_t
const ll MOD = 1e9 + 7;

int main() {
  ll N;
  string S;
  cin >> N >> S;
  ll cnt = 0;
  for (int i = 0; i < N; i++)
    if (S[i] == 'R')
      cnt++;
  ll cnt2 = 0;
  for (int i = 0; i < cnt; i++)
    if (S[i] == 'R')
      cnt2++;
  cout << cnt - cnt2 << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int N;
  cin >> N;
  string s;
  long long int count = 0;
  for (long long int i = 0; i < N; i++) {
    if (s[i] == 'R') {
      count++;
    }
  }
  long long int finals = 0;
  for (long long int i = 0; i < count; i++) {
    if (s[i] == 'W') {
      finals++;
    }
  }
  cout << finals << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, ans = 0;
  cin >> n;
  string s;
  cin >> s;
  string t = s;
  sort(s.begin(), s.end());
  for (int i = 0; i < n; i++)
    ans += (s[i] != t[i]);
  cout << ans / 2 + ans % 2;
}

Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
bool ok(string s, int n) {
  bool k = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      k = 1;
    if (k && s[i] == 'R')
      return false;
  }
  return true;
}
int main() {
  int n, ans = 0;
  string s;
  cin >> n >> s;
  while (!ok(s, n)) {
    int wh, bk;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'W') {
        wh = i;
        break;
      }
    }
    for (int i = n - 1; i >= 0; i--) {
      if (s[i] == 'R') {
        bk = i;
        break;
      }
    }
    swap(s[wh], s[bk]);
    ans++;
  }
  cout << ans;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define ll long long
#define pii pair<int, int>

void test_case() {
  int n;
  string s;
  cin >> n >> s;
  vector<int> red, white;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      red.pb(i);
    else
      white.pb(i);
  }
  int cnt = 0, i = red.size() - 1, j = 0;
  while (i >= 0 && j < (int)white.size() && (red[i] > white[j])) {
    cnt++;
    int rr = red[i], lw = white[j];
    swap(s[rr], s[lw]);
    i--;
    j++;
  }

  cout << cnt;
}

int main() {
  fastio
  //    int t;
  //    cin>>t;
  //    while(t--)
  test_case();
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb push_back
#define ll long long
#define pii pair<int, int>

void test_case() {
  int n;
  string s;
  cin >> n >> s;
  set<char> red, white;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      red.insert(i);
    else
      white.insert(i);
  }
  int cnt = 0;
  while (!red.empty() && !white.empty() && (*red.rbegin() > *white.begin())) {
    cnt++;

    int rr = *red.rbegin(), lw = *white.begin();
    swap(s[rr], s[lw]);
    auto it = --red.end();
    red.erase(it);
    white.erase(white.begin());
  }

  cout << cnt;
}

int main() {
  fastio
  //    int t;
  //    cin>>t;
  //    while(t--)
  test_case();
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define all(a) (a).begin(), (a).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
const int INF = 1e9;
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;

int main() {
  cin >> N;
  int j = N - 1;
  vector<char> arr(N);
  for (int i = 0; i < N; i++)
    cin >> arr.at(i);

  for (int i = 0; i < N; i++) {
    if (arr[i] == 'W') {
      for (; j >= 0; j--) {

        if (i >= j) {
          cout << C;
          return 0;
        }
        if (arr[j] == 'R') {
          arr[i] = 'R';
          arr[j] = 'W';
          C++;
          break;
        }
      }
    }
    // if(i>=j)
    // {
    //     cout << C;
    //     return 0;
    // }
  }
  cout << 0;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define all(a) (a).begin(), (a).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
const int INF = 1e9;
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;

int main() {
  int j;
  cin >> N;
  vector<char> arr(N);
  for (int i = 0; i < N; i++)
    cin >> arr.at(i);

  for (int i = 0; i < N; i++) {
    if (arr[i] == 'W') {
      for (j = N - 1; j >= 0; j--) {

        if (i >= j) {
          cout << C;
          return 0;
        }
        if (arr[j] == 'R') {
          arr[i] = 'R';
          arr[j] = 'W';
          C++;
          break;
        }
      }
    }
    // if(i>=j)
    // {
    //     cout << C;
    //     return 0;
    // }
  }
  cout << 0;

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<ll>>;
int main() {
  ll A;
  cin >> A;
  vector<ll> vec(0);
  for (ll i = 0; i < A; i++) {
    char a;
    cin >> a;
    if (a == 'R') {
      vec.push_back(i + 1);
    }
  }
  sort(vec.begin(), vec.end());
  ll f = vec.size();
  ll g = 0;
  for (ll i = 0; i < f; i++) {
    if (vec.at(i) > vec.size()) {
      g = i;
      break;
    }
    if (i == f - 1) {
      g = f;
    }
  }
  cout << min(f - g, A - f) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<ll>>;
int main() {
  ll A;
  cin >> A;
  vector<ll> vec(0);
  for (ll i = 0; i < A; i++) {
    char a;
    cin >> a;
    if (a == 'R') {
      vec.push_back(i + 1);
    }
  }
  ll d = 0;
  sort(vec.begin(), vec.end());
  ll f = vec.size();

  for (ll i = 0; i < f; i++) {

    if (vec.at(i) == i + 1)
      continue;
    vec.at(f - 1) = i + 1;
    sort(vec.begin(), vec.end());
    d += 1;
  }
  cout << min(d, A - f) << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int main(void) {
  int N;
  string C;
  cin >> N;
  cin >> C;
  int ans = 0;
  int m = 0, n = N - 1;

  char ch = C[0];
  for (int i = 0; i < N; i++) {
    if (ch != C[i]) {
      break;
    }
    if (i == N - 1) {
      cout << "0" << endl;
      return 0;
    }
  }

  while (m <= n) {
    for (int k = m; k < N - 1; k++) {
      if (C[k] == 'W') {
        m = k;
        break;
      }
    }
    if (m >= n)
      break;
    for (int k = n; k > 0; k--) {
      if (C[k] == 'R') {
        n = k;
        break;
      }
    }
    if (m >= n)
      break;
    if (n != m) {
      ans++;
      m++;
      n--;
      // C[m]='R';
      // C[n]='W';
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

int main(void) {
  int N;
  string C;
  cin >> N;
  cin >> C;
  int ans = 0;
  int m = 0, n = N - 1;

  char ch = C[0];
  for (int i = 0; i < N; i++) {
    if (ch != C[i]) {
      break;
    }
    if (i == N - 1) {
      cout << "0" << endl;
      return 0;
    }
  }

  while (m <= n) {
    for (int k = m; k < N - 1; k++) {
      if (C[k] == 'W') {
        m = k;
        break;
      }
    }
    if (m >= n)
      break;
    for (int k = n; k > 0; k--) {
      if (C[k] == 'R') {
        n = k;
        break;
      }
    }
    if (m >= n)
      break;
    if (n != m) {
      ans++;
      C[m] = 'R';
      C[n] = 'W';
    }
  }
  cout << ans << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double d;
// #define for(i,a,b) for(int i=a;i<b;i++)
// #define f(i,a,b) for(int i=a;i<=b;i++)
int main() {
  int n;
  string s, r;
  int cnt1 = 0, cnt2 = 0;
  cin >> n;
  cin >> s;
  r = s;
  for (int i = 0, j = n - 1; j >= 0 && i <= n - 1; j--) {
    if (s[j] == 'R') {
      while (s[i] != 'W') {
        i++;
        if (i == n)
          break;
      }
      if (i >= j)
        break;
      swap(s[i], s[j]);
      cnt1++;
    }
  }
  s = r;
  for (int i = 0, j = n - 1; j >= 0 && i <= n - 1; i++) {
    if (s[i] == 'W') {
      while (s[j] != 'R') {
        j--;
        if (j == -1)
          break;
      }
      if (i >= j)
        break;
      swap(s[i], s[j]);
      cnt2++;
    }
  }
  cout << min(cnt1, cnt2) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double d;
// #define for(i,a,b) for(int i=a;i<b;i++)
// #define f(i,a,b) for(int i=a;i<=b;i++)
int main() {
  int n;
  string s, r;
  int cnt1 = 0, cnt2 = 0;
  cin >> n;
  cin >> s;
  r = s;
  for (int i = 0, j = n - 1; j >= 0 && i <= n - 1; j--) {
    if (s[j] == 'R') {
      while (s[i] != 'W') {
        i++;
        if (i == n)
          break;
      }
      if (i >= j)
        break;
      swap(s[i], s[j]);
      cnt1++;
    }
  }
  s = r;
  for (int i = 0, j = n - 1; j >= 0 && i <= n - 1; i++) {
    if (s[i] == 'W') {
      while (s[j] != 'R') {
        j--;
        if (i == n)
          break;
      }
      if (i >= j)
        break;
      swap(s[i], s[j]);
      cnt2++;
    }
  }
  cout << min(cnt1, cnt2) << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define int long long

using namespace std;

void solve() {
  int n, r = 0, cnt = 0, c = 0;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      r++;
  }
  if (r == 0) {
    cout << cnt << endl;
    return;
  }
  for (int i = 0; i < r; i++) {
    if (s[i] == 'R')
      c++;
  }

  cout << r - c << endl;
}

int32_t main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

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

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define int long long

using namespace std;

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

int32_t main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

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

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

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

ll mod = 1e9 + 7;

int main() {
  int n, ans = 0;
  string c;
  cin >> n >> c;
  int cntw = 0, cntr = 0;
  int w[n], r[n];
  for (int i = 0; i < n; i++) {
    if (c[i] == 'W')
      cntw++;
    else
      cntr++;
    w[i] = cntw;
    r[i] = cntr;
  }
  for (int i = 0; i < n; i++) {
    if (w[i] == cntr - r[i])
      ans = w[i];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

ll mod = 1e9 + 7;

int main() {
  int n, ans = 0;
  string c;
  cin >> n >> c;
  for (int i = 0; i < n; i++) {
    int cntw = 0, cntr = 0;
    for (int j = 0; j < i; j++) {
      if (c[j] == 'W')
        cntw++;
    }
    for (int j = i; j < n; j++) {
      if (c[j] == 'R')
        cntr++;
    }
    if (cntw == cntr)
      ans = cntw;
  }
  cout << ans << endl;
}