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

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

const ll P = 1000000007;
const long long INF = 1LL << 60;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
vector<char> Alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
                         'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                         'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
vector<char> alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                         'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                         's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cout << fixed << setprecision(10);
  int N;
  cin >> N;
  int r = N - 1, cnt = 0;
  vector<char> c(N);
  rep(i, N) cin >> c[i];
  rep(i, N) {
    if (c[i] == 'W') {
      for (int j = r; j >= i; j--) {
        if (c[j] == 'R') {
          swap(c[i], c[j]);
          r = j;
          cnt++;
          break;
        }
        if (j == i) {
          cout << cnt << endl;
          return 0;
        }
      }
    }
  }
  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ll long long
using namespace std;

const ll P = 1000000007;
const long long INF = 1LL << 60;
int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
vector<char> Alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
                         'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                         'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
vector<char> alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                         'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                         's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cout << fixed << setprecision(10);
  int N;
  cin >> N;
  int r = N - 1, cnt = 0;
  vector<char> c(N);
  rep(i, N) cin >> c[i];
  rep(i, N) {
    if (c[i] == 'W') {
      for (int j = r; j >= i; j--) {
        if (c[j] == 'R') {
          swap(c[i], c[j]);
          r = j;
          cnt++;
          break;
        }
      }
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
// #define int long long
#define ar array<int, 2>
#define inf 1000000000000000000
int mod = 1e9 + 7;
int min(int a, int b) { return (a < b) ? a : b; }
int max(int a, int b) { return (a > b) ? a : b; }
int fp(int a, int b) {
  if (b == 0)
    return 1;
  int x = fp(a, b / 2);
  x = (x * x) % mod;
  if (b & 1)
    x = (x * a) % mod;
  return x;
}

const int N = 1e6 + 5;
int mp[N] = {0};

void solve() {
  int n;
  cin >> n;
  string s, s1;
  cin >> s;
  int w = 0, r = 0;
  for (auto i : s) {
    if (i == 'W')
      w++;
    else
      r++;
  }
  while (r > 0) {
    r--;
    s1.push_back('R');
  }
  while (w > 0) {
    w--;
    s1.push_back('W');
  }

  // cout<<s;
  int mp[200];
  mp['W'] = 0;
  mp['R'] = 0;

  for (int i = 0; i < s.size(); i++)
    if (s[i] != s1[i])
      mp[s[i]]++;
  int ans = min(mp['W'], mp['R']) + abs(mp['W'] - mp['R']);
  cout << ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  srand(time(0));
  int t = 1;
  for (int i = 1; i <= t; i++) {
    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
// #define int long long
#define ar array<int, 2>
#define inf 1000000000000000000
int mod = 1e9 + 7;
int min(int a, int b) { return (a < b) ? a : b; }
int max(int a, int b) { return (a > b) ? a : b; }
int fp(int a, int b) {
  if (b == 0)
    return 1;
  int x = fp(a, b / 2);
  x = (x * x) % mod;
  if (b & 1)
    x = (x * a) % mod;
  return x;
}

const int N = 1e6 + 5;
int mp[N] = {0};

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  string s1 = s;

  sort(s.begin(), s.end(), [&](char a, char b) {
    if (a == 'W')
      return 0;
    return 1;
  });
  // cout<<s;
  map<char, int> mp;
  for (int i = 0; i < s.size(); i++)
    if (s[i] != s1[i])
      mp[s[i]]++;
  int ans = min(mp['W'], mp['R']) + abs(mp['W'] - mp['R']);
  cout << ans;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  srand(time(0));
  int t = 1;
  for (int i = 1; i <= t; i++) {
    solve();
  }
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;
#define ll long long int

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

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

  return 0;
}


Error: 0

Fixed submission:

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

using namespace std;
#define ll long long int

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n, r = 0, w = 0, rh, wh;
  cin >> n;
  string s;
  cin >> s;
  std::vector<ll> rv, wv;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'R') {
      r++;
      rv.push_back(i);
    } else {
      w++;
      wv.push_back(i);
    }
    if (i == (n - 1) / 2) {
      wh = w;
      rh = r;
    }
  }
  ll ans = min(r, w);
  if (r == 0 || w == 0)
    cout << "0" << endl;
  else {
    ans = min(ans, r - rh);
    ans = min(ans, wh);
    ans = min(ans, n / 2);
  }
  ll count = 0;
  ll wi = 0, ri = rv.size() - 1;
  while (rv[ri] > wv[wi]) {
    count++;
    ri--;
    wi++;
  }
  ans = min(count, ans);
  cout << ans << endl;

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

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define int long long
#define endl "\\n"
#define all(x) (x).begin(), (x).end()
const int MOD = 1e9 + 7;
const int mxN = 2e5 + 3;

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int st = 0;
  int en = n - 1;
  while (s[st] == 'R') {
    st++;
  }
  while (s[en] == 'W') {
    en--;
  }
  int ans = 0;
  while (st < en) {
    if (s[st] == 'W' and s[en] == 'R')
      ans++, st++, en--;
    else if (s[st] == 'R' and s[en] == 'R')
      st++;
    else if (s[st] == 'W' and s[en] == 'W')
      en--;
    else
      st++, en--;
  }
  cout << ans << endl;
}

