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

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

#define mod 1000000000000000007
#define pi 3.1415926535897932384626433832795
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vecsort(v) sort(v.begin(), v.end())
#define lcm(x, y) (x / __gcd(x, y)) * y
#define forn(i, n) for (i = 0; i < n; i++)
#define deb(x) cout << #x << " " << x << endl;
#define vecsum(v) accumulate(v.begin(), v.end(), 0)
#define T(t) cout << "Case " << t << ": ";
#define fast_cin                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

int mark[1000005];
int main() {

  fast_cin;

  ll n;
  cin >> n;
  string s;
  cin >> s;
  int w = 0, r = 0;
  for (int i = 0; s[i]; i++) {
    w += (s[i] == 'W');
    r += (s[i] == 'R');
  }
  int ans1 = 0, ans2 = 0;
  for (int i = 0; i < r; i++) {
    if (s[i] == 'W')
      ans1++;
  }
  for (int i = r; i < n; i++) {
    if (s[i] == 'R')
      ans2++;
  }

  cout << min(ans1, ans2) << endl;
}

Error: TLE

Fixed submission:

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

#define mod 1000000000000000007
#define pi 3.1415926535897932384626433832795
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vecsort(v) sort(v.begin(), v.end())
#define lcm(x, y) (x / __gcd(x, y)) * y
#define forn(i, n) for (i = 0; i < n; i++)
#define deb(x) cout << #x << " " << x << endl;
#define vecsum(v) accumulate(v.begin(), v.end(), 0)
#define T(t) cout << "Case " << t << ": ";
#define fast_cin                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)

int main() {

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

  int r = 0;
  int w = 0;

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

  int cnt = 0;
  for (int i = 0; i < s.size(); i++) {

    if (s[i] == 'W') {
      int x = s.rfind('R');
      if (x > i) {
        swap(s[i], s[x]);
        cnt++;
      }
    }
  }
  cout << cnt << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  int nn;
  cin >> nn;
  string s;
  cin >> s;
  vector<int> vec;
  bool mrk = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'W') {
      vec.push_back(i);
      mrk = 1;
    }
  }
  if (!mrk)
    cout << 0 << endl;
  else {
    int in = 0, cnt = 0;
    for (int j = s.size() - 1; j >= 0; j--) {
      if (s[j] == 'R') {
        if (vec[in] < j) {
          cnt++;
          swap(s[vec[in++]], s[j]);
          if (in >= j)
            break;
          if (in >= s.size())
            break;
        } else {
          break;
        }
      }
    }
    cout << cnt << endl;
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  int nn;
  cin >> nn;
  string s;
  cin >> s;
  vector<int> vec;
  bool mrk = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == 'W') {
      vec.push_back(i);
      mrk = 1;
    }
  }
  if (!mrk)
    cout << 0 << endl;
  else {
    int in = 0, cnt = 0;
    for (int j = s.size() - 1; j >= 0; j--) {
      if (s[j] == 'R') {
        if (vec[in] < j) {
          cnt++;
          swap(s[vec[in++]], s[j]);
          if (in >= s.size())
            break;
        } else {
          break;
        }
      }
    }
    cout << cnt << endl;
  }
}
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)
#define MOD 1e9 + 7;
using namespace std;
using ll = long long;

