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

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

int dp[2001][2001][2];
int main(void) {
  int M, N;
  string S, T;
  cin >> M >> N >> S >> T;

  // for(int i=0; i < M; i++) for(int j=0; j < N; j++) dp[i][j][1] = -INT_MAX;

  for (int i = 0; i < M; i++) {
    if (S[i] == 'I') {
      dp[i + 1][0][0] = max(dp[i + 1][0][0], dp[i][0][1] + 1);
    }
    if (S[i] == 'O') {
      if (dp[i][0][0] != 0) {
        dp[i + 1][0][1] = max(dp[i + 1][0][1], dp[i][0][0] + 1);
      }
    }
  }
  for (int j = 0; j < N; j++) {
    if (T[j] == 'I') {
      dp[0][j + 1][0] = max(dp[0][j + 1][0], dp[0][j][1] + 1);
    }
    if (T[j] == 'O') {
      if (dp[0][j][0] != 0) {
        dp[0][j + 1][1] = max(dp[0][j + 1][1], dp[0][j][0] + 1);
      }
    }
  }

  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      if (S[i] == 'I') {
        dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i][j + 1][1] + 1);
      }
      if (S[i] == 'O') {
        if (dp[i][j + 1][0] != 0) {
          dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][1], dp[i][j + 1][0] + 1);
        }
      }

      if (T[j] == 'I') {
        dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i + 1][j][1] + 1);
      }
      if (T[j] == 'O') {
        if (dp[i + 1][j][0] != 0) {
          dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][1], dp[i + 1][j][0] + 1);
        }
      }
    }
  }

  /*
  for(int j=0; j <= N; j++){
    printf("%7d ", j);
  }
  printf(" --->%s\\n", T.c_str());
  for(int i=0; i <= M; i++){
    for(int j=0; j <= N; j++){
      printf("(%2d,%2d)", dp[i][j][0], dp[i][j][1]);
      if(j != N) printf(" ");
    }
    printf("\\n");
  }
  */

  int ans = 0;

  for (int i = 0; i <= M; i++) {
    for (int j = 0; j <= N; j++) {
      ans = max(ans, dp[i][j][0]);
    }
  }

  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

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

