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

#pragma GCC optimize(2)
#include <bits/stdc++.h>

namespace __temp_inbuilt {
int read(void) {
  int res = 0, sign = 1;
  char ch = std::getchar();
  while (!std::isdigit(ch)) {
    if (ch == '-')
      sign = -1;
    ch = std::getchar();
  }
  while (std::isdigit(ch))
    res = res * 10 + ch - 48, ch = std::getchar();
  return sign * res;
}

template <class Tp> void write(const Tp &x) {
  if (x > 9)
    write(x / 10);
  std::putchar(x % 10 + 48);
}
} // namespace __temp_inbuilt

using __temp_inbuilt::read;

template <class Tp> void writeln(const Tp &x) {
  if (x < 0)
    std::putchar('-');
  __temp_inbuilt::write(x > 0 ? x : -x);
  std::putchar('\\n');
}

template <class Tp> void writesp(const Tp &x) {
  if (x < 0)
    std::putchar('-');
  __temp_inbuilt::write(x > 0 ? x : -x);
  std::putchar(' ');
}

template <class Tp> void write(const Tp &x) {
  if (x < 0)
    std::putchar('-');
  __temp_inbuilt::write(x > 0 ? x : -x);
}

typedef long long ll;
typedef unsigned long long ull;
typedef double rl;
typedef long double RL;

const rl PI = 3.1415926535898, E = 2.71828182845904;

namespace contest {
int k;

int main() {
  k = read();
  int mod = 7 % k, ans = 1;
  if (!mod) {
    writeln(ans);
    return 0;
  }
  while (mod) {
    ++ans;
    mod = (mod * 10 + 7) % k;
    if (mod == 7 % k || ans >= 3e7) {
      writeln(-1);
      return 0;
    }
  }
  writeln(ans);
  return 0;
  return 0;
}
} // namespace contest

