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

//
//  main.cpp
//  codeforces 1
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define int long long
#define N 1500002
using namespace std;
signed main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int r = 0, w = 0;
  char t[n];
  for (int i = 0; i < n; i++) {
    t[i] = s[i];
    if (s[i] == 'R') {
      r++;
    } else {
      w++;
    }
  }
  int j = n - 1, C1 = 0;
  while (t[j] != 'R' && j >= 0) {
    C1++;
    j--;
  }
  j = 0;
  int C2 = 0;
  while (t[j] != 'W' && j <= n - 1) {
    C2++;
    j++;
  }
  r = r - C2;
  w = w - C1;
  // cout<<r<<" "<<w<<endl;
  char arr[n - C1 - C2];
  if (n - C1 - C2 == 0) {
    cout << 0 << endl;
    return 0;
  }
  for (int i = C2; i < n - C1; i++) {
    arr[i - C2] = s[i];
    // cout<<arr[i-C2]<<" ";
  }
  if (n - C1 - C2 == 0) {
    cout << 0 << endl;
  } else {
    n = n - C1 - C2;
    int count = 0;
    for (int i = r; i < n; i++) {
      if (arr[i] == 'R') {
        count++;
      }
    }
    cout << count << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

//
//  main.cpp
//  codeforces 1
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
#define int long long
#define N 1500002
using namespace std;
signed main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int r = 0, w = 0, p1 = 0, p21 = -1, p22 = -1;
  char t[n];
  for (int i = 0; i < n; i++) {
    t[i] = s[i];
    if (s[i] == 'R') {
      r++;
    } else {
      w++;
    }
  }
  int j = n - 1, C1 = 0;
  while (t[j] != 'R') {
    C1++;
    j--;
  }
  j = 0;
  int C2 = 0;
  while (t[j] != 'R') {
    C2++;
    j++;
  }
  // cout<<C1<<" "<<C2<<endl;
  r = r - C2;
  w = w - C1;
  p1 = min(r, w);
  int k = 0;
  if (r >= w) {
    for (int i = C2; i < n - C1; i++) {
      if (s[i] == 'R') {
        p21 = p21 + i - k;
        k++;
      }
    }
  }
  if (r <= w) {
    k = 0;
    reverse(s.begin(), s.end());
    for (int i = C1; i < n - C2; i++) {
      if (s[i] == 'W') {
        p22 = p22 + i - k;
        k++;
      }
    }
  }
  // cout<<p1<<" "<<p21<<" "<<p22<<endl;
  if (p21 == -1) {
    cout << min(p1, p22) << endl;
  } else if (p22 == -1) {
    cout << min(p1, p21) << endl;
  } else {
    int p2 = min(p21, p22);
    cout << min(p1, p2) << endl;
  }

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

#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int c = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      c++;
  }
  int w = 0;
  for (int i = 0; i < c; i++) {
    if (s[i] != 'R')
      w++;
  }
  int r = 0;
  for (int i = c; i < n; i++) {
    if (s[i] != 'W')
      r++;
  }
  cout << ((w > r) ? w : r) << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  string a;
  cin >> a;
  int c = 0, d = 0;
  int j = 0;
  while (j < n) {
    int l = 0;
    int r = n - 1;
    while (l < r) {
      if ((a.at(l) == 'W') && (a.at(l + 1) == 'R')) {
        a.at(l) = 'R';
        c++;
        l++;
      }
      l++;
      if (l + 1 < r && (a.at(r - 1) == 'W') && (a.at(r) == 'R')) {
        a.at(r) = 'W';
        d++;
        r--;
      }
      r--;
    }
    j++;
  }
  cout << ((c > d) ? c : d) << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int i = 0, j = s.size() - 1;
  int count = 0;
  while (i < n && j >= 0 && i < j) {
    while (i < n && s[i] == 'R')
      i++;
    while (j >= 0 && s[j] == 'W')
      j--;
    if (i < n && j >= 0 && i < j) {
      swap(s[i], s[j]);
      count++;
    }
  }
  cout << count << endl;
}


Error: TLE

Fixed submission:

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

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


#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define endl "\\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;

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

  ll right = 0, left = 0, swap = 0;
  for (ll i = 0; i < n; ++i) {
    if (s[i] == 'W') {
      ++left;
    } else {
      ++right;
    }
  }
  ll ans = min(right, left);

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

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

