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

#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
int N;
string str;
int S[200001];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  cin >> str;
  rep(i, N) S[i] = str[i] == 'R' ? 0 : 1;
  int numzero = 0;
  int numswap = 0;
  rep(i, N) if (S[i] == 0) numzero++;
  rep(i, numzero) if (S[i] == 1) numswap++;
  int ans = numswap;
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
int N;
string str;
int S[200001];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  cin >> str;
  rep(i, N) S[i] = str[i] == 'R' ? 0 : 1;
  int numzero = 0;
  int numswap = 0;
  rep(i, N) if (S[i] == 0) numzero++;
  rep(i, numzero) if (S[i] == 1) numswap++;
  int ans = numswap;
  int count = 0;
  for (int x = -1; x < N; x++) {
    if (x >= 0 && S[x] == 1) {
      // S[x] = 0;
      numzero--;
      count++;
      if (x != numzero && S[numzero] == 1)
        numswap--;
    }
    int cnt = count;
    int ns = numswap;
    int nz = numzero;
    int y = N;
    ans = min(ans, cnt + ns);
  }
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
#define INF __INT32_MAX__
#define LINF __LONG_LONG_MAX__

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

  int cnt = 0;
  int l = 0;
  int r = N - 1;
  while (l < r) {
    // 一番左のWを見つける
    while (C[l] != 'W' && l < N)
      l++;

    // 一番右のRを見つける
    while (C[r] != 'R' && r >= 0)
      r--;

    if (l >= r)
      break;
    if (l >= N)
      break;
    if (r < 0)
      break;

    // cout << "l: " << l << ", r: " << r << endl;

    swap(C[l], C[r]);
    cnt++;
  }

  cout << cnt << endl;
  // cout << C << endl;
}

Error: 0

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;
typedef vector<int> vi;
typedef vector<long long> vl;
#define INF __INT32_MAX__
#define LINF __LONG_LONG_MAX__

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

  int cnt = 0;
  int l = 0;
  int r = N - 1;
  while (l < r) {
    if (l >= N)
      break;
    if (r < 0)
      break;

    // 一番左のWを見つける
    while (C[l] != 'W')
      l++;

    // 一番右のRを見つける
    while (C[r] != 'R')
      r--;

    if (l >= r)
      break;

    // cout << "l: " << l << ", r: " << r << endl;

    swap(C[l], C[r]);
    cnt++;
  }

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

#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const ll N = 2e5 + 5;

int n;
string s;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> s;
  int R = n - 1;
  int cnt = 0;
  for (int L = 0; L < R; L++, R--) {
    while (s[L] != 'W' && L < R)
      L++;
    while (s[R] != 'R' && L < R)
      R--;
    if (s[L] != 'W' || s[R] != 'R')
      break;
    cnt++;
  }
  cout << cnt << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF = 0x3f3f3f3f;
const ll mod = 1e9 + 7;
const ll N = 2e5 + 5;

int n;
string s, a;