int main() {
  contest::main();
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#include <bits/stdc++.h>

namespace __temp_inbuilt {
int read(void) {
  int res = 0, sign = 1;
  char ch = std::getchar();
  while (!std::isdigit(ch)) {
    if (ch == '-')
      sign = -1;
    ch = std::getchar();
  }
  while (std::isdigit(ch))
    res = res * 10 + ch - 48, ch = std::getchar();
  return sign * res;
}

template <class Tp> void write(const Tp &x) {
  if (x > 9)
    write(x / 10);
  std::putchar(x % 10 + 48);
}
} // namespace __temp_inbuilt

using __temp_inbuilt::read;

template <class Tp> void writeln(const Tp &x) {
  if (x < 0)
    std::putchar('-');
  __temp_inbuilt::write(x > 0 ? x : -x);
  std::putchar('\\n');
}

template <class Tp> void writesp(const Tp &x) {
  if (x < 0)
    std::putchar('-');
  __temp_inbuilt::write(x > 0 ? x : -x);
  std::putchar(' ');
}

template <class Tp> void write(const Tp &x) {
  if (x < 0)
    std::putchar('-');
  __temp_inbuilt::write(x > 0 ? x : -x);
}

typedef long long ll;
typedef unsigned long long ull;
typedef double rl;
typedef long double RL;

const rl PI = 3.1415926535898, E = 2.71828182845904;

namespace contest {
int k;

int main() {
  k = read();
  int mod = 7 % k, ans = 1;
  if (!mod) {
    writeln(ans);
    return 0;
  }
  while (mod) {
    ++ans;
    mod = (mod * 10 + 7) % k;
    if (mod == 7 % k) {
      writeln(-1);
      return 0;
    }
  }
  writeln(ans);
  return 0;
  return 0;
}
} // namespace contest

int main() {
  contest::main();
  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
  int a[1000001];
  int K;
  cin >> K;
  a[1] = 7 % K;

  for (int i = 2; i <= K; i++) {
    a[i] = (a[i - 1] * 10 + 7) % K;
  }

  // for(int i=1;i<=K;i++){
  // cout<<a[i]<<" ";
  //}

  for (int i = 1; i <= K; i++) {
    if (a[i] == 0) {
      cout << i << endl;
      return 0;
    }
  }

  cout << -1 << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
  int K, c = 1, pre = 7;
  cin >> K;

  if (K % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }

  int T = K;
  while (T / 10 != 0) {
    c++;
    pre = pre * 10 + 7;
    T /= 10;
  }

  int n = c;
  while (pre % K != 0) {
    n++;
    pre = pre * 10 + 7;
  }

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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

// abcdefghijklmnopqrstuvwxyz
// ABCDEFGHIJKLMNOPQRSTUVWXYZ

typedef long long ll;
typedef long double lld;
typedef unsigned long long ull;
// ll INF = INT_MAX;
// ll mod = 1000000007;
// lld PI = 3.141592653;

// const int N = 2e5 + 5;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // ifstream cin("input.txt");

  int k;
  cin >> k;
  if (k % 2 == 0 || k % 5 == 0) {
    cout << "-1\\n";
    return 0;
  }
  if (k % 7 == 0)
    k /= 7;
  int t = 1;
  ll now = 10;
  now %= 9 * k;
  while (now != 1) {
    // cout<<now<<endl;
    now *= 10;
    t++;
    now %= 9 * k;
  }
  cout << t << "\\n";
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

// abcdefghijklmnopqrstuvwxyz
// ABCDEFGHIJKLMNOPQRSTUVWXYZ

typedef long long ll;
typedef long double lld;
typedef unsigned long long ull;
// ll INF = INT_MAX;
// ll mod = 1000000007;
// lld PI = 3.141592653;

// const int N = 2e5 + 5;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // ifstream cin("input.txt");

  int k;
  cin >> k;
  if (k % 2 == 0) {
    cout << "-1\\n";
    return 0;
  }
  if (k % 7 == 0)
    k /= 7;
  int t = 1;
  ll now = 10;
  now %= 9 * k;
  while (now != 1) {
    now *= 10;
    t++;
    now %= 9 * k;
  }
  cout << t << "\\n";
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll k;
  cin >> k;
  if (k % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }
  ll mod = 0;
  int ans = 0;
  map<ll, int> mp;
  while (true) {
    mod = (mod * 10 + 7) % k;
    ans++;
    if (mod == 0) {
      cout << ans << endl;
      return 0;
    }
    if (mp[mod]) {
      cout << -1 << endl;
      return 0;
    }
    mp[mod]++;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  ll k;
  cin >> k;
  if (k % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }
  ll mod = 0;
  int ans = 0;
  while (true) {
    mod = (mod * 10 + 7) % k;
    ans++;
    if (mod == 0) {
      cout << ans << endl;
      return 0;
    }
  }
}
Problem p02596 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
int dp[MAXN];
int main() {
  int k;
  cin >> k;
  dp[1] = 7 % k;
  if (dp[1] == 0)
    return cout << 1 << "\\n", 0;
  for (int i = 2; i < MAXN; i++) {
    dp[i] = (dp[1] + (dp[i - 1] * (10 % k)) % k) % k;
    if (dp[i] == 0)
      return cout << i << "\\n", 0;
  }
  return cout << -1 << "\\n", 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
int dp[MAXN];
int main() {
  int k;
  cin >> k;
  dp[1] = 7 % k;
  if (dp[1] == 0)
    return cout << 1 << "\\n", 0;
  for (int i = 2; i <= MAXN; i++) {
    dp[i] = (dp[1] + (dp[i - 1] * (10 % k)) % k) % k;
    if (dp[i] == 0)
      return cout << i << "\\n", 0;
  }
  return cout << -1 << "\\n", 0;
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int k;
  cin >> k;
  ll ans = 0;
  if (k % 2 == 0)
    cout << -1 << endl;
  else if (k % 5 == 0)
    cout << -1 << endl;
  else {
    ll num = 7, col = 1;
    while (num % k != 0) {
      ans++;
      num %= k;
      col *= 10;
      col %= k;
      num += 7 * col;
    }
    cout << ans + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int k;
  cin >> k;
  ll ans = 0;
  if (k % 2 == 0)
    cout << -1 << endl;
  else {
    ll num = 7, col = 1;
    while (num % k != 0) {
      ans++;
      num %= k;
      col *= 10;
      col %= k;
      num += 7 * col;
    }
    cout << ans + 1 << endl;
  }
  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;

int main() {
  ll K, sev = 0, rep = 0;
  cin >> K;
  if (K % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }

  while (rep <= K) {
    sev = sev * 10 + 7;
    sev = sev % K;
    rep++;
    if (sev == 0) {
      cout << rep << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;

int main() {
  ll K, sev = 0, rep = 0;
  cin >> K;
  if (K % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }

  while (sev <= K) {
    sev = sev * 10 + 7;
    sev = sev % K;
    rep++;
    if (sev == 0) {
      cout << rep << endl;
      return 0;
    }
  }
  cout << -1 << endl;
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define ri register
#define inf 0x7fffffff
#define E (1)
#define mk make_pair
#define int long long
#define double long double
using namespace std;
const int N = 1000010;
inline int read() {
  int s = 0, w = 1;
  ri char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = (s << 3) + (s << 1) + (ch - '0'), ch = getchar();
  return s * w;
}
void print(int x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}
int book[N];
signed main() {
  int K = read();
  int res = 0;
  for (ri int i = 1;; i++) {
    res = (res * 10 + 7) % K;
    if (!res) {
      printf("%lld\\n", i);
      break;
    }
    if (book[res]) {
      puts("-1");
      break;
    }
    book[res] = 1;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define ri register
#define inf 0x7fffffff
#define E (1)
#define mk make_pair
#define int long long
#define double long double
using namespace std;
const int N = 400010;
inline int read() {
  int s = 0, w = 1;
  ri char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = (s << 3) + (s << 1) + (ch - '0'), ch = getchar();
  return s * w;
}
void print(int x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    print(x / 10);
  putchar(x % 10 + '0');
}
signed main() {
  int K = read();
  if (!(K & 1ll) || (K % 5 == 0))
    puts("-1");
  int res = 0;
  for (ri int i = 1;; i++) {
    res = (res * 10 + 7) % K;
    if (!res) {
      printf("%lld\\n", i);
      break;
    }
  }
  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <fstream>
#include <iostream>

using namespace std;

int const NMAX = 1e6;
bool freq[1 + NMAX];

int main() {

  int n, m = 0, ans = 0;
  cin >> n;
  while (freq[m] == false) {
    freq[m] = true;
    m = (m * 10 + 7) % n;
    ans++;
    if (m == 0) {
      cout << ans;
      return 0;
    }
  }
  cout << -1;
  return 0;
}


Error: TLE

Fixed submission:

#include <fstream>
#include <iostream>

using namespace std;

int const NMAX = 1e6;
bool freq[1 + NMAX];

int main() {

  int n, m = 0, ans = 0;
  cin >> n;
  while (freq[m] == false) {
    m = (m * 10 + 7) % n;
    ans++;
    if (m == 0) {
      cout << ans;
      return 0;
    }
  }
  cout << -1;
  return 0;
}
Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
long long K, sum, mod = 7LL;
int main() {
  cin >> K;
  for (int i = 0; i < 10000000; mod *= 10LL, i++) {
    mod %= K;
    sum += mod;
    sum %= K;
    if (sum == 0LL) {
      cout << i + 1 << endl;
      return 0;
    }
    if (mod == 0LL) {
      cout << -1 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long K, sum, mod = 7LL;
int main() {
  cin >> K;
  for (int i = 0;; mod *= 10, i++) {
    mod %= K;
    sum += mod;
    sum %= K;
    if (sum == 0) {
      cout << i + 1 << endl;
      return 0;
    }
    if (mod == 0) {
      cout << -1 << endl;
      return 0;
    }
  }
  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 lower(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
using namespace std;
using ll = long long;
int INF = 1001001001;

int main() {
  int n;
  cin >> n;
  string c;
  cin >> c;
  int r, w;
  r = w = 0;
  rep(i, n) {
    if (c[i] == 'R')
      r++;
  }
  int ans = max(r, w);
  rep(i, n) {
    if (c[i] == 'R')
      r--;
    else
      w++;
    int m = max(r, w);
    ans = min(ans, m);
  }
  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 lower(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
using namespace std;
using ll = long long;
int INF = 1001001001;

int main() {
  int n;
  cin >> n;
  string c;
  int r, w;
  r = w = 0;
  rep(i, n) {
    cin >> c[i];
    if (c[i] == 'R')
      r++;
  }
  int ans = INF;
  rep(i, n) {
    if (c[i] == 'R')
      r--;
    else
      w++;
    int m = max(r, w);
    ans = min(ans, m);
  }
  cout << ans << endl;

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

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

typedef long int lint;
typedef long long int llint;

#define INF 1073741824
#define LINF 4611686018000000000
#define MOD 1000000007

int main() {
  int n;
  string ss;
  cin >> n >> ss;
  int ans = 0;
  int check = ss.size() - 1;
  int count = 0;
  for (int i = 0; i <= check; i++) {
    if (ss[i] == 'W') {
      bool ok = true;
      for (int j = check; j > i; j--) {
        count++;
        if (ss[j] == 'R') {
          check = j - 1;
          ans++;
          ok = false;
          break;
        }
      }
      if (ok)
        break;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long int lint;
typedef long long int llint;

#define INF 1073741824
#define LINF 4611686018000000000
#define MOD 1000000007

int main() {
  int n;
  string ss;
  cin >> n >> ss;
  int ans = 0;
  int check = ss.size() - 1;
  for (int i = 0; i <= check; i++) {
    if (ss[i] == 'W') {
      for (int j = check; j > i; j--) {
        if (ss[j] == 'R') {
          check = j - 1;
          ans++;
          break;
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<char> c(n);
  for (int i = 0; i < n; i++)
    cin >> c[i];

  int strt = 0;
  int counter = 0;
  vector<char>::iterator itr;
  int rnum = count(c.begin(), c.end(), 'R');
  itr = find(c.begin(), c.end(), 'R');
  while (true) {
    if (itr == c.end())
      break;
    if (distance(c.begin(), itr) >= rnum) {
      iter_swap(c.begin() + strt, itr);
      counter++;
      strt++;
    }
    itr = find(itr + 1, c.end(), 'R');
  }
  cout << counter;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<char> c(n);
  for (int i = 0; i < n; i++)
    cin >> c[i];

  int strt = 0;
  int counter = 0;
  vector<char>::iterator itr;
  while (strt <= n) {
    int rnum = count(c.begin(), c.end(), 'R');
    itr = find(c.begin() + strt, c.end(), 'R');
    if (itr == c.end())
      break;
    if (c.begin() + strt != itr && distance(c.begin(), itr) >= rnum) {
      iter_swap(c.begin() + strt, itr);
      counter++;
    }
    strt++;
  }
  cout << counter;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N;
  char c[200000];
  cin >> N;
  cin >> c;
  int rs = 0, ws = 0;
  rep(i, N) {
    if (c[i] == 'R')
      rs++;
    else
      ws++;
  }
  int diff = 0;
  for (int i = 0; i < rs; i++) {
    if (c[i] != 'R')
      diff++;
  }
  for (int i = rs; i < N; i++) {
    if (c[i] != 'W')
      diff++;
  }
  cout << diff / 2 << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int N;
  char c[20000];
  cin >> N;
  cin >> c;
  int rs = 0, ws = 0;
  rep(i, N) {
    if (c[i] == 'R')
      rs++;
    else
      ws++;
  }
  int diff = 0;
  for (int i = 0; i < rs; i++) {
    if (c[i] != 'R')
      diff++;
  }
  for (int i = rs; i < N; i++) {
    if (c[i] != 'W')
      diff++;
  }
  cout << diff / 2 << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define fr(i, n) for (i = 0; i < n; i++)
#define F first
#define S second
#define fast std::ios_base::sync_with_stdio(false)
#define mod 1000000007
#define pi 3.14159265
int main() {
  fast;
  lli n;
  cin >> n;
  string s;
  cin >> s;
  lli i, ans = mod, countr = 0, countw = 0;
  fr(i, s.length()) {
    if (s[i] == 'R')
      countr++;
    else
      countw++;
  }
  ans = min(countw, countr);
  if (ans == 0) {
    cout << 0;
    return 0;
  }
  lli count = 0, count1 = 0;
  fr(i, countr) {
    if (s[i] == 'W')
      count++;
  }
  for (i = s.length() - 1; i >= s.length() - countw; i--) {
    if (s[i] == 'R')
      count1++;
  }
  ans = min(ans, count);
  ans = min(ans, count1);
  cout << ans;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define fr(i, n) for (i = 0; i < n; i++)
#define F first
#define S second
#define fast std::ios_base::sync_with_stdio(false)
#define mod 1000000007
#define pi 3.14159265
int main() {
  fast;
  lli n;
  cin >> n;
  string s;
  cin >> s;
  lli i, ans = mod, countr = 0, countw = 0;
  fr(i, s.length()) {
    if (s[i] == 'R')
      countr++;
    else
      countw++;
  }
  ans = min(countw, countr);
  lli count = 0, count1 = 0;
  fr(i, countr) {
    if (s[i] == 'W')
      count++;
  }
  for (i = s.length() - 1; i >= s.length() - countw; i--) {
    if (s[i] == 'R')
      count1++;
  }
  ans = min(ans, count);
  ans = min(ans, count1);
  cout << ans;

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

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  ios::sync_with_stdio(false);
  char s[1000006], s2[1000006];
  int n, sum = 0, j;
  cin >> n >> s;
  int len = strlen(s);
  strcpy(s2, s);
  sort(s, s + len);
  for (int i = 0; i <= len; ++i) {
    if (s[i] != s2[i])
      sum++;
  }
  cout << sum / 2 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  ios::sync_with_stdio(false);
  string s;
  int n, sum = 0, j;
  cin >> n >> s;
  j = s.length();
  int i = 0;
  while (s[i++] != 'R')
    sum++;
  while (s[j--] != 'W')
    ;
  for (; i <= j; ++i) {
    if (s[i] == 'W')
      sum++;
  }
  if (sum == s.length())
    sum = 0;
  cout << sum << endl;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
string s;
int n;
int main() {
  cin >> n >> s;
  int w = 0, r = 0;
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == 'W')
      w++;
    // else if(s[i]=='R') r++;
  }
  int cnt = 0;
  // cout<<w<<endl;
  // cout<<n-w<<endl;
  for (int i = n - 1; i >= n - w; --i) {
    if (s[i] == 'R')
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string s;
int n;
int main() {
  cin >> n >> s;
  int w = 0, r = 0;
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == 'W')
      w++;
    else if (s[i] == 'R')
      r++;
  }
  int cnt = 0;
  // cout<<w<<endl;
  // cout<<n-w<<endl;
  for (int i = s.size() - 1; i >= s.size() - w; --i) {
    if (s[i] == 'R')
      cnt++;
  }
  cout << cnt << endl;
  return 0;
}
Problem p02597 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, cnt = 0, res = 0;
  string s;
  cin >> n >> s;
  int k = n - 1;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      cnt++;
  }
  while (s[k] != 'R' && k > 0)
    k--;
  for (int i = n - cnt; i < n; i++) {
    if (s[i] == 'R')
      res++;
  }
  cout << min(res, cnt + k - 1);
}

Error: WA

Fixed submission:

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

int main() {
  int n, cnt = 0, res = 0;
  string s;
  cin >> n >> s;
  int k = n - 1;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'W')
      cnt++;
  }
  while (s[k] != 'R' && k > 0)
    k--;
  for (int i = n - cnt - k; i < n; i++) {
    if (s[i] == 'R')
      res++;
  }
  cout << min(res, cnt + k - 1);
}
Problem p02597 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  int red = 0;
  int white = 0;
  vector<char> C(N);
  for (int i = 0; i < N; i++) {
    cin >> C.at(i);
    if (C.at(i) == 'R') {
      red++;
    } else {
      white++;
    }
  }

  int ans = red;
  int R = red;
  int L = 0;
  for (int i = 0; i < N; i++) {
    if (C.at(i) == 'R') {
      R--;
    } else {
      L++;
    }
    ans = min(ans, max(L, R));
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<char> L(N);
  for (int i = 0; i < N; i++) {
    cin >> L.at(i);
  }

  bool WR = true;
  int count = 0;
  while (WR == true) {
    for (int i = count; i < N - 1 - count; i++) {
      if (L.at(i) == 'W' && L.at(i + 1) == 'R') {
        for (int j = count; j < N - count; j++) {
          if (L.at(j) == 'W') {
            L.at(j) = 'R';
            break;
          }
        }
        for (int k = count; k < N - count; k++) {
          if (L.at(N - 1 - k) == 'R') {
            L.at(N - 1 - k) = 'W';
            break;
          }
        }
        count++;
        break;
      } else {
        if (i == N - 2 - count) {
          WR = false;
        }
      }
    }
  }
  cout << count << endl;
}