int main() {
  std::ios::sync_with_stdio(false);
  solve();
  return 0;
}


Error: TLE

Fixed submission:


#include <bits/stdc++.h>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#define endl "\\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(), c.end()
#define mp(x, y) make_pair(x, y)
#define mem(a, val) memset(a, val, sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;

void solve() {
  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';
}

int main() {
  std::ios::sync_with_stdio(false);
  solve();
  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> S(N);
  int r = 0;
  for (int i = 0; i < N; i++) {
    cin >> S.at(i);
    if (S.at(i) == 'R') {
      r++;
    }
  }
  int ans = 0;
  for (int i = 0; i < r; i++) {
    if (S.at(i) == 'W') {
      ans++;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n - 1); i >= 0; --i)

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

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;

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

  int l = 0, r = N - 1;
  int ans = 0;
  while (l < r) {
    while (S[l] == 'R')
      l++;
    while (S[r] == 'W')
      r--;
    if (l < r) {
      swap(S[l], S[r]);
      ans++;
    }
    l++, r--;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n - 1); i >= 0; --i)

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

typedef long long ll;
typedef pair<int, int> P;

const int MAX = 200005;
const int INF = 1001001001;
const int MOD = 1000000007;

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

  int l = 0, r = N - 1;
  int ans = 0;
  while (l < r) {
    bool flg = 0;
    for (int i = l; i < r; ++i) {
      if (S[i] == 'W' && S[i + 1] == 'R')
        flg = 1;
    }
    if (flg) {
      while (S[l] == 'R')
        l++;
      while (S[r] == 'W')
        r--;
      if (l < r) {
        swap(S[l], S[r]);
        ans++;
      }
    }
    l++, r--;
  }

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

#include <bits/stdc++.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define ll long long int
#define mod 1000000007
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define prec(n) fixed << setprecision(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>
#define PI 3.1415926535897932384626
#define bits(n) __builtin_popcount(n)

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cr = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'R')
      cr++;
  }
  int res = 0;
  for (int i = 0; i < cr; i++) {
    if (s[i] == 'W')
      res++;
  }
  cout << res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input1.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output1.txt", "w", stdout);
#endif

  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define ll long long int
#define mod 1000000007
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define prec(n) fixed << setprecision(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>
#define PI 3.1415926535897932384626
#define bits(n) __builtin_popcount(n)

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> a;
  a.push_back(0);
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'R')
      a.push_back(i);
  }
  int c = 0;
  for (int i = 1; i < a.size(); i++) {
    if (a[i] - a[i - 1] > 1) {
      for (int j = 0; j < a[i] - a[i - 1]; j++) {
        a.pop_back();
        c++;
      }
    }
  }
  cout << c;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input1.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output1.txt", "w", stdout);
#endif

  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
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 s;
  cin >> s;
  string t = s;
  sort(t.begin(), t.end());
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == t[i])
      continue;
    ans++;
  }
  cout << (ans + 1) / 2;
}


Error: TLE

Fixed submission:

#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 s;
  cin >> s;
  int ans = 0;
  int sentou = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == 'W')
      continue;
    for (int j = sentou; j < i; j++) {
      if (s[j] == 'W') {
        ans++;
        sentou = j + 1;
        break;
      }
    }
  }
  cout << ans;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

#define rep(i, a, b) for (ll i = a; i < b; i++)
typedef long long ll;
int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll j = 0, k = 0;
  rep(i, 0, n) {
    if (s[i] == 'R')
      j++;
    else
      k++;
  }
  ll h = 0, m = 0;
  rep(i, 0, j) {
    if (s[i] == 'W')
      h++;
  }
  for (ll i = n - 1; i >= max(0ll, n - k); i--)
    if (s[i] == 'R')
      m++;
  cout << min(h, m);
  return 0;
}

Error: WA

Fixed submission:

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

#define rep(i, a, b) for (ll i = a; i < b; i++)
#define pb push_back
#define lcm(a, b) (a * b) / __gcd(a, b)
#define bs(v, x) binary_search(v.begin(), v.end(), x)
#define ub(v, x) upper_bound(v.begin(), v.end(), x)
#define lb(v, x) lower_bound(v.begin(), v.end(), x)
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define all(a) (a).begin(), (a).end()
#define inp(a, n)                                                              \\
  for (ll i = 0; i < n; i++)                                                   \\
    cin >> a[i];