int main() {
  int n, ans = 0;
  cin >> n;
  char str;
  vector<int> c(n);
  rep(i, n) {
    cin >> str;
    if (str == 'W')
      c[i] = 0;
    else
      c[i] = 1;
  }
  int x = 0, y = n - 1;
  while (x != y) {
    if (c[x] == 0 && c[y] == 1) {
      x++;
      if (x != y)
        y--;
      ans++;
    } else if (c[x] == 0 && c[y] == 0)
      y--;
    else if (c[x] == 1 && c[y] == 1)
      x++;
    else if (c[x] == 1 && c[y] == 0)
      x++;
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, ans = 0;
  cin >> n;
  char str;
  vector<int> c(n);
  rep(i, n) {
    cin >> str;
    if (str == 'W')
      c[i] = 0;
    else
      c[i] = 1;
  }
  int x = 0, y = n - 1;
  while (x != y) {
    if (c[x] == 0 && c[y] == 1) {
      x++;
      if (x != y)
        y--;
      ans++;
    } else if (c[x] == 0 && c[y] == 0)
      y--;
    else if (c[x] == 1 && c[y] == 1)
      x++;
  }
  cout << 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 a;
  cin >> a;
  int a1 = 0;
  int a2 = n - 1;
  int minoperation = 0;
  while (1) {
    // cout<<a1<<" "<<a2<<endl;
    if (a2 <= a1)
      break;
    if (a[a1] == 'W' && a[a2] == 'R') {
      swap(a[a1], a[a2]);
      minoperation++;
    }
    if (a[a1] == 'R')
      a1++;
    if (a[a2] == 'W')
      a2--;
  } // cout<<a<<endl;
  cout << minoperation;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  string a;
  cin >> a;
  int a1 = 0;
  int a2 = n - 1;
  int minoperation = 0;
  while (a1 != a2 || abs(a2 - a1)) {
    if (a[a1] == 'R' && a[a2] == 'W') {
      swap(a[a1], a[a2]);
      minoperation++;
    }
    a1++;
    a2--;
  }
  cout << minoperation;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define fint register int
#define h 5001
#define p 459384
using namespace std;
char a[p];
int main() {
  int n;
  cin >> n;
  scanf("%s", a + 1);
  int ans = 0;
  int j = n;
  for (fint i = 1; i <= n; i++) {
    if (a[i] == 'W')
      for (; j >= 1; j--) {
        if (j <= i)
          goto flag;
        if (a[j] == 'R') {
          a[i] = 'R', a[j] = 'W', ans++;
          break;
        }
      }
  }
flag:
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fint register int
#define h 5001
#define p 459384
using namespace std;
char a[p];
int main() {
  int n;
  cin >> n;
  scanf("%s", a + 1);
  int ans = 0;
  for (fint i = 1; i <= n; i++) {
    if (a[i] == 'W')
      for (fint j = n; j >= 1; j--) {
        if (j <= i)
          goto flag;
        if (a[j] == 'R') {
          a[i] = 'R', a[j] = 'W', ans++;
          break;
        }
      }
  }
flag:
  cout << ans;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

using namespace std;

string exchange(string c, int i, int j) {
  string temp;
  temp = c[i];
  c[i] = c[j];
  c[j] = temp[0];

  return c;
}

string change(string c, int i) {

  if (c[i] == 'W') {
    c[i] = 'R';
  } else {
    c[i] = 'W';
  }

  return c;
}

int judge(string c, int N) {
  int t = 0;
  int has_R = 0;
  int area_R = 0;

  for (int i = 0; i < N; i++) { // count a number of 'R'
    if (c[i] == 'R') {
      has_R++;
    }
  }

  for (int i = has_R; i < N; i++) {

    if (c[i] == 'R') {
      for (int j = area_R; j < has_R; j++) {
        if (c[j] == 'W') {
          c = exchange(c, j, i);
          t++;
          area_R = j;

          break;
        }
      }
    }
    has_R = i;
  }

  return t;
}

int main(void) {
  int N;
  string c;

  cin >> N;
  cin >> c;

  int ans;

  ans = judge(c, N);
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

string exchange(string c, int i, int j) {
  string temp;
  temp = c[i];
  c[i] = c[j];
  c[j] = temp[0];

  return c;
}

string change(string c, int i) {

  if (c[i] == 'W') {
    c[i] = 'R';
  } else {
    c[i] = 'W';
  }

  return c;
}

int judge(string c) {
  int t = 0;
  int length = c.length();
  int has_R = 0;

  for (int i = 0; i < length; i++) { // count a number of 'R'
    if (c[i] == 'R') {
      has_R++;
    }
  }

  for (int i = has_R; i < length; i++) {
    if (c[i] == 'R') {
      for (int j = 0; j < has_R; j++) {
        if (c[j] == 'W') {
          c = exchange(c, j, i);
          t++;

          break;
        }
      }
    }
    has_R = i;
  }

  return t;
}

int main(void) {
  int N;
  string c;

  cin >> N;
  cin >> c;

  int ans;

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

#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define MOD 1000000007

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;

const ll INF = LLONG_MAX;
const int mxn = 2e5 + 5;
bool DEBUG = 0;
string s;
int n;
int main() {
  // ios_base::sync_with_stdio(0); cin.tie(0);
  cin >> n >> s;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += s[i] == 'R';
  }
  int ans = 0;
  for (int i = 0; i < cnt; i++) {
    ans += s[i] == 'W';
  }
  cout << ans << '\\n';
}

// READ & UNDERSTAND
// ll, int overflow, array bounds, memset(0)
// special cases (n=1?), n+1 (1-index)
// do smth instead of nothing & stay organized
// WRITE STUFF DOWN


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define MOD 1000000007

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;

const ll INF = LLONG_MAX;
const int mxn = 2e5 + 5;
bool DEBUG = 0;

int solve(string s) {
  int n = s.length();
  int ptr = 0, ptr2 = n - 1;
  while (ptr < n) {
    if (s[ptr] == 'R')
      ptr++;
    else
      break;
  }
  while (ptr2 >= 0) {
    if (s[ptr2] == 'W')
      ptr2--;
    else
      break;
  }
  int len = ptr2 - ptr + 1;
  if (len == 0)
    return 0;
  else
    swap(s[ptr], s[ptr2]);
  return 1 + solve(s.substr(ptr, len));
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += s[i] == 'W';
  }
  if (cnt == 0 || cnt == n) {
    cout << 0 << '\\n';
    return 0;
  }
  cout << solve(s);
}

// READ & UNDERSTAND
// ll, int overflow, array bounds, memset(0)
// special cases (n=1?), n+1 (1-index)
// do smth instead of nothing & stay organized
// WRITE STUFF DOWN
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int n;
char a[2222222];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int l = -1, r = n;
  int ans = 0;
  while (l < r) {
    l++;
    while (a[l] != 'W' && l < n)
      l++;
    r--;
    while (a[r] != 'R' && r >= 0)
      r--;
    ans++;
  }
  cout << ans - 1 << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n;
char a[2222222];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int l = -1, r = n;
  int ans = 0;
  while (l < r && l < n && r >= 0) {
    l++;
    while (a[l] != 'W')
      l++;
    r--;
    while (a[r] != 'R')
      r--;
    ans++;
  }
  cout << ans - 1 << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define FASTio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long int
#define endl '\\n'
ll mod = 1e9 + 7;

int main() {
  FASTio;
  int n, i, j;
  cin >> n;
  string s;
  cin >> s;
  int r = 0, w = 0;
  for (i = 0; i < n; i++) {
    if (s[i] == 'W')
      w++;
    if (s[i] == 'R')
      r++;
  }
  int temp = 0;
  for (i = 0; i < r; i++) {
    if (s[i] == 'W')
      temp++;
  }

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FASTio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long int
#define endl '\\n'
const int mod = 1e9 + 7;

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\\n"

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  int rCount = 0;
  for (int i = 0; i < N; ++i) {
    if (s[i] == 'R')
      ++rCount;
  }

  int count = 0;
  for (int i = 0; i < rCount; ++i) {
    if (s[i] == 'W')
      ++count;
  }

  cout << count << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
long long MOD = 1000000007LL;
const double PI = 3.14159265358979323846;
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define endl "\\n"

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  int rCount = 0;
  for (int i = 0; i < N; ++i) {
    if (s[i] == 'R')
      ++rCount;
  }

  int count = 0;
  for (int i = 0; i < s.length(); ++i) {
    if (s[i] == 'W') {
      for (int j = max(i + 1, rCount); j < s.length(); ++j) {
        if (s[j] == 'R') {
          swap(s[i], s[j]);
          ++count;
          break;
        }
      }
    }
  }

  cout << count << endl;

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

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

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

  int64_t N;
  cin >> N;

  string c(N, 'a');
  cin >> c;

  int64_t cnt = 0;

  int64_t j = N - 1;
  int64_t i = 0;

  while (i < j and j >= 0 and i <= N - 1) {
    if (c[i] == 'W' and c[j] == 'W') {
      j--;
    }
    if (c[i] == 'W' and c[j] == 'R') {
      i++;
      j--;
      cnt++;
    }
    if (c[i] == 'R' and c[j] == 'W') {
      i++;
      j--;
    }
    if (c[i] == 'R' and c[j] == 'R') {
      i++;
    }
  }
  cout << cnt;

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

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

  int64_t N;
  cin >> N;

  string c(N, 'a');
  cin >> c;

  int64_t cnt = 0;

  int64_t j = N - 1;
  int64_t i = 0;

  while (i < j and j >= 0 and i <= N - 1) {
    if (c.at(i) == 'W' and c.at(j) == 'W') {
      j--;
    }
    if (c.at(i) == 'W' and c.at(j) == 'R') {
      i++;
      j--;
      cnt++;
    }
    if (c.at(i) == 'R' and c.at(j) == 'W') {
      i++;
      j--;
    }
    if (c.at(i) == 'R' and c.at(j) == 'R') {
      i++;
    }
  }
  cout << cnt;

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

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

int main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt","r",stdin);
  // 	freopen("outputc.txt","w",stdout);
  // #endif

  // int t;
  // cin>>t;
  // while(t--)
  {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int ans = 0;
    int i, j;
    for (i = 0, j = n - 1; i < n && j >= 0; i++) {
      if (i >= j)
        break;
      if (s[i] == 'W') {
        while (1) {
          if (s[j] == 'R' || i >= j || j == 0)
            break;
          j--;
        }
        if (i != j) {
          swap(s[i], s[j]);
          ans++;
        }
      }
      // cout<<s<<'\\n';
    }
    // cout<<s;
    cout << ans << '\\n';
  }
}


Error: 0

Fixed submission:

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

int main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt","r",stdin);
  // 	freopen("outputc.txt","w",stdout);
  // #endif

  // int t;
  // cin>>t;
  // while(t--)
  {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int ans = 0;
    int i, j;
    for (i = 0, j = n - 1; i < n && j >= 0; i++) {
      if (i == j)
        break;
      if (s[i] == 'W') {
        while (1) {
          if (s[j] == 'R' || j == i)
            break;
          j--;
        }
        if (i != j) {
          swap(s[i], s[j]);
          ans++;
        }
      }
    }
    for (i = 1; i < n; i++) {
      if (s[i] == 'R' && s[i - 1] == 'W')
        ans++;
    }
    cout << ans << '\\n';
  }
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

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

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  string cs;
  cin >> cs;
  int count = 0;
  int sum = 0;
  for (int i = n - 1; i >= 0; i) {
    if (cs[i] == 'W') {
      sum += (i + 1) - n - count;
      count++;
    }
  }
  cout << sum << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

int match(char *s, char *ss, int n) {
  int count = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] != ss[i])
      count++;
  }
  return count / 2;
}
int main() {
  int n, r = 0, w = 0;
  cin >> n;
  char s[n + 1], ss[n + 1];
  cin >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      w++;
    else
      r++;
  }
  for (int i = 1; i <= n; i++) {
    if (i <= r)
      ss[i - 1] = 'R';
    else
      ss[i - 1] = 'W';
  }
  cout << match(s, ss, n);
}