int32_t main() {
  IO;

  int nT = 1;
  // cin >> nT;
  for (int i = 1; i <= nT; ++i)
    solve();
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define int long long
#define endl "\\n"
#define all(x) (x).begin(), (x).end()
const int MOD = 1e9 + 7;
const int mxN = 2e5 + 3;

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int st = 0;
  int en = n - 1;
  while (s[st] == 'R') {
    st++;
  }
  while (s[en] == 'W') {
    en--;
  }
  int ans = 0;
  while (st < en) {
    if (s[st] == 'W' and s[en] == 'R')
      ans++, st++, en--;
    else if (s[st] == 'R' and s[en] == 'R')
      st++;
    else if (s[st] == 'W' and s[en] == 'W')
      en--;
  }
  cout << ans << endl;
}

int32_t main() {
  IO;

  int nT = 1;
  // cin >> nT;
  for (int i = 1; i <= nT; ++i)
    solve();
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
#define REP(k, m, n) for (int(k) = (m); (k) < (n); (k)++)
#define rep(i, n) REP((i), 0, (n))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using tp3 = tuple<int, int, int>;
using Mat = vector<vector<ll>>;
constexpr int INF = 1 << 28;
constexpr ll INFL = 1ll << 60;
constexpr int dh[4] = {0, 1, 0, -1};
constexpr int dw[4] = {-1, 0, 1, 0};
bool isin(const int H, const int W, const int h, const int w) {
  return 0 <= h && h < H && 0 <= w && w < W;
}
// ============ template finished ============

vector<int> make_acc(const string &s, const char c) {
  const int N = s.size();
  vector<int> acc(N, 0);
  rep(i, N) { acc[i] = (i == 0 ? 0 : acc[i - 1]) + (s[i] == c ? 1 : 0); }
  return acc;
}

int count(const vector<int> &accr, const vector<int> &accw, const int redcnt) {
  const int N = accr.size();
  int wout = redcnt == 0 ? 0 : accw[redcnt - 1];
  int rout = redcnt == 0 ? accr.back() : accr.back() - accr[redcnt - 1];
  return max(wout, rout);
}

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

  auto accR = make_acc(s, 'R');
  auto accW = make_acc(s, 'W');

  int res = INF;
  rep(i, N + 1) { res = min(res, count(accR, accW, i)); }
  cout << res << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define REP(k, m, n) for (int(k) = (m); (k) < (n); (k)++)
#define rep(i, n) REP((i), 0, (n))
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using tp3 = tuple<int, int, int>;
using Mat = vector<vector<ll>>;
constexpr int INF = 1 << 28;
constexpr ll INFL = 1ll << 60;
constexpr int dh[4] = {0, 1, 0, -1};
constexpr int dw[4] = {-1, 0, 1, 0};
bool isin(const int H, const int W, const int h, const int w) {
  return 0 <= h && h < H && 0 <= w && w < W;
}
// ============ template finished ============

int count(const string &s, int redcnt) {
  const int N = s.size();
  int rout = 0, wout = 0;
  rep(i, N) {
    if (s[i] == 'R') {
      if (redcnt <= i)
        rout++;
    } else {
      if (i < redcnt)
        wout++;
    }
  }
  return max(rout, wout);
}

vector<int> make_acc(const string &s, const char c) {
  const int N = s.size();
  vector<int> acc(N, 0);
  rep(i, N) { acc[i] = (i == 0 ? 0 : acc[i - 1]) + (s[i] == c ? 1 : 0); }
  return acc;
}

int count(const vector<int> &accr, const vector<int> &accw, const int redcnt) {
  const int N = accr.size();
  int wout = redcnt == 0 ? 0 : accw[redcnt - 1];
  int rout = redcnt == 0 ? accr.back() : accr.back() - accr[redcnt - 1];
  return max(wout, rout);
}

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

  auto accR = make_acc(s, 'R');
  auto accW = make_acc(s, 'W');

  int redL = 0, redR = N;
  while (abs(redL - redR) > 3) {
    int redml = (redL * 2 + redR) / 3;
    int redmr = (redL + redR * 2) / 3;
    int vl = count(accR, accW, redml);
    int vr = count(accR, accW, redmr);
    if (vl < vr) {
      redR = vr;
    } else {
      redL = vl;
    }
  }

  int res = INF;
  REP(i, redL, redR + 1) { res = min(res, count(accR, accW, i)); }
  cout << res << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, a, b) for (int i = a; i >= b; --i)