int main(void) {
  int M, N;
  char S[2222], T[2222];
  int dp[2222][2222][2];
  // memset(dp, 0, 2001*2001*2*sizeof(int));
  for (int i = 0; i < 2001; i++) {
    for (int j = 0; j < 2001; j++) {
      for (int k = 0; k < 2; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  scanf("%d %d", &M, &N);
  scanf("%s %s", S, T);

  // for(int i=0; i < M; i++) for(int j=0; j < N; j++) dp[i][j][1] = -INT_MAX;

  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      if (S[i] == 'I')
        dp[i + 1][j + 1][0] = dp[i][j + 1][1] + 1;
      if (T[j] == 'I')
        dp[i + 1][j + 1][0] = max(dp[i + 1][j + 1][0], dp[i + 1][j][1] + 1);
      if (S[i] == 'O')
        if (dp[i][j + 1][0] != 0)
          dp[i + 1][j + 1][1] = dp[i][j + 1][0] + 1;
      if (T[j] == 'O')
        if (dp[i + 1][j][0])
          dp[i + 1][j + 1][1] = max(dp[i + 1][j + 1][1], dp[i + 1][j][0] + 1);
    }
  }

  cout << max(dp[M][N][0], dp[M][N][1]) << endl;

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

#include <algorithm>
#include <iostream>

using namespace std;

#define M_INF (-1) << 30

int dp[2010][2010][2];

int main(void) {
  int m, n, i, j, ans = 0, p, q, x, y;
  char s[2010], t[2010];
  cin >> m >> n;
  for (i = 0; i <= m; i++) {
    for (j = 0; j <= n; j++) {
      dp[i][j][0] = 0;
      dp[i][j][1] = M_INF;
    }
  }
  cin >> s;
  cin >> t;
  s[m] = 'O';
  t[n] = 'O';
  for (i = 0; i <= m; i++) {
    for (j = 0; j <= n; j++) {
      if (s[i] == 'O' && i < m) {
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][1] + 1);
      } else if (s[i] == 'I' && i < m) {
        dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][0] + 1);
      }
      if (t[j] == 'O' && j < n) {
        dp[i][j + 1][0] = max(dp[i][j + 1][0], dp[i][j][1] + 1);
      } else if (t[j] == 'I' && j < n) {
        dp[i][j + 1][1] = max(dp[i][j + 1][1], dp[i][j][0] + 1);
      }
    }
  }
  for (i = 0; i <= m; i++) {
    for (j = 0; j <= n; j++) {
      ans = max(dp[i][j][1], ans);
    }
  }
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main(void) {
  int m, n, i, j, dp[2010][2010][2], ans = 0, p, q, x, y;
  char s[2010], t[2010];
  for (i = 0; i < 2010; i++) {
    for (j = 0; j < 2010; j++) {
      dp[i][j][0] = 0;
      dp[i][j][1] = 0;
    }
  }
  cin >> m >> n;
  cin >> s;
  cin >> t;
  s[m] = '*';
  t[n] = '*';
  for (i = 0; i <= m; i++) {
    for (j = 0; j <= n; j++) {
      if (s[i] == 'O' && dp[i][j][1] != 0) {
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][1] + 1);
      }
      if (t[j] == 'O' && dp[i][j][1] != 0) {
        dp[i][j + 1][0] = max(dp[i][j + 1][0], dp[i][j][1] + 1);
      }
      if (s[i] == 'I') {
        dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][0] + 1);
      }
      if (t[j] == 'I') {
        dp[i][j + 1][1] = max(dp[i][j + 1][1], dp[i][j][0] + 1);
      }
    }
  }
  for (i = 0; i <= m; i++) {
    for (j = 0; j <= m; j++) {
      ans = max(dp[i][j][1], ans);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p00527 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#define max(a, b) a > b ? a : b
using namespace std;
int dp[2005][2005][2];
int main() {
  int n, m;
  int i, j, k;
  int a, b;
  string t[2];
  string sa = "IO";
  int s = 0;
  cin >> n >> m;
  for (i = 0; i < 2; i++) {
    cin >> t[i];
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= m; j++) {
      for (k = 0; k < 2; k++) {
        a = (i + j + k) % 2;
        if (a == 0 && dp[i][j][k] == 0) {
          if (t[0][i] == 'I')
            dp[i + 1][j][k] = max(dp[i + 1][j][k], 1);
          if (t[1][j] == 'I')
            dp[i][j + 1][k] = max(dp[i][j + 1][k], 1);
        }
        if (dp[i][j][k] == 0)
          continue;
        if (t[0][i] == sa[a]) {
          dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k] + 1);
        }
        if (t[1][j] == sa[a]) {
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k] + 1);
        }
      }
    }
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= m; j++) {
      for (k = 0; k < 2; k++) {
        if (dp[i][j][k] % 2 == 0)
          continue;
        s = max(s, dp[i][j][k]);
      }
    }
  }
  cout << s << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
#define max(a, b) a > b ? a : b
using namespace std;
int dp[1005][1005][2];
int main() {
  int n, m;
  int i, j, k;
  int a, b;
  string t[2];
  string sa = "IO";
  int s = 0;
  cin >> n >> m;
  for (i = 0; i < 2; i++) {
    cin >> t[i];
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= m; j++) {
      for (k = 0; k < 2; k++) {
        a = (i + j + k) % 2;
        if (a == 0 && dp[i][j][k] == 0) {
          if (t[0][i] == 'I')
            dp[i + 1][j][k] = max(dp[i + 1][j][k], 1);
          if (t[1][j] == 'I')
            dp[i][j + 1][k] = max(dp[i][j + 1][k], 1);
        }
        if (dp[i][j][k] == 0)
          continue;
        if (t[0][i] == sa[a]) {
          dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k] + 1);
        }
        if (t[1][j] == sa[a]) {
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k] + 1);
        }
      }
    }
  }
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= m; j++) {
      for (k = 0; k < 2; k++) {
        if (dp[i][j][k] % 2 == 0)
          continue;
        s = max(s, dp[i][j][k]);
      }
    }
  }
  cout << s << endl;
  return 0;
}
Problem p00527 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

