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

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

int main(void) {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int w = 0, r = 0;
  int p[N]; // W:0, R:1
  rep(i, N) {
    if (S[i] == 'W') {
      w++;
      p[i] = 0;
    } else {
      r++;
      p[i] = 1;
    }
  }
  int ans = min(w, r);
  if (ans == 0) {
    cout << ans << endl;
    return 0;
  }

  int k = 0;
  int lw = 0, rr = N - 1;
  while (1) {
    while (lw < N) {
      if (p[lw] == 0)
        break;
      lw++;
    }

    while (rr >= 0) {
      if (p[rr] == 1)
        break;
      rr--;
    }

    if (rr < lw)
      break;

    p[lw] = 1;
    p[rr] = 0;
    k++;
  }

  ans = min(ans, k);
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main(void) {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int w = 0, r = 0;
  int p[N]; // W:0, R:1
  rep(i, N) {
    if (S[i] == 'W') {
      w++;
      p[i] = 0;
    } else {
      r++;
      p[i] = 1;
    }
  }
  int ans = min(w, r);
  if (ans == 0) {
    cout << ans << endl;
    return 0;
  }

  int k = 0;
  while (1) {
    int lw, rr;
    rep(i, N) {
      if (p[i] == 0) {
        lw = i;
        break;
      }
    }

    for (int i = N - 1; i >= 0; i--) {
      if (p[i] == 1) {
        rr = i;
        break;
      }
    }

    if (rr < lw)
      break;
    p[lw] = 1;
    p[rr] = 0;
    k++;
  }

  ans = min(ans, k);
  cout << ans << endl;

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

//
// AtCoder Beginner Contest 174
// D
//

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SIZE(x) (int)(x).size()
#define SIZEL(x) (ll)(x).size()
#define MSG(a) cout << #a << " " << a << endl;

using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;

const double PI = 3.14159265358979323846;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }           // 最小公倍数

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

  vector<char> c(n);
  int rCnt = 0;
  int wCnt = 0;
  REP(i, n) {
    cin >> c[i];
    if (c[i] == 'R')
      rCnt++;
    if (c[i] == 'W')
      wCnt++;
  }
  int ans = 0;

  int rEnd = rCnt;
  REP(i, rCnt) {
    if (c[i] == 'W') {
      FOR(j, rEnd, n - 1) {
        if (c[j] == 'R') {
          swap(c[i], c[j]);
          ans++;
          rEnd = j + 1;
          break;
        }
      }
    }
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

//
// AtCoder Beginner Contest 174
// D
//

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <vector>

using namespace std;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SIZE(x) (int)(x).size()
#define SIZEL(x) (ll)(x).size()
#define MSG(a) cout << #a << " " << a << endl;

using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;

const double PI = 3.14159265358979323846;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }           // 最小公倍数

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

  vector<char> c(n);
  int rCnt = 0;
  int wCnt = 0;
  REP(i, n) {
    cin >> c[i];
    if (c[i] == 'R')
      rCnt++;
    if (c[i] == 'W')
      wCnt++;
  }
  int ans = 0;

  REP(i, rCnt) {
    if (c[i] == 'W') {
      FOR(j, rCnt, n - 1) {
        if (c[j] == 'R') {
          swap(c[i], c[j]);
          ans++;
          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;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0)
int main() {
  fastio;
  ll len;
  cin >> len;
  string s;
  cin >> s;
  int c = 0;
  ll j = len - 1;
  int i = 0;
  while (j > i) {

    while (s[i] == 'W' && s[j] == 'R' && j > i) {
      swap(s[i], s[j]);
      c++;
      i++;
      j--;
    }
    if (s[i] == 'R')
      i++;
    if (s[j] == 'W')
      j--;
  }
  cout << c << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0)
int main() {
  fastio;
  ll len;
  cin >> len;
  string s;
  cin >> s;
  int c = 0;
  ll j = len - 1;
  bool isW = false;
  for (int i = 0; i < len; i++) {
    // cout<<s[i]<<endl;
    if (s[i] == 'W') {
      // cout<<j<< " " << i<<endl;
      for (int r = j; r >= i; r--) {
        // cout<<s[r]<<endl;
        if (s[r] == 'R') {
          c++;
          s[i] = 'R';
          s[r] = 'W';
          j = r;
          break;
        }
      }
    }
    if (i > j)
      break;
    if (i > j && s[i] == 'W' && !isW) {
      isW = true;
    }
    if (isW && i > j && s[i] == 'R') {
      c++;
    }
  }
  cout << c << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 100000000;
#define rep1(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, k, n) for (ll i = k; i < (n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n;
  cin >> n;
  string c;
  cin >> c;
  vector<ll> a(n + 10, 0);
  rep2(i, 1, n + 1) {
    if (c[i - 1] == 'R')
      a[i] += a[i - 1] + 1;
    else
      a[i] += a[i - 1];
  }
  ll num = a[n];
  ll ans;
  if (num == 0)
    ans = 0;
  else
    ans = num - a[num];
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 100000000;
#define rep1(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, k, n) for (ll i = k; i < (n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n;
  cin >> n;
  string c;
  cin >> c;
  vector<ll> a(n, 0);
  rep1(i, n) {
    if ((i == 0) && (c[i] == 'R'))
      a[i]++;
    else {
      if (c[i] == 'R')
        a[i] += a[i - 1] + 1;
      else
        a[i] += a[i - 1];
    }
  }
  ll num = a[n - 1];
  ll ans;
  if (num == 0)
    ans = 0;
  else
    ans = num - a[num - 1];
  cout << ans << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  char str[N];
  cin >> str;
  int ans = 0;
  int count = 0;
  for (int i = 0; i < N; i++) {
    if (str[i] == 'R') {
      count++;
    }
  }
  for (int i = count; i < N; i++) {
    if (str[i] == 'R') {
      ans++;
    }
  }

  cout << ans << endl;

  /*int N = 3;
  vector<int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }*/

  /*for (int i = 0; i < vec.size(); i ++){
    cout << vec.at(i) << endl;
  }

  for (int i = 0; i < vec.size()-1; i ++){
    cout << vec.at(i) << " ";
  }
  cout << vec.at(vec.size() - 1) << endl;*/
}


Error: TLE

Fixed submission:

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

int judge(char str[], int N) {
  int a = 0;
  for (int i; i < N - 1; i++) {
    if (str[i] == 'W' && str[i + 1] == 'R') {
      a = 1;
      break;
    }
  }
  return a;
}

int main() {
  int N;
  cin >> N;
  char str[N];
  cin >> str;
  int ans = 0;
  while (judge(str, N) == 1) {
    for (int i = 0; i < N; i++) {
      if (str[i] == 'W') {
        str[i] = 'R';
        break;
      }
    }
    for (int i = 0; i < N; i++) {
      if (str[N - i - 1] == 'R') {
        str[N - i - 1] = 'W';
        break;
      }
    }
    ans++;
  }

  cout << ans << endl;

  /*int N = 3;
  vector<int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }*/

  /*for (int i = 0; i < vec.size(); i ++){
    cout << vec.at(i) << endl;
  }

  for (int i = 0; i < vec.size()-1; i ++){
    cout << vec.at(i) << " ";
  }
  cout << vec.at(vec.size() - 1) << endl;*/
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, ans = 0;
  cin >> n;
  vector<int> data(n);
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    data.at(i) = s.at(i);
  }
  sort(data.begin(), data.end()); // 小さい順に並べる
  for (int i = 0; i < n; i++) {
    if (data.at(i) != s.at(i)) {
      ans++;
    }
  }
  cout << ans / 2 << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, for_right = 0, ans = 0, sum_w = 0, sum_r = 0;
  cin >> n;
  vector<char> data(n);
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    data.at(i) = s.at(i);
    if (s.at(i) == 'W')
      sum_w++;
    if (s.at(i) == 'R')
      sum_r++;
  }
  for (int i = 0; i < n; i++) {
    bool flag = 1;
    for (int j = i; j < n; j++) {
      if (data.at(j) == 'R')
        flag = 0;
    }
    if (flag)
      break;
    if (data.at(i) == 'W') {
      for (int j = for_right; j < n - i; j++) {
        if (data.at(n - j - 1) == 'R') {
          data.at(n - j - 1) = 'W';
          data.at(i) = 'R';
          ans++;
          for_right++;
          break;
        }
        for_right++;
      }
      if (for_right == n - i - 2)
        break;
      if (data.at(i) == 'W') {
        data.at(i) = 'R';
        ans++;
      }
    }
  }
  if (ans < sum_w && ans < sum_r) {
    cout << ans << endl;
  } else if (sum_w < sum_r) {
    cout << sum_w << endl;
  } else {
    cout << sum_r << endl;
  }
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define EB emplace_back
#define int long long
#define F first
#define S second
#define N 500005
#define all(x) x.begin(), x.end()
#define endl '\\n'
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

using namespace std;

int32_t main() {
  IOS;
  int tt = 1;
  // cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int i = 0, j = n - 1;
    int cnt = 0;
    while (i < j) {
      while (s[j] != 'R' && j >= 0)
        j--;
      while (s[i] != 'W' && i < n)
        i++;
      if (i >= j)
        break;
      // cout << i << " " << j << endl;
      swap(s[i], s[j]);
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define EB emplace_back
#define int long long
#define F first
#define S second
#define N 500005
#define all(x) x.begin(), x.end()
#define endl '\\n'
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

using namespace std;

int32_t main() {
  IOS;
  int tt = 1;
  // cin >> tt;
  while (tt--) {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int i = 0, j = n - 1;
    int cnt = 0;
    while (i < j) {
      while (s[j] != 'R')
        j--;
      while (s[i] != 'W')
        i++;
      if (i >= j)
        break;
      // cout << i << " " << j << endl;
      swap(s[i], s[j]);
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int maxi = 2e5 + 5;

char a[maxi];

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<char> v;
  for (int i = 0; i < n; i++) {
    char c;
    cin >> c;
    v.pb(c);
  }
  int i, j;
  i = 0;
  j = v.size() - 1;
  int count = 0;
  while (i < j) {
    if (v[i] == 'R' && v[j] == 'R')
      i++;

    if (v[i] == 'W' && v[j] == 'R') {
      i++, j--;
      count++;

    } else if (v[i] == 'R' && v[j] == 'W')
      j--;
  }

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

#include <bits/stdc++.h>

using namespace std;
int n, cnt, ans;
string s;
int main() {
  scanf("%d", &n);
  cin >> s;
  for (int i = 0; i < n; i++)
    if (s[i] == 'W')
      cnt++;
  for (int i = n - cnt; i < n; i++)
    if (s[i] != 'W')
      ans++;
  printf("%d\\n", ans);
  return 0;
}

Error: WA

Fixed submission:

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

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

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

int main() {
  long long int N, R = 0, W = 0, count = 0;
  cin >> N;
  vector<char> c(N);
  queue<long long int> r, w;

  for (long long int i = 0; i < N; i++) {
    cin >> c.at(i);
    if (c.at(i) == 'R') {
      R++;
      r.push(i);
    } else {
      W++;
      w.push(i);
    }
    if (i == 0)
      continue;
    if (c.at(i - 1) == 'W' && c.at(i) == 'R') {
      count++;
    }
  }
  // cout << "complete0" << endl;
  if (count == 0) {
    cout << 0 << endl;
  } else if (w.empty() || r.empty()) {
    cout << 0 << endl;
  } else {
    vector<long long int> tmp(R);
    for (long long int i = R - 1; i > -1; i--) {
      tmp.at(i) = r.front();
      r.pop();
    }
    for (long long int i = 0; i < R; i++) {
      r.push(tmp.at(i));
    }

    // cout << "complete1" << endl;

    long long int w_min, r_max;
    w_min = w.front();
    r_max = r.front();
    count = 0;
    while (true) {
      if (w_min < r_max) {
        count++;

        w.pop();
        if (!w.empty()) {
          w_min = min(w.front(), r_max);
        } else
          break;

        r.pop();
        if (!r.empty()) {
          r_max = max(r.front(), w_min);
        } else
          break;

      } else {
        break;
      }
    }
    cout << count << endl;
  }
}


Error: 0

Fixed submission:

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

int main() {
  long long int N, R = 0, W = 0, count = 0;
  cin >> N;
  vector<char> c(N);
  queue<long long int> r, w;

  for (long long int i = 0; i < N; i++) {
    cin >> c.at(i);
    if (c.at(i) == 'R') {
      R++;
      r.push(i);
    } else {
      W++;
      w.push(i);
    }
    if (i == 0)
      continue;
    if (c.at(i - 1) == 'W' && c.at(i) == 'R') {
      count++;
    }
  }
  if (count == 0) {
    cout << 0 << endl;
  } else if (w.empty() || r.empty()) {
    cout << 0 << endl;
  } else {
    vector<long long int> tmp(W);
    for (long long int i = R - 1; i > -1; i--) {
      tmp.at(i) = r.front();
      r.pop();
    }
    for (long long int i = 0; i < R; i++) {
      r.push(tmp.at(i));
    }
    long long int w_min, r_max;
    w_min = w.front();
    r_max = r.front();
    count = 0;
    while (true) {
      if (w_min < r_max) {
        count++;

        w.pop();
        if (!w.empty()) {
          w_min = min(w.front(), r_max);
        } else
          break;

        r.pop();
        if (!r.empty()) {
          r_max = max(r.front(), w_min);
        } else
          break;

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

#include <bits/stdc++.h>
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout.precision(30);
#define ll long long
#define pi 3.14159265359
using namespace std;

int main() {
  IOS;
  int n;
  cin >> n;
  string s;
  cin >> s;
  int i = 0, j = n - 1;
  int c = 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]);
      c++;
    }
  }
  cout << c;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout.precision(30);
#define ll long long
#define pi 3.14159265359
using namespace std;

int main() {
  IOS;
  int n;
  cin >> n;
  string s;
  cin >> s;
  int i = 0, j = n - 1;
  int c = 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]);
      c++;
    }
  }
  cout << c;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

#define froop1(a, n) for (int i = a; i < n; i++)
#define froop2(a, n) for (int i = a; i > n; i--)

#define ll long long

#define pq(name, type) priority_queue<type> name
#define vec(name, type, length, value) vector<type> name(length, value)
#define vec2(name, type, length, width, value)                                 \\
  vector<vector<type>> name(length, vector<type>(width, value));

int main() {
  int S;
  string N;
  cin >> S >> N;
  int W = 0, R = 0;
  froop1(1, S) {
    if (N[i] == 'R')
      R++;
    else
      W++;
  }
  int count = 0, a = 0, b = R;
  count = max(a, b);
  if (N[0] == 'W')
    a++;
  froop1(1, S) {
    if (N[i] == 'R')
      b--;
    if (count > max(a, b)) {
      count = max(a, b);
    }
    if (N[i] == 'W')
      a++;
  }
  cout << count << endl;
  return 0;
}

Error: 0

Fixed submission:

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

#define froop1(a, n) for (int i = a; i < n; i++)
#define froop2(a, n) for (int i = a; i > n; i--)

#define ll long long

#define pq(name, type) priority_queue<type> name
#define vec(name, type, length, value) vector<type> name(length, value)
#define vec2(name, type, length, width, value)                                 \\
  vector<vector<type>> name(length, vector<type>(width, value));

int main() {
  int S;
  string N;
  cin >> S >> N;
  int mid = S / 2;
  int W = 0, R = 0, count = 0;
  froop1(0, mid) {
    if (N[i] == 'W') {
      W++;
    }
    if (N[S - 1 - i] == 'R') {
      R++;
    }
  }
  int a = max(W, R);
  count += a;
  for (int i = 0; a != 0; i++) {
    if (N[i] == 'W') {
      N[i] = 'R';
      a--;
    }
  }
  a = max(W, R);
  for (int i = S - 1; a != 0; i--) {
    if (N[i] == 'R') {
      N[i] = 'W';
      a--;
    }
  }
  int Wc = 0;
  froop1(0, S) {
    if (N[i] == 'W') {
      Wc++;
    } else {
      count += Wc;
      Wc = 0;
    }
  }

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using LP = pair<ll, ll>;
const double PI = 3.1415926535897932;

int main() {
  int N;
  cin >> N;
  string cn;
  cin >> cn;
  string mn = cn;
  sort(mn.begin(), mn.end());

  int cnt = 0;
  for (int i = 0; i < N; ++i) {
    if (mn[i] == 'R') {
      if (mn[i] != cn[i]) {
        cnt++;
      }
    } else {
      break;
    }
  }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using LP = pair<ll, ll>;
const double PI = 3.1415926535897932;

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

  int cnt = 0;
  int EP = N;
  for (int i = 0; i < EP; ++i) {
    if (cn[i] == 'W') {
      for (int j = EP; j > i; --j) {
        if (cn[j] == 'R') {
          cn[i] = 'R';
          cn[j] = 'W';
          cnt++;
          EP--;
          break;
        }
      }
    }
  }
  cout << cnt << endl;
  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 < (int)(n); i++)

int main() {
  int n;

  cin >> n;

  long long ans = 0, ans_2 = 0;

  vector<char> c(n);

  rep(i, n) { cin >> c.at(i); }

  int x = 0;
  int y = n - 1;

  while (1) {
    if (x > y)
      break;
    else if (c.at(x) == 'W' && c.at(y) == 'R') {
      ans++;
      x++;
      y--;
    } else if (c.at(x) == 'R') {
      x++;
    } else if (c.at(x) == 'W') {
      y--;
    }
  }
  cout << ans;
}


Error: TLE

Fixed submission:

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

int main() {
  int n;

  cin >> n;

  long long ans = 0, ans_2 = 0;

  vector<char> c(n);

  rep(i, n) { cin >> c.at(i); }

  int x = 0;
  int y = n - 1;

  while (1) {
    if (x > y)
      break;
    else if (c.at(x) == 'W' && c.at(y) == 'R') {
      ans++;
      x++;
      y--;
    } else if (c.at(x) == 'R' && c.at(y) == 'R') {
      x++;
    } else if (c.at(x) == 'W' && c.at(y) == 'W') {
      y--;
    }
  }
  cout << ans;
}