#define trace(v)                                                               \\
  for (auto it = v.begin(); it != v.end(); it++)                               \\
    cout << *it << " ";                                                        \\
  cerr << endl;
#define printv(a, n)                                                           \\
  for (ll i = 1; i <= n; i++)                                                  \\
    cout << a[i] << " ";                                                       \\
  cout << "\\n";
#define Pi 3.14159265
#define PII pair<ll, ll>;
#define reset(x) memset(x, 'z', sizeof(x))
#define f first
#define s second
#define mp make_pair
#define pq1 priority_queue<ll>;
#define pq2 priority_queue<ll, vector<ll>, greater<ll>>;
typedef long long ll;
typedef vector<ll> vll;
typedef bool B;
typedef double ff;
const ll INF = 1e10;
const ll MAX_N = 100005;
char en = '\\n';
const int N = 1e5 + 7;

void solve() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll j = 0, k = 0;
  rep(i, 0, n) {
    if (s[i] == 'R')
      j++;
    else
      k++;
  }
  ll h = 0, m = 0;
  rep(i, 0, j) {
    if (s[i] == 'W')
      h++;
  }
  for (ll i = n - 1; i >= max(0ll, n - k); i--)
    if (s[i] == 'R')
      m++;
  cout << min(h, m);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_eUDGE
  freopen("input.txt", "r", stdin);
  freopen("error.txt", "w", stderr);
  freopen("output.txt", "w", stdout);
#endif
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
    cout << en;
  }
  cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define PI 3.14159265358979323846264338327950
#define INF 10000000000000000

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int numr = 0;
  int numw = 0;
  int ans = 200001;
  rep(i, n) {
    if (s[i] == 'R') {
      numr++;
    }
  }
  ans = min(ans, max(numr, numw));
  rep(i, n) {

    if (s[i] == 'W') {
      numw++;
    } else {
      numr--;
    }
    ans = min(ans, max(numr, numw));
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define PI 3.14159265358979323846264338327950
#define INF 10000000000000000

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int t = 0;
  int ans = 200001;
  while (t <= n) {
    int numw = 0;
    int numr = 0;
    rep(i, t) {
      if (s[i] == 'W') {
        numw++;
      }
    }
    for (int k = t; k < n; k++) {
      if (s[k] == 'R') {
        numr++;
      }
    }
    ans = min(ans, max(numw, numr));
    t++;
  }
  cout << ans << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

using namespace std;

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

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

  int counter = 0;
  int numBeforeAllRed = 0;
  for (int i = 1; i <= N; i++) {
    if (c[i] == 'W') {
      counter++;
    } else {
      break;
    }
  }
  if (counter >= N / 2) {
    numBeforeAllRed = 1;
  }
  counter = 0;
  for (int i = N; i != 1; i--) {
    if (c[i] == 'R') {
      if (numBeforeAllRed > 0) {
        if (i > numBeforeAllRed) {
          counter++;
        } else {
          break;
        }
      } else {
        for (int j = 1; j < i; j++) {
          numBeforeAllRed = j;
          if (c[j] == 'W') {
            c[j] = 'R';
            c[i] = 'W';
            counter++;
            numBeforeAllRed = 0;
            break;
          }
        }
      }
    }
  }
  cout << counter << "\\n";
}