#define mem0(x) memset(x, 0, sizeof(x))
#define meminf(x) memset(x, 0x3f, sizeof(x))
#define VI vector<int>
#define VL vector<ll>
using namespace std;
char stone[200005];
vector<int> posr;
int main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  int cntw = 0, cntr = 0;
  int lastr;
  rep(i, 1, n) {
    cin >> stone[i];
    if (stone[i] == 'W') {
      cntw++;
    } else {
      cntr++;
      posr.push_back(i);
    }
  }
  if (!cntw || !cntr) {
    cout << 0;
    return 0;
  }
  int step = 0;
  rep(i, 1, n) {

    if (stone[i] == 'W') {
      int flag = 0;
      if (*posr.rbegin() > i) {
        stone[i] = 'R';
        stone[posr[posr.size() - 1]] = 'W';
        posr.pop_back();
        ++step;
      } else {
        cout << step;
        return 0;
      }
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, a, b) for (int i = a; i >= b; --i)
#define mem0(x) memset(x, 0, sizeof(x))
#define meminf(x) memset(x, 0x3f, sizeof(x))
#define VI vector<int>
#define VL vector<ll>
using namespace std;
char stone[200005];
int main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  int cntw = 0, cntr = 0;
  rep(i, 1, n) {
    cin >> stone[i];
    if (stone[i] == 'W') {
      cntw++;
    } else {
      cntr++;
    }
  }
  if (!cntw || !cntr) {
    cout << 0;
    return 0;
  }
  int step = 0;
  rep(i, 1, n) {

    if (stone[i] == 'W') {
      int flag = 0;
      per(j, n, i + 1) {
        if (stone[j] == 'R') {
          stone[i] = 'R';
          stone[j] = 'W';
          flag = 1;
          ++step;
          break;
        }
      }
      if (!flag) {
        cout << step;
        break;
      }
    }
  }
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

// solution:
signed main() {
  int n, count, moves = 0;
  cin >> n, count = n;
  string s;
  cin >> s;
  for (int i = n - 1; ~i; --i)
    count -= (s[i] == 'W');
  for (int i = 0; i < count; ++i)
    moves += (s[i] == 'W');
  cout << moves << "\\n";
  return 0;
} // farewell, until we meet again..

Error: -11

Fixed submission:

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

// solution:
signed main() {
  int n, count = n, moves = 0;
  cin >> n;
  string s;
  cin >> s;
  for (int i = n - 1; ~i; --i)
    count -= (s[i] == 'W');
  for (int i = 0; i < count; ++i)
    moves += (s[i] == 'W');
  cout << moves << "\\n";
  return 0;
} // farewell, until we meet again..
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using llint = long long int;
using u64 = std::uint_fast64_t;

void solve(void) {

  int n;
  std::string C;
  std::cin >> n >> C;
  int leftW = 0;
  int rightR = n;

  int cnt = 0;

  while (true) {
    // std::cout << leftW << " " << rightR << std::endl;
    while (C[leftW] != 'W' && leftW < n - 1)
      leftW++;
    while (C[rightR - 1] != 'R' && rightR > 1)
      rightR--;
    if (leftW >= rightR - 1)
      break;

    C[leftW] = 'R';
    C[rightR - 1] = 'W';
    cnt++;
  }
  std::cout << cnt << std::endl;
}
int main(void) {
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using llint = long long int;
using u64 = std::uint_fast64_t;

void solve(void) {

  int n;
  std::string C;
  std::cin >> n >> C;
  int leftW = 0;
  int rightR = n;

  int cnt = 0;

  while (true) {
    // std::cout << leftW << " " << rightR << std::endl;
    while (C[leftW] != 'W')
      leftW++;
    while (C[rightR - 1] != 'R')
      rightR--;
    if (leftW >= rightR - 1)
      break;

    C[leftW] = 'R';
    C[rightR - 1] = 'W';
    cnt++;
  }
  std::cout << cnt << std::endl;
}
int main(void) {
  solve();
  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++)
#define put(i) cout << fixed << i << endl
using namespace std;
using ll = long long;

// yokuwakaranaidesune;;

int main() {
  ll n;
  cin >> n;
  vector<char> c(n);
  rep(i, n) cin >> c[i];

  ll r = 0, ans = 0;
  for (auto x : c)
    if (x == 'R')
      r++;
  rep(i, r) if (c[i] == 'W') ans++;

  put(ans);
}

Error: TLE

Fixed submission:

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

bool check(vector<char> a) {
  ll flag = 0;
  for (int i = 0; i < a.size(); i++) {
    if (a[i] == 'W')
      flag = 1;
    if (flag and a[i] == 'R') {
      return false;
    }
  }
  return true;
}

int main() {
  ll n;
  cin >> n;
  vector<char> c(n);
  rep(i, n) cin >> c[i];

  ll ans = 0;
  ll left = 0, right = n - 1;
  for (int i = 0;; i++) {
    if (check(c)) {
      put(ans);
      exit(0);
    }
    for (int j = left; j <= right and j < c.size(); j++) {
      if (c[j] == 'W') {
        left = j;
        break;
      }
    }
    for (int j = right; j > left and j > -1; j--) {
      if (c[j] == 'R') {
        right = j;
        break;
      }
    }
    swap(c[left], c[right]);
    ans++;
  }
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define pb push_back
#define fi first
#define se second

using namespace std;

int main() {
  int n;

  cin >> n;

  string frase;

  cin >> frase;

  int nr = 0, nw = 0;

  for (int i = 0; i < n; ++i) {
    if (frase[i] == 'R')
      nr++;
    if (frase[i] == 'W')
      nw++;
  }

  int nwe = 0;
  for (int i = 0; i < nr; ++i) {
    if (frase[i] == 'W')
      nwe++;
  }

  cout << nwe << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define pb push_back
#define fi first
#define se second

using namespace std;

int checkStr(string s) {

  if (s[0] == 'W')
    return 0;

  int sz = s.size();

  int flag1 = 1;

  for (int i = 0; i < sz; i++) {
    if (flag1) {
      if (s[i] == 'W') {
        flag1 = 0;
      }
    } else {
      if (s[i] == 'R') {
        return 0;
      }
    }
  }
  return 1;
}

int main() {

  int n;

  cin >> n;

  string s;

  cin >> s;

  long long counter = 0;

  int sz = s.size();

  int notW = 1;
  int notR = 1;

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

    if (s[i] == 'R') {
      notR = 0;
    }
  }

  if (notR || notW) {
    cout << 0 << endl;
    return 0;
  }

  while (!checkStr(s)) {
    int rPos, wPos;

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

    for (int i = sz - 1; i >= 0; i--) {
      if (s[i] == 'R') {
        rPos = i;
        i = -1;
      }
    }

    swap(s[rPos], s[wPos]);

    counter++;

    if (counter > 2000000) {
      cout << 0 << endl;
      return 0;
    }
  }

  cout << counter << endl;

  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 P = pair<int, int>;
using ll = long long;

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

  int rcnt = count(s.cbegin(), s.cend(), 'R');
  int wcnt = count(s.cbegin(), s.cbegin() + rcnt, 'W');
  cout << wcnt << 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 P = pair<int, int>;
using ll = long long;

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

  int w = s.find_first_of("W");
  int r = s.find_last_of("R");

  if (r == string::npos || w == string::npos) {
    cout << "0" << endl;
    return 0;
  }

  int ans = 0;
  while (w < r) {
    ++ans;
    s[r] = 'W';
    s[w] = 'R';

    w = s.find_first_of("W");
    r = s.find_last_of("R");
  }
  cout << ans << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
int main() {
  int n;
  cin >> n;
  vector<int> red;
  vector<int> white;
  for (int i = 0; i < n; i++) {
    char c;
    cin >> c;
    if (c == 'R') {
      red.push_back(i);
    }
    if (c == 'W') {
      white.push_back(i);
    }
  }
  reverse(red.begin(), red.end());
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (red.size() == 0 || white.size() == 0) {
      break;
    }
    if (red[0] > white[0]) {
      ans++;
      red.erase(red.begin());
      white.erase(white.begin());
    } else {
      break;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
int main() {
  ll n;
  cin >> n;
  vector<char> c(n);
  vector<ll> red;
  vector<ll> white;
  for (ll i = 0; i < n; i++) {
    cin >> c[i];
    if (c[i] == 'R') {
      red.push_back(i);
    }
    if (c[i] == 'W') {
      white.push_back(i);
    }
  }
  reverse(red.begin(), red.end());
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    if (red.size() == 0 || white.size() == 0) {
      break;
    }
    if (red[0] > white[0]) {
      ans++;
      red.erase(red.begin());
      white.erase(white.begin());
    } else {
      break;
    }
  }
  cout << ans << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <stack>

using namespace std;

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

  queue<long long> white;
  stack<long long> red;
  long long i = 0;
  for (string::iterator itr = s.begin(); itr != s.end(); itr++) {
    if (*itr == 'W')
      white.push(i);
    else
      red.push(i);
    i++;
  }
  long long count = 0;
  while (!white.empty() && !red.empty()) {
    long long w = white.front(), r = red.top();
    white.pop();
    red.pop();
    if (w > r)
      break;
    count++;
  }
  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

long long n;
bool ball[200000];

bool check() {
  bool last = ball[0];
  for (long long i = 1; i < n; i++) {
    if (!last && ball[i])
      return false;
    last = ball[i];
  }
  return true;
}

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

  long long i = 0;
  for (string::iterator itr = s.begin(); itr != s.end(); itr++) {
    if (*itr == 'R')
      ball[i] = true;
    else
      ball[i] = false;
    i++;
  }
  long long count = 0;
  while (!check()) {
    long long leftw = -1;
    long long rightr = -1;
    for (long long j = 0; j < n; j++) {
      if (ball[j]) {
        rightr = j;
      } else {
        if (leftw < 0) {
          leftw = j;
        }
      }
    }
    ball[leftw] = !ball[leftw];
    ball[rightr] = !ball[rightr];
    count++;
  }
  cout << count << endl;
  return 0;
}