int check() {
  a = s;
  int R = n - 1;
  int cnt = 0;
  for (int L = 0; L < R; L++) {
    while (s[L] != 'W' && L < R)
      L++;
    while (s[R] != 'R' && L < R)
      R--;
    if (s[L] != 'W' || s[R] != 'R')
      break;
    swap(s[L], s[R]);
    cnt++;
  }
  cout << cnt << '\\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> s;
  check();
  // int L=0,R=n+1;
  // while(L<R){
  // 	int mid=L+R>>1;
  // 	if(check(mid))
  // 		R=mid;
  // 	else
  // 		L=mid+1;
  // }
  // cout<<L<<'\\n';
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#pragma warning(disable : 4996)
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <math.h>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n, rtow = 0, wtor = 0;
  string s, s2;
  cin >> n >> s;
  s2 = s;
  int k = s.size();
  rep(i, k) {
    s2[i] = 'W';
    if (s[i] == 'R')
      rtow++;
  }

  int ans = rtow;
  rep(i, k) {
    s2[i] = 'R';
    if (s[i] == 'W')
      wtor++;
    if (s[i] == 'R')
      rtow--;
    if (max(wtor, rtow) <= ans)
      ans = max(rtow, wtor);
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#pragma warning(disable : 4996)
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <math.h>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n, rtow = 0, wtor = 0;
  string s, s2;
  cin >> n >> s;
  int k = s.size();
  rep(i, k) {
    s2[i] = 'W';
    if (s[i] == 'R')
      rtow++;
  }

  int ans = rtow;
  rep(i, k) {
    s2[i] = 'R';
    if (s[i] == 'W')
      wtor++;
    if (s[i] == 'R')
      rtow--;
    if (max(wtor, rtow) <= ans)
      ans = max(rtow, wtor);
  }
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <deque>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n;
  int w_count = 0, r_count = 0, cost = 0;
  string c;
  cin >> n >> c;
  for (int i = 0; i < n; i++)
    if (c[i] == 'R')
      r_count++;
  cost = r_count;
  for (int i = 0; i < n; i++) {
    if (c[i] == 'W')
      w_count++;
    else
      r_count--;
    cost = min(max(r_count, w_count), cost);
  }
  cout << cost << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n;
  int ans = 0;
  string c;
  cin >> n >> c;
  deque<int> r, w;
  for (int i = 0; i < n; i++) {
    if (c[i] == 'W')
      w.push_back(i);
    else
      r.push_front(i);
  }
  for (int i = 0; i < r.size(); i++) {
    for (int j = 0; j <= i; j++) {
      if (j >= w.size())
        break;
      if (r[i] > w[j]) {
        int p = r[i];
        r[i] = w[j];
        w[j] = p;
        ans++;
        break;
      }
    }
  }
  cout << ans << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long long N, W = 0, R = 0;
  cin >> N;
  string c;
  cin >> c;
  string p;

  for (int i = 0; i < N; i++) {
    if (c[i] == 'R') {
      R++;
    } else {
      W++;
    }
  }
  for (int i = 0; i < R; i++) {
    p.push_back('R');
  }
  for (int i = R; i < R + W; i++) {
    p.push_back('W');
  }
  long long counter = 0, M = c.size();
  long datap[c.size()], datac[c.size()];
  for (int i = 0; i < M; i++) {
    datac[i] = c[i];
    datap[i] = p[i];
    if (datac[i] != datap[i]) {
      counter++;
    }
  }
  cout << counter / 2 << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  long long N, W = 0, R = 0;
  cin >> N;
  string c;
  cin >> c;
  string p;

  for (int i = 0; i < N; i++) {
    if (c[i] == 'R') {
      R++;
    } else {
      W++;
    }
  }
  for (int i = 0; i < R; i++) {
    p[i] = 'R';
  }
  for (int i = R; i < R + W; i++) {
    p[i] = 'W';
  }
  long long counter = 0, M = c.size();
  long datap[c.size()], datac[c.size()];
  for (int i = 0; i < M; i++) {
    datac[i] = c[i];
    datap[i] = p[i];
    if (datac[i] != datap[i]) {
      counter++;
    }
  }
  cout << counter / 2 << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

ll N;
string s;
ll cnt[200005];

int main() {
  cin >> N >> s;
  ll ans = 0;
  ll l = 0;
  ll r = N - 1;
  while (1) {
    while (s[l] != 'W') {
      l++;
      if (l >= N)
        break;
    }
    while (s[r] != 'R') {
      r--;
      if (r < 0)
        break;
    }
    if (l < r)
      swap(s[l], s[r]);
    else
      break;
    ans++;
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

ll N;
string s;
ll cnt[200005];

int main() {
  cin >> N >> s;
  ll ans = 0;
  ll l = 0;
  ll r = N - 1;
  while (1) {
    while (s[l] != 'W')
      l++;
    while (s[r] != 'R')
      r--;
    if (l < r)
      swap(s[l], s[r]);
    else
      break;
    ans++;
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int swaps = 0;
  int N;
  cin >> N;
  string C;
  cin >> C;

  int left = 0, right = N - 1;
  while (left < right) {
    while ((left < N) && (C[left] < 'T'))
      left++;
    while ((right > -1) && (C[right] > 'T'))
      right--;
    if (left < right) {
      int temp = C[left];
      C[left] = C[right];
      C[right] = temp;
      swaps++;
    }
  }

  cout << swaps << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int swaps = 0;
  int N;
  cin >> N;
  vector<char> C(N);
  for (int i = 0; i < N; i++) {
    cin >> C[i];
  }
  int left = 0, right = N - 1;
  while (left < right) {
    while ((left < N) && (C[left] < 'T'))
      left++;
    while ((right > -1) && (C[right] > 'T'))
      right--;
    if (left < right) {
      swaps++;
    }
  }

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

// Wed Jul 29 19:48:22 IST 2020
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define forn(ii, a, b) for (int ii = (int)a; ii < (int)b; ii++)
#define debug(x) cerr << #x << " " << x << " -- \\n"
#define I__O ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define int64 long long
#define all(x) (x).begin(), (x).end()
#define precise(x) cout << fixed << setprecision(12) << x << '\\n';
using namespace std;
// policy based
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

const int MOD = 1e9 + 7;
const double pi = 3.141592653589793238;
const int N = 2e5 + 7;
const int INF = INT_MAX;

int tests() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  for (char &c : s)
    if (c == 'W')
      c = '0';
    else
      c = '1';

  vector<int> v[2];
  for (int i = 0; i < n; i++)
    v[s[i] - '0'].push_back(i);
  int ans = 0;
  if (v[0].empty())
    return cout << 0 << '\\n', 0;

  for (int i = 0; i < int(v[0].size()); i++)
    if (!v[1].empty() and v[1].back() > v[0][i]) {
      // cerr << (v[1].back());
      v[1].pop_back();
      ans++;
    }
  cout << ans << '\\n';
  return 0;
}
int32_t main() {
  I__O;
  int cases = 1;
  // cin >> cases ;
  for (int test = 1; test <= cases; ++test) {
    // cout << "Case #" << test << ":" << '\\n';
    tests();
  }
  return 0;
}


Error: 0

Fixed submission:

// Wed Jul 29 19:48:22 IST 2020
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define forn(ii, a, b) for (int ii = (int)a; ii < (int)b; ii++)
#define debug(x) cerr << #x << " " << x << " -- \\n"
#define I__O ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define int64 long long
#define all(x) (x).begin(), (x).end()
#define precise(x) cout << fixed << setprecision(12) << x << '\\n';
using namespace std;
// policy based
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;

const int MOD = 1e9 + 7;
const double pi = 3.141592653589793238;
const int N = 2e5 + 7;
const int INF = INT_MAX;

int tests() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  for (char &c : s)
    if (c == 'W')
      c = '0';
    else
      c = '1';

  vector<int> v[2];
  for (int i = 0; i < n; i++)
    v[s[i] - '0'].push_back(i);
  int ans = 0;
  for (int i = 0; i < int(v[0].size()); i++)
    if (!v[1].empty() and v[1].back() > v[0][i]) {
      v[1].pop_back();
      ans++;
    }
  cout << ans << '\\n';
}
int32_t main() {
  I__O;
  int cases = 1;
  // cin >> cases ;
  for (int test = 1; test <= cases; ++test) {
    // cout << "Case #" << test << ":" << '\\n';
    tests();
  }
  return 0;
}
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;
using ll = long long;

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

  int x;
  int b = 0;
  int y = N - 1;
  int count = 0;

  rep(i, N) {
    if (s.at(i) == 'W') {
      for (int j = y; j > i; j--) {
        if (s.at(j) == 'R') {
          char tmp = s.at(i);
          s.at(i) = s.at(j);
          s.at(j) = tmp;
          count++;
          b = 1;
          y = j - 1;
          break;
        }
      }
      if (b == 0) {
        cout << count << endl;
        return 0;
      }
      b = 0;
    }
  }

  cout << count << endl;
}


Error: TLE

Fixed submission:

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

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

  int x;
  int y = N - 1;
  int count = 0;

  rep(i, N) {
    if (s.at(i) == 'W') {
      for (int j = y; j > i; j--) {
        if (s.at(j) == 'R') {
          char tmp = s.at(i);
          s.at(i) = s.at(j);
          s.at(j) = tmp;
          count++;
          y = j - 1;
          break;
        }
      }
    }

    if (i >= y) {
      break;
    }
  }

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

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

typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vc c(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  int left = -1, right = n;
  int ans = -1;
  while (left <= right) {
    while (1) {
      left++;
      if (left == n) {
        break;
      }
      if (c[left] == 'W')
        break;
    }
    while (1) {
      right--;
      if (right == -1) {
        break;
      }
      if (c[right] == 'R')
        break;
    }
    ans++;
  }
  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

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

typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<char>> vvc;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vc c(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  int left = -1, right = n;
  int ans = -1;
  while (left < right) {
    while (1) {
      left++;
      if (c[left] == 'W')
        break;
    }
    while (1) {
      right--;
      if (c[right] == 'R')
        break;
    }
    ans++;
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repo(i, n) for (int i = 1; i <= n; i++)
#define INF 1001001001
#define INFll 100100100100100
// debug用
#define PrintVec(x)                                                            \\
  for (auto elementPrintVec : x) {                                             \\
    cout << elementPrintVec << " ";                                            \\
  }                                                                            \\
  cout << endl;

using namespace std;
using ull = unsigned long long;
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int lw = n, rr = -1;
  rep(i, n) {
    if (s[i] == 'R') {
      rr = i;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == 'W') {
      lw = i;
    }
  }
  int ans = 0;
  while (lw < rr) {
    ans++;
    lw++;
    rr--;
    while (lw < n && s[lw] != 'W') {
      lw++;
    }
    while (rr >= 0 && s[rr] != 'R') {
      rr--;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repo(i, n) for (int i = 1; i <= n; i++)
#define INF 1001001001
#define INFll 100100100100100
// debug用
#define PrintVec(x)                                                            \\
  for (auto elementPrintVec : x) {                                             \\
    cout << elementPrintVec << " ";                                            \\
  }                                                                            \\
  cout << endl;

using namespace std;
using ull = unsigned long long;
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

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

#include <bits/stdc++.h>
using namespace std;
int main() {

  int n;
  string s;
  cin >> n >> s;
  int red = 0, white = 0;
  for (int i = 0; i < n; i++) {
    red += (s[i] == 'R');
  }
  for (int i = 0; i < red; i++) {
    white += (s[i] == 'W');
  }
  cout << white << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  int n, cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }

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

          ++cnt;
          break;
        }
      }
    }
  }
  // if(cnt>0)
  cout << cnt << endl;
  // else cout<<0<<endl;

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

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  int n;
  int numr = n;
  int ans = 0;
  cin >> n;
  string s;
  cin >> s;
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W') {
      c[i] = 1;
      numr--;
    }
  }
  int t = 0;
  int k = 0;
  for (int i = n - 1; i >= ans; i--) {

    if (c[i] == 0) {
      for (int j = k; j < i; j++) {
        if (c[j] == 1) {
          ans++;
          c[i] = 1;
          c[j] = 0;
          k = j + 1;
          break;
        } else if (j == i - 1) {
          t = 1;
        }
      }
    }
    if (t == 1)
      break;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  int n;
  int ans = 0;
  cin >> n;
  string s;
  cin >> s;
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W') {
      c[i] = 1;
    }
  }
  int t = 0;

  for (int i = n - 1; i >= ans; i--) {

    if (c[i] == 0) {
      for (int j = 0; j < i; j++) {
        if (c[j] == 1) {
          ans++;
          c[i] = 1;
          c[j] = 0;
          break;
        } else if (j == i - 1) {
          t = 1;
        }
      }
    }
    if (t == 1)
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef map<ll, ll>::iterator itll;
typedef long double ld;
typedef map<ll, ll> mapll;

#define con continue
#define pb push_back
#define fi first
#define se second
#define fr front()
#define one 1.0
#define inf 1000000000000000000
#define all(vl) vl.begin(), vl.end()
#define m_p make_pair
#define sz(a) sizeof(a)
#define forn(mp, it) for (it = mp.begin(); it != mp.end(); it++)
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define FORE(i, a, n) FOR(i, a, n + 1)
#define Endl endl
#define eNdl endl
#define enDl endl
#define endL endl
#define PI 3.14159265358979323846

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll lef = 0, rig = s.size() - 1;
  ll ans = 0;
  while (1) {
    while (lef < s.size() && s[lef] == 'R')
      lef++;
    while (rig >= 0 && s[rig] == 'W')
      rig--;
    if (lef == s.size())
      break;
    if (rig == -1)
      break;
    if (lef > rig)
      break;
    s[lef] = 'R';
    s[rig] = 'W';
    ans++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef map<ll, ll>::iterator itll;
typedef long double ld;
typedef map<ll, ll> mapll;

#define con continue
#define pb push_back
#define fi first
#define se second
#define fr front()
#define one 1.0
#define inf 1000000000000000000
#define all(vl) vl.begin(), vl.end()
#define m_p make_pair
#define sz(a) sizeof(a)
#define forn(mp, it) for (it = mp.begin(); it != mp.end(); it++)
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define FORE(i, a, n) FOR(i, a, n + 1)
#define Endl endl
#define eNdl endl
#define enDl endl
#define endL endl
#define PI 3.14159265358979323846

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll lef, rig;
  ll ans = 0;
  while (1) {
    lef = 0, rig = s.size() - 1;
    while (lef < s.size() && s[lef] == 'R')
      lef++;
    while (rig >= 0 && s[rig] == 'W')
      rig--;
    if (lef == s.size())
      break;
    if (rig == -1)
      break;
    if (lef > rig)
      break;
    s[lef] = 'R';
    s[rig] = 'W';
    ans++;
  }
  cout << ans << endl;
  return 0;
}