int dp[2005][2005][2]; // a???n??????b???m????????§?????????(1,0)=(I,O)
int a, b;
string c, d;
int main() {
  cin >> a >> b >> c >> d;
  c += " ";
  d += " ";
  for (int e = a + b - 1; e >= 0; e--) {
    for (int x = 0; x <= a; x++) {
      int y = e - x;
      if (y >= 0 && y <= b) {
        int I = 0, O = 0;
        if (c[x] == 'I') {
          I = dp[x + 1][y][0] + 1;
        } else if (c[x] == 'O') {
          O = dp[x + 1][y][1] + 1;
        }
        if (d[y] == 'I') {
          I = max(I, dp[x][y + 1][0] + 1);
        } else if (d[y] == 'O') {
          O = max(O, dp[x][y + 1][1] + 1);
        }
        dp[x][y][1] = I;
        dp[x][y][0] = O;
      }
    }
  }
  int MAX = 0;
  for (int p = 0; p <= a; p++) {
    for (int q = 0; q <= b; q++) {
      if ((dp[p][q][1] & 1) == 0)
        dp[p][q][1]--;
      MAX = max(MAX, dp[p][q][1]);
    }
  }
  cout << MAX << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

int dp[2000][2000][2]; // a???n??????b???m????????§?????????(1,0)=(I,O)
int a, b;
string c, d;
int main() {
  cin >> a >> b >> c >> d;
  c += " ";
  d += " ";
  for (int e = a + b - 1; e >= 0; e--) {
    for (int x = 0; x <= a; x++) {
      int y = e - x;
      if (y >= 0 && y <= b) {
        int I = 0, O = 0;
        if (c[x] == 'I') {
          I = dp[x + 1][y][0] + 1;
        } else if (c[x] == 'O') {
          O = dp[x + 1][y][1] + 1;
        }
        if (d[y] == 'I') {
          I = max(I, dp[x][y + 1][0] + 1);
        } else if (d[y] == 'O') {
          O = max(O, dp[x][y + 1][1] + 1);
        }
        dp[x][y][1] = I;
        dp[x][y][0] = O;
      }
    }
  }
  int MAX = 0;
  for (int p = 0; p <= a; p++) {
    for (int q = 0; q <= b; q++) {
      if ((dp[p][q][1] & 1) == 0)
        dp[p][q][1]--;
      MAX = max(MAX, dp[p][q][1]);
    }
  }
  cout << MAX << endl;
}
Problem p00528 (C++) — buggy submission, verdict: Time Limit Exceeded

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

ll d[200000][2];
int x[200000], y[200000];
vector<int> X[100000], Y[100000];
struct st {
  int p;
  bool b;
  ll c;
};
bool operator<(st a, st b) { return a.c > b.c; }
int main() {
  int m, n, k;
  scanf("%d%d%d", &m, &n, &k);
  priority_queue<st> que;
  memset(d, 0x3f, sizeof(d));
  rep(i, k) {
    scanf("%d%d", &x[i], &y[i]);
    x[i]--;
    y[i]--;
    X[x[i]].push_back(i);
    Y[y[i]].push_back(i);
    if (x[i] == 0) {
      d[i][1] = y[i];
      que.push({i, 1, d[i][1]});
    }
  }
  while (!que.empty()) {
    st p = que.top();
    que.pop();
    if (d[p.p][p.b] != p.c)
      continue;
    if (p.b) {
      for (int u : Y[y[p.p]]) {
        if (d[u][0] > p.c + abs(x[p.p] - x[u]) + 1) {
          d[u][0] = p.c + abs(x[p.p] - x[u]) + 1;
          que.push({u, 0, d[u][0]});
        }
      }
    } else {
      for (int u : X[x[p.p]]) {
        if (d[u][1] > p.c + abs(y[p.p] - y[u]) + 1) {
          d[u][1] = p.c + abs(y[p.p] - y[u]) + 1;
          que.push({u, 1, d[u][1]});
        }
      }
    }
  }
  ll Min = INF;
  for (int u : X[m - 1])
    Min = min(Min, d[u][1] + abs(y[u] - n + 1));
  for (int u : Y[n - 1])
    Min = min(Min, d[u][0] + abs(x[u] - m + 1));
  if (Min == INF)
    puts("-1");
  else
    printf("%lld\\n", Min);
}

Error: TLE

Fixed submission:

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

int d[200000][2];
int x[200000], y[200000], lenx[100000], leny[100000];
vector<int> X[100000], Y[100000];
struct st {
  int p;
  bool b;
  int c;
};
bool operator<(st a, st b) { return a.c > b.c; }
int main() {
  int m, n, k;
  scanf("%d%d%d", &m, &n, &k);
  rep(i, m) X[i] = vector<int>(n);
  rep(i, n) Y[i] = vector<int>(m);

  priority_queue<st> que;
  memset(d, 0x3f, sizeof(d));
  rep(i, k) {
    scanf("%d%d", &x[i], &y[i]);
    x[i]--;
    y[i]--;
    X[x[i]][lenx[x[i]]++] = i;
    Y[y[i]][leny[y[i]]++] = i;
    if (x[i] == 0) {
      d[i][1] = y[i];
      d[i][0] = y[i] + 1;
      que.push({i, 1, d[i][1]});
    }
  }
  while (!que.empty()) {
    st p = que.top();
    que.pop();
    if (d[p.p][p.b] != p.c)
      continue;
    rep(i, lenx[x[p.p]]) {
      int u = X[x[p.p]][i];
      if (d[u][1] > p.c + abs(y[p.p] - y[u]) + !p.b) {
        d[u][1] = p.c + abs(y[p.p] - y[u]) + !p.b;
        que.push({u, 1, d[u][1]});
        d[u][0] = min(d[u][0], d[u][1] + 1);
      }
    }
    rep(i, leny[y[p.p]]) {
      int u = Y[y[p.p]][i];
      if (d[u][0] > p.c + abs(x[p.p] - x[u]) + p.b) {
        d[u][0] = p.c + abs(x[p.p] - x[u]) + p.b;
        que.push({u, 0, d[u][0]});
        d[u][1] = min(d[u][1], d[u][0] + 1);
      }
    }
  }
  int Min = INF;
  rep(i, lenx[m - 1]) {
    int u = X[m - 1][i];
    Min = min(Min, d[u][1] + abs(y[u] - n + 1));
  }
  rep(i, leny[n - 1]) {
    int u = Y[n - 1][i];
    Min = min(Min, d[u][0] + abs(x[u] - m + 1));
  }
  if (Min == INF)
    puts("-1");
  else
    printf("%d\\n", Min);
}
Problem p00528 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define LL long long
const LL inf = 1e15;

struct edge {
  int n, ty;
  LL cost;
  bool operator<(const edge &e1) const { return e1.cost < cost; }
};

int M, N, K;
int z[200010][2];
LL D[200010][2];
vector<int> G[100010][2];
int main() {
  cin >> M >> N >> K;
  for (int i = 0; i <= K; i++) {
    D[i][0] = inf;
    D[i][1] = inf;
  }
  z[0][0] = z[0][1] = 1;
  for (int i = 1; i <= K; i++) {
    cin >> z[i][0] >> z[i][1];
    G[z[i][0]][0].push_back(i);
    G[z[i][1]][1].push_back(i);
  }
  priority_queue<edge> pq;
  pq.push(edge{0, 0, 0});
  D[0][0] = 0;
  while (!pq.empty()) {
    edge e = pq.top();
    pq.pop();
    if (D[e.n][e.ty] < e.cost)
      continue;
    int inv = (e.ty + 1) & 1;
    for (auto &u : G[z[e.n][e.ty]][e.ty]) {
      LL c = e.cost + (LL)abs(z[e.n][inv] - z[u][inv]) + 1;
      if (D[u][inv] > c) {
        D[u][inv] = c;
        pq.push(edge{u, inv, c});
      }
    }
  }
  LL ans = inf;
  for (int i = 1; i <= K; i++) {
    if (z[i][0] == M) {
      ans = min(ans, min(D[i][0], D[i][1] + 1) + abs(N - z[i][1]));
    }
    if (z[i][1] == N) {
      ans = min(ans, min(D[i][0] + 1, D[i][1]) + abs(M - z[i][0]));
    }
  }
  cout << (ans == inf ? -1 : ans) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define LL long long
const LL inf = 1e15;

struct edge {
  int n, ty;
  LL cost;
  bool operator<(const edge &e1) const { return e1.cost < cost; }
};

int M, N, K;
LL z[200010][2];
LL D[200010][2];
vector<int> G[100010][2];
int main() {
  cin >> M >> N >> K;
  for (int i = 0; i <= K; i++) {
    D[i][0] = inf;
    D[i][1] = inf;
  }
  z[0][0] = z[0][1] = 1;
  for (int i = 1; i <= K; i++) {
    cin >> z[i][0] >> z[i][1];
    G[z[i][0]][0].push_back(i);
    G[z[i][1]][1].push_back(i);
  }
  priority_queue<edge> pq;
  pq.push(edge{0, 0, 0});
  D[0][0] = 0;
  while (!pq.empty()) {
    edge e = pq.top();
    pq.pop();
    if (D[e.n][e.ty] < e.cost)
      continue;
    int inv = (e.ty + 1) & 1;
    for (auto &u : G[z[e.n][e.ty]][e.ty]) {
      int c = e.cost + abs(z[e.n][inv] - z[u][inv]) + 1;
      if (D[u][inv] > c) {
        D[u][inv] = c;
        pq.push(edge{u, inv, c});
      }
    }
  }
  LL ans = inf;
  for (int i = 1; i <= K; i++) {
    if (z[i][0] == M) {
      ans = min(ans, min(D[i][0], D[i][1] + 1) + abs(N - z[i][1]));
    }
    if (z[i][1] == N) {
      ans = min(ans, min(D[i][0] + 1, D[i][1]) + abs(M - z[i][0]));
    }
  }
  cout << (ans == inf ? -1 : ans) << endl;
  return 0;
}
Problem p00529 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;
string S;
int n;
bool solve(int p) {
  int II = 0, OO = 0, RR = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (S[i] == 'I' && II < p)
      II++;
    else if (S[i] == 'O' && II > OO && OO < p)
      OO++;
    else if (RR < OO && S[i] != 'O')
      RR++;
  }
  if (RR == p)
    return true;
  return false;
}
int main() {
  cin >> n >> S;
  int L = 0, R = n, M;
  while (true) {
    M = (L + R) / 2;
    bool p1 = solve(M), p2 = solve(M + 1);
    if (p1 == true && p2 == false) {
      cout << M << endl;
      break;
    }
    if (p1 == false)
      R = M;
    if (p2 == true)
      L = M;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
string S;
int n;
bool solve(int p) {
  int II = 0, OO = 0, RR = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (S[i] == 'I' && II < p)
      II++;
    else if (S[i] == 'O' && II > OO && OO < p)
      OO++;
    else if (RR < OO && S[i] != 'O')
      RR++;
  }
  if (RR == p)
    return true;
  return false;
}
int main() {
  cin >> n >> S;
  for (int i = 1; i <= n; i++) {
    if (solve(i) == false) {
      cout << i - 1 << endl;
      break;
    }
  }
  return 0;
}
Problem p00529 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
string S;
vector<P> OI;
bool check(int h) {
  // cout << "h " << h << endl;
  int tk = S.size() - 1;
  if (OI.size() < h)
    return false;
  for (int i = 0; i < h; i++) {
    tk = min(tk, OI[i].second);
  }
  int res = 0;
  int H = h;
  h--;
  for (int i = 0; i < (int)S.size() && h > -1; i++) {
    if ((i < tk && S[i] == 'I') || S[i] == 'J') {
      // cout << i << " -> ";
      while (h > -1 && i >= OI[h].first)
        h--;
      if (h > -1) {
        // cout << OI[h].first << " -> " << OI[h].second << endl;
        res++;
        h--;
      }
    }
  }
  // cout << H << " "<< res << endl;
  return res == H;
}
int N;
int main() {
  cin >> N;
  cin >> S;
  int st = 0, ed = (int)S.size() / 3;
  int oc = S.size() - 1;
  for (int i = (int)S.size() - 1; i > -1; i--) {
    oc = min(oc, i - 1);
    if (S[i] == 'I') {
      P p = P(-1, i);
      for (; oc > -1; oc--) {
        if (S[oc] == 'O') {
          p.first = oc;
          oc--;
          break;
        }
      }
      if (p.first != -1)
        OI.push_back(p);
    }
  }
  // for(int i=0;i<OI.size();i++) cout << OI[i].first << ", "<<OI[i].second <<
  // endl;

  int res = 0;
  while (st <= ed) {
    int h = (st + ed) / 2;
    if (check(h)) {
      st = h + 1;
      res = h;
    } else {
      ed = h - 1;
    }
  }
  cout << res << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
string S;
vector<P> OI;
bool check(int h) {
  // cout << "h " << h << endl;
  int tk = S.size() - 1;
  for (int i = 0; i < h; i++) {
    tk = min(tk, OI[i].second);
  }
  int res = 0;
  int H = h;
  h--;
  for (int i = 0; i < (int)S.size() && h > -1; i++) {
    if ((i < tk && S[i] == 'I') || S[i] == 'J') {
      // cout << i << " -> ";
      while (h > -1 && i >= OI[h].first)
        h--;
      if (h > -1) {
        // cout << OI[h].first << " -> " << OI[h].second << endl;
        res++;
        h--;
      }
    }
  }
  // cout << H << " "<< res << endl;
  return res == H;
}
int N;
int main() {
  cin >> N;
  cin >> S;
  int st = 0, ed = (int)S.size() / 3;
  int oc = S.size() - 1;
  for (int i = (int)S.size() - 1; i > -1; i--) {
    oc = min(oc, i - 1);
    if (S[i] == 'I') {
      P p = P(-1, i);
      for (; oc > -1; oc--) {
        if (S[oc] == 'O') {
          p.first = oc;
          oc--;
          break;
        }
      }
      if (p.first != -1)
        OI.push_back(p);
    }
  }
  // for(int i=0;i<OI.size();i++) cout << OI[i].first << ", "<<OI[i].second <<
  // endl;

  int res = 0;
  while (st <= ed) {
    int h = (st + ed) / 2;
    if (check(h)) {
      st = h + 1;
      res = h;
    } else {
      ed = h - 1;
    }
  }
  cout << res << endl;
}
Problem p00529 (C++) — buggy submission, verdict: Runtime Error

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

int n;
string s;

bool check(int x) {
  int l, r;
  static bool isUseI[1000001], isUseO[1000001];
  int cntI = 0;

  for (r = 0; r < n; r++) {
    isUseI[r] = false;
    isUseO[r] = false;
  }

  for (r = n - 1; r >= 0; r--) {
    if (cntI == x) {
      break;
    }
    if (s[r] == 'I') {
      isUseI[r] = true;
      cntI++;
    }
  }
  if (cntI < x)
    return false;

  l = n - 1;
  for (r = n - 1; r >= 0; r--) {
    if (isUseI[r]) {
      bool f = false;
      for (l = min(l, r); l >= 0; l--) {
        if (s[l] == 'O') {
          isUseO[l] = true;
          l--;
          f = true;
          break;
        }
      }
      if (!f)
        return false;
    }
  }

  l = n - 1;
  for (r = n - 1; r >= 0; r--) {
    if (isUseO[r]) {
      bool f = false;
      for (l = min(l, r); l >= 0; l--) {
        if (s[l] == 'J' || (s[l] == 'I' && !isUseI[l])) {
          l--;
          f = true;
          break;
        }
      }
      if (!f)
        return false;
    }
  }
  return true;
}

int main() {
  int i;

  cin >> n;
  cin >> s;

  int st = 0, ed = n, medi;
  while (ed - st > 1) {
    medi = (st + ed) / 2;
    if (check(medi)) {
      st = medi;
    } else {
      ed = medi - 1;
    }
  }
  medi = (st + ed) / 2;
  if (check(medi + 1))
    cout << medi + 1 << endl;
  else
    cout << medi << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int n;
string s;

bool check(int x) {
  int l, r;
  static bool isUseI[1000001], isUseO[1000001];
  int cntI = 0;

  for (r = 0; r < n; r++) {
    isUseI[r] = false;
    isUseO[r] = false;
  }

  for (r = n - 1; r >= 0; r--) {
    if (cntI == x) {
      break;
    }
    if (s[r] == 'I') {
      isUseI[r] = true;
      cntI++;
    }
  }
  if (cntI < x)
    return false;

  l = n - 1;
  for (r = n - 1; r >= 0; r--) {
    if (isUseI[r]) {
      bool f = false;
      for (l = min(l, r); l >= 0; l--) {
        if (s[l] == 'O') {
          isUseO[l] = true;
          l--;
          f = true;
          break;
        }
      }
      if (!f)
        return false;
    }
  }

  l = n - 1;
  for (r = n - 1; r >= 0; r--) {
    if (isUseO[r]) {
      bool f = false;
      for (l = min(l, r); l >= 0; l--) {
        if (s[l] == 'J' || (s[l] == 'I' && !isUseI[l])) {
          l--;
          f = true;
          break;
        }
      }
      if (!f)
        return false;
    }
  }
  return true;
}

int main() {
  int i;

  cin >> n;
  for (i = 0; i < n; i++)
    cin >> s[i];

  int st = 0, ed = n, medi;
  while (ed - st > 1) {
    medi = (st + ed) / 2;
    if (check(medi)) {
      st = medi;
    } else {
      ed = medi - 1;
    }
  }
  medi = (st + ed) / 2;
  if (check(medi + 1))
    cout << medi + 1 << endl;
  else
    cout << medi << endl;
  return 0;
}
Problem p00529 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  string str;
  cin >> n;
  cin >> str;
  int I = 0, J = 0, O = 0, JO = 0, IOI = 0, JOI = 0;
  for (int i = 0; i < n; i++) {
    if (str[i] == 'O') {
      if (I - IOI + J > O)
        O++;
      if (J > JO)
        JO++;
    } else if (str[i] == 'I') {
      I++;
      if (O * J * JO)
        J--, JO--, O--, JOI++;
      else if (O * I)
        O--, IOI++;
    } else {
      J++;
    }
  }
  cout << JOI + min(IOI, (I - JOI) / 2) << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  while (1) {
    int n, j = 0, o = 0, i = 0, r = 0;
    cin >> n;
    char c;
    while (n--) {
      cin >> c;
      if (c == 'J')
        j++;
      if (c == 'O')
        o = min(o + 1, j);
      if (c == 'I') {
        if (o > 0 && j > 0) {
          j--, o--, r++;
        } else {
          j++;
        }
      }
    }
    cout << r << endl;
  }
}
Problem p00531 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int main() {
  int a, b, c, d, p, x, y;
  scanf("%d %d %d %d %d", &a, &b, &c, &d, &p);
  x = a * p;
  if (p <= c) {
    y = b;
  } else {
    y = (p - c) * d + b;
  }
  if (x <= y) {
    printf("%d\\n", x);
  } else {
    printf("%d\\n", y);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <stdio.h>
int main() {
  int a, b, c, d, p, x, y;
  scanf("%d %d %d %d %d", a, b, c, d, p);
  x = a * p;
  if (p <= c) {
    y = b;
  } else {
    y = (p - c) * d + b;
  }
  if (x <= y) {
    printf("%d\\n", x);
  } else {
    printf("%d\\n", y);
  }
  return 0;
}