Error: WA

Fixed submission:

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

using namespace std;

int match(char *s, char *ss, int n) {
  int count = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] != ss[i])
      count++;
  }
  return count / 2;
}
int main() {
  int n, r = 0, w = 0;
  char s[n + 1], ss[n + 1];
  cin >> n >> s;
  // cout<<s<<endl;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      w++;
    else
      r++;
  }
  for (int i = 1; i <= n; i++) {
    if (i <= r)
      ss[i - 1] = 'R';
    else
      ss[i - 1] = 'W';
  }
  cout << match(s, ss, n);
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()

int main() {
  ll N;
  cin >> N;
  string S;
  cin >> S;
  ll ans = 0;
  ll red = 0;
  for (ll i = 0; i < N; i++) {
    if (S[i] == 'W') {
      for (ll j = red; j < N - i - 1; j++) {
        if (S[N - 1 - j] == 'R') {
          ans++;
          red = j + 1;
          break;
        }
        if (j == N - i - 2) {
          red = j + 1;
        }
      }
      if (red == N - i - 1) {
        break;
      }
    }
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()

int main() {
  ll N;
  cin >> N;
  string S;
  cin >> S;
  ll ans = 0;
  ll red = 0;
  for (ll i = 0; i < N; i++) {
    if (S[i] == 'W') {
      for (ll j = red; j < N - i - 1; j++) {
        if (S[N - 1 - j] == 'R') {
          ans++;
          red = j + 1;
          break;
        }
      }
    }
  }

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

#include <bits/stdc++.h>
#define int long long
#define FASTIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
/*int fun(string &str)
{
    int ind=-1;
    for(int i=0;i<str.size()-1;i++)
    {
        if(str[i]=='W' && str[i+1]=='R')
        {
            ind=i;
            break;
        }
    }
    if(ind==-1)
        return 1;
    return 0;
}*/
void swap(char *a, char *b) {
  char c = (*a);
  *a = *b;
  *b = c;
}
int32_t main() {
  int n;
  cin >> n;
  string str;
  cin >> str;
  int re = 0, w = 0;
  for (int i = 0; i < str.size(); i++) {
    if (str[i] == 'R')
      re++;
    else
      w++;
  }
  int ans = min(re, w);
  int cnt = 0, p = 0, r = n - 1;
  int ok1 = 0, ok2 = 0;
  while (p <= r) {
    if (str[p] == 'W') {
      ok1 = 1;
    }
    if (str[r] == 'R') {
      ok2 = 1;
    }
    if (ok1 == 1 && ok2 == 1) {
      str[p] = 'R';
      str[r] = 'W';
      cnt++;
      p++;
      r--;
      ok1 = 0;
      ok2 = 0;
    } else if (ok1 == 0 && ok2 == 1) {
      p++;
    } else if (ok1 == 1 && ok2 == 0) {
      r--;
    } else if (ok1 == 0 && ok2 == 0) {
      p++;
      r--;
    }
  }
  cout << min(ans, cnt);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define FASTIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
/*int fun(string &str)
{
    int ind=-1;
    for(int i=0;i<str.size()-1;i++)
    {
        if(str[i]=='W' && str[i+1]=='R')
        {
            ind=i;
            break;
        }
    }
    if(ind==-1)
        return 1;
    return 0;
}*/
void swap(char *a, char *b) {
  char c = (*a);
  *a = *b;
  *b = c;
}
int32_t main() {
  int n;
  cin >> n;
  string str;
  cin >> str;
  int re = 0, w = 0;
  for (int i = 0; i < str.size(); i++) {
    if (str[i] == 'R')
      re++;
    else
      w++;
  }
  int ans = min(re, w);
  int cnt = 0, p = 0, r = n - 1;
  int ok1 = 0, ok2 = 0;
  while (p <= r) {
    if (str[p] == 'W') {
      ok1 = 1;
    }
    if (str[r] == 'R') {
      ok2 = 1;
    }
    if (ok1 == 1 && ok2 == 1) {
      swap(&str[p], &str[r]);
      cnt++;
      p++;
      r--;
      ok1 = 0;
      ok2 = 0;
    } else if (ok1 == 0 && ok2 == 1) {
      p++;
    } else if (ok1 == 1 && ok2 == 0) {
      r--;
    }
  }
  cout << min(ans, cnt);
  return 0;
}