Error: TLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

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

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

  int counter = 0;
  int numBeforeAllRed = 0;
  for (int i = N; i != 1; i--) {
    if (c[i] == 'R') {
      if (numBeforeAllRed > 0) {
        if (i > numBeforeAllRed) {
          counter++;
        } else {
          break;
        }
      } else {
        for (int j = 1; j < i; j++) {
          numBeforeAllRed = j;
          if (c[j] == 'W') {
            c[j] = 'R';
            c[i] = 'W';
            counter++;
            numBeforeAllRed = 0;
            break;
          }
        }
      }
    }
  }
  cout << counter << "\\n";
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int n, i, j, result = 0;
  cin >> n;
  char t, c[n];

  for (i = 0; i < n; i++)
    cin >> c[i];
  i = 0;
  j = n - 1;
  while (i < j) {
    while (c[i] != 'W' && i < n)
      i++;
    while (c[j] != 'R' && j >= 0)
      j--;
    if (i < j) {
      result++;
      t = c[i];
      c[i] = c[j];
      c[j] = t;
      i++;
      j--;
    }
  }
  cout << result;
  return (0);
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, i, j, result = 0;
  char t, c[n];
  cin >> n;
  for (i = 0; i < n; i++)
    cin >> c[i];
  i = 0;
  j = n - 1;
  while (i < j) {
    while (c[i] != 'W' && i < n)
      i++;
    while (c[j] != 'R' && j >= 0)
      j--;
    if (i < j) {
      result++;
      t = c[i];
      c[i] = c[j];
      c[j] = t;
      i++;
      j--;
    }
  }
  cout << result;
  return (0);
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (s[i] == 'R')
      cnt++;
  }
  int ans = 0;
  for (int i = 0; i < cnt; i++) {
    if (s[i] == 'W')
      ans++;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

int N;
string s;
int stone[200000];

int lower() {
  int res = 0;
  for (int i = 0; i < N; i++) {
    if (stone[i] == 1)
      return i;
  }
  return res;
}
int main() {
  cin >> N >> s;
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (s[i] == 'R') {
      stone[i] = 0;
      cnt++;
    } else
      stone[i] = 1;
  }
  if (cnt == N) {
    cout << 0 << endl;
    return 0;
  }
  int num = lower();
  int ans = 0;
  int check = N - 1;
  while (num < cnt) {

    if (stone[check] == 0) {
      swap(stone[num], stone[check]);
      ans++;
      num = lower();
    }
    check--;
  }
  cout << ans << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long int ll;
typedef vector<long long> lve;
typedef vector<pair<ll, ll>> lvp;
typedef vector<pair<string, ll>> svp;

#define endl '\\n'
#define F first
#define S second
#define pp pop_back
#define PI 2 * acos(0)
#define pb push_back
#define mp make_pair
#define GCD(x, y) __gcd(x, y)
#define lb lower_bound
#define up upper_bound
#define bs binary_search
#define For(i, j, k) for (i = j; i < k; i++)
#define FOR(i, j, k) for (i = j; i >= k; i--)
#define LCM(x, y) ((x * y) / __gcd(x, y))
#define btpc __builtin_popcountll
#define sp(x) cout << fixed << setprecision(x)

const long long cs = 2e5 + 5;
const long long inf = 1e18;
const long long mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll t, n, m, a, b, c, d, e, i, j, k, x, y, z, cnt = 0;
  bool flag = false;

  cin >> n;
  string s;
  cin >> s;
  x = n - 1, d = 0, cnt = 0;

  while (x > d) {
    while (d < n && s[d] == 'R')
      d++;
    while (x >= 0 && s[x] == 'W')
      x--;
    if (d > x)
      break;
    cnt++;
    s[d] = 'R', s[x] = 'W';
  }

  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long int ll;
typedef vector<long long> lve;
typedef vector<pair<ll, ll>> lvp;
typedef vector<pair<string, ll>> svp;

#define endl '\\n'
#define F first
#define S second
#define pp pop_back
#define PI 2 * acos(0)
#define pb push_back
#define mp make_pair
#define GCD(x, y) __gcd(x, y)
#define lb lower_bound
#define up upper_bound
#define bs binary_search
#define For(i, j, k) for (i = j; i < k; i++)
#define FOR(i, j, k) for (i = j; i >= k; i--)
#define LCM(x, y) ((x * y) / __gcd(x, y))
#define btpc __builtin_popcountll
#define sp(x) cout << fixed << setprecision(x)

const long long cs = 2e5 + 5;
const long long inf = 1e18;
const long long mod = 1e9 + 7;
char s[cs];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll t, n, m, a, b, c, d, e, i, j, k, x, y, z, cnt = 0;
  bool flag = false;
  char c1;
  cin >> n;
  cin >> s;
  for (i = n - 1; i >= 1; i--) {
    for (j = 0; j < i; j++) {
      if (s[i] == 'R' && s[j] == 'W') {
        c1 = s[i];
        s[i] = s[j];
        s[j] = c1;
        cnt++;
        break;
      }
    }
  }
  for (i = n - 1; i >= 0; i--) {
    if (s[i] == 'R') {
      x = i;
      break;
    }
  }
  for (i = 0; i <= x; i++) {
    if (s[i] == 'W')
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}