Problem p02652 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
template <class S, class T> inline S max_L(S a, T b) { return a >= b ? a : b; }
template <class T> inline void walloc1d(T **arr, int x, void **mem = &wmem) {
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void *)(((char *)(*mem)) + skip[((unsigned long long)(*mem)) & 15]);
  (*arr) = (T *)(*mem);
  (*mem) = ((*arr) + x);
}
template <class T1> void sortA_L(int N, T1 a[], void *mem = wmem) {
  sort(a, a + N);
}
template <class T1, class T2>
void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem) {
  int i;
  pair<T1, T2> *arr;
  walloc1d(&arr, N, &mem);
  for (i = (0); i < (N); i++) {
    arr[i].first = a[i];
    arr[i].second = b[i];
  }
  sort(arr, arr + N);
  for (i = (0); i < (N); i++) {
    a[i] = arr[i].first;
    b[i] = arr[i].second;
  }
}
inline int my_getchar_unlocked() {
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if (s == e && e == 1048576) {
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if (s == e) {
    return EOF;
  }
  return buf[s++];
}
inline void rd(char &c) {
  int i;
  for (;;) {
    i = my_getchar_unlocked();
    if (i != ' ' && i != '\\n' && i != '\\r' && i != '\\t' && i != EOF) {
      break;
    }
  }
  c = i;
}
inline int rd(char c[]) {
  int i;
  int sz = 0;
  for (;;) {
    i = my_getchar_unlocked();
    if (i != ' ' && i != '\\n' && i != '\\r' && i != '\\t' && i != EOF) {
      break;
    }
  }
  c[sz++] = i;
  for (;;) {
    i = my_getchar_unlocked();
    if (i == ' ' || i == '\\n' || i == '\\r' || i == '\\t' || i == EOF) {
      break;
    }
    c[sz++] = i;
  }
  c[sz] = '\\0';
  return sz;
}
struct MY_WRITER {
  char buf[1048576];
  int s;
  int e;
  MY_WRITER() {
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER() {
    if (s) {
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
};
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a) {
  if (MY_WRITER_VAR.s == MY_WRITER_VAR.e) {
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a) { my_putchar_unlocked(a); }
inline void wt_L(int x) {
  int s = 0;
  int m = 0;
  char f[10];
  if (x < 0) {
    m = 1;
    x = -x;
  }
  while (x) {
    f[s++] = x % 10;
    x /= 10;
  }
  if (!s) {
    f[s++] = 0;
  }
  if (m) {
    my_putchar_unlocked('-');
  }
  while (s--) {
    my_putchar_unlocked(f[s] + '0');
  }
}
template <class S> void arrErase(int k, int &sz, S a[]) {
  int i;
  sz--;
  for (i = (k); i < (sz); i++) {
    a[i] = a[i + 1];
  }
}
template <class S, class T> void arrErase(int k, int &sz, S a[], T b[]) {
  int i;
  sz--;
  for (i = (k); i < (sz); i++) {
    a[i] = a[i + 1];
  }
  for (i = (k); i < (sz); i++) {
    b[i] = b[i + 1];
  }
}
template <class S, class T, class U>
void arrErase(int k, int &sz, S a[], T b[], U c[]) {
  int i;
  sz--;
  for (i = (k); i < (sz); i++) {
    a[i] = a[i + 1];
  }
  for (i = (k); i < (sz); i++) {
    b[i] = b[i + 1];
  }
  for (i = (k); i < (sz); i++) {
    c[i] = c[i + 1];
  }
}
template <class S>
inline void arrInsert(const int k, int &sz, S a[], const S aval) {
  int i;
  sz++;
  for (i = sz - 1; i > k; i--) {
    a[i] = a[i - 1];
  }
  a[k] = aval;
}
template <class S, class T>
inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[],
                      const T bval) {
  int i;
  sz++;
  for (i = sz - 1; i > k; i--) {
    a[i] = a[i - 1];
  }
  for (i = sz - 1; i > k; i--) {
    b[i] = b[i - 1];
  }
  a[k] = aval;
  b[k] = bval;
}
template <class S, class T, class U>
inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[],
                      const T bval, U c[], const U cval) {
  int i;
  sz++;
  for (i = sz - 1; i > k; i--) {
    a[i] = a[i - 1];
  }
  for (i = sz - 1; i > k; i--) {
    b[i] = b[i - 1];
  }
  for (i = sz - 1; i > k; i--) {
    c[i] = c[i - 1];
  }
  a[k] = aval;
  b[k] = bval;
  c[k] = cval;
}
template <class S, class T, class U, class V>
inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[],
                      const T bval, U c[], const U cval, V d[], const V dval) {
  int i;
  sz++;
  for (i = sz - 1; i > k; i--) {
    a[i] = a[i - 1];
  }
  for (i = sz - 1; i > k; i--) {
    b[i] = b[i - 1];
  }
  for (i = sz - 1; i > k; i--) {
    c[i] = c[i - 1];
  }
  for (i = sz - 1; i > k; i--) {
    d[i] = d[i - 1];
  }
  a[k] = aval;
  b[k] = bval;
  c[k] = cval;
  d[k] = dval;
}
int N;
char S[1000000 + 2];
int ds;
int dp0[10000];
int dp1[10000];
int ns;
int nx0[10000];
int nx1[10000];
int main() {
  wmem = memarr;
  int i;
  int j;
  int k;
  int res;
  N = rd(S);
  int Lj4PdHRW;
  int KL2GvlyY;
  int Q5VJL1cS;
  Lj4PdHRW = 1;
  KL2GvlyY = N;
  while (Lj4PdHRW < KL2GvlyY) {
    if ((Lj4PdHRW + KL2GvlyY) % 2 == 0) {
      Q5VJL1cS = (Lj4PdHRW + KL2GvlyY) / 2;
    } else {
      Q5VJL1cS = (Lj4PdHRW + KL2GvlyY - 1) / 2;
    }
    ds = 1;
    dp0[0] = dp1[0] = 0;
    for (k = (0); k < (N); k++) {
      ns = 0;
      for (i = (0); i < (ds); i++) {
        if (S[k] == '0' || S[k] == '?') {
          nx0[ns] = max_L(0, dp0[i] + 1);
          nx1[ns] = max_L(0, dp1[i] - 1);
          ns++;
        }
        if (S[k] == '1' || S[k] == '?') {
          nx0[ns] = max_L(0, dp0[i] - 1);
          nx1[ns] = max_L(0, dp1[i] + 1);
          ns++;
        }
      }
      ds = 0;
      for (i = (0); i < (ns); i++) {
        if (nx0[i] <= Q5VJL1cS && nx1[i] <= Q5VJL1cS) {
          dp0[ds] = nx0[i];
          dp1[ds] = nx1[i];
          ds++;
        }
      }
      sortA_L(ds, dp0, dp1);
      j = 0;
      for (i = (0); i < (ds); i++) {
        if (j && dp1[i] >= dp1[j - 1]) {
          continue;
        }
        dp0[j] = dp0[i];
        dp1[j] = dp1[i];
        j++;
      }
      ds = j;
      if (ds >= 3 && dp0[1] + 2 < dp0[2]) {
        arrInsert(2, ds, dp0, dp0[1] + 2, dp1, dp1[1] - 2);
      }
      if (ds >= 3 && dp0[ds - 3] + 2 < dp0[ds - 2]) {
        arrInsert(ds - 2, ds, dp0, dp0[ds - 2] - 2, dp1, dp1[ds - 2] + 2);
      }
      while (ds > 6) {
        arrErase(3, ds, dp0, dp1);
      }
    }
    if (ds) {
      KL2GvlyY = Q5VJL1cS;
    } else {
      Lj4PdHRW = Q5VJL1cS + 1;
    }
  }
  res = KL2GvlyY;
  wt_L(res);
  wt_L('\\n');
  return 0;
}
// cLay varsion 20200509-1

// --- original code ---
// int N;
// char S[1d6+2];
//
// int ds, dp0[10000], dp1[10000];
// int ns, nx0[10000], nx1[10000];
//
// {
//   int i, j, k, res;
//   rd(S@N);
//   // N = 1d6;
//   // rep(i,N) S[i] = rand()%3 + '0';
//   // rep(i,N) if(S[i]=='2') S[i] = '?';
//
//   res = bsearch_min[int,x,1,N][
//     ds = 1;
//     dp0[0] = dp1[0] = 0;
//
//     rep(k,N){
//       ns = 0;
//       rep(i,ds){
//         if(S[k]=='0' || S[k]=='?'){
//           nx0[ns] = max(0, dp0[i]+1);
//           nx1[ns] = max(0, dp1[i]-1);
//           ns++;
//         }
//         if(S[k]=='1' || S[k]=='?'){
//           nx0[ns] = max(0, dp0[i]-1);
//           nx1[ns] = max(0, dp1[i]+1);
//           ns++;
//         }
//       }
//
//       ds = 0;
//       rep(i,ns) if(nx0[i] <= x && nx1[i] <= x){
//         dp0[ds] = nx0[i];
//         dp1[ds] = nx1[i];
//         ds++;
//       }
//
//       sortA(ds, dp0, dp1);
//       j = 0;
//       rep(i,ds){
//         if(j && dp1[i] >= dp1[j-1]) continue;
//         dp0[j] = dp0[i];
//         dp1[j] = dp1[i];
//         j++;
//       }
//       ds = j;
//
//       if(ds >= 3 && dp0[1] + 2 < dp0[2]) arrInsert(2, ds, dp0, dp0[1]+2, dp1,
//       dp1[1]-2); if(ds >= 3 && dp0[ds-3] +2 < dp0[ds-2]) arrInsert(ds-2, ds,
//       dp0, dp0[ds-2]-2, dp1, dp1[ds-2]+2); while(ds > 6) arrErase(3, ds, dp0,
//       dp1);
//
// /*      if(x==6){
//         wtF("{S[k]}");
//         rep(i,ds) wtF(" [{dp0[i]},{dp1[i]}]"); wt("");
//       }*/
//     }
//   ](ds);
//
//   wt(res);
// }


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
void *wmem;
char memarr[96000000];
template <class S, class T> inline S max_L(S a, T b) { return a >= b ? a : b; }
template <class T> inline void walloc1d(T **arr, int x, void **mem = &wmem) {
  static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
  (*mem) = (void *)(((char *)(*mem)) + skip[((unsigned long long)(*mem)) & 15]);
  (*arr) = (T *)(*mem);
  (*mem) = ((*arr) + x);
}
template <class T1> void sortA_L(int N, T1 a[], void *mem = wmem) {
  sort(a, a + N);
}
template <class T1, class T2>
void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem) {
  int i;
  pair<T1, T2> *arr;
  walloc1d(&arr, N, &mem);
  for (i = (0); i < (N); i++) {
    arr[i].first = a[i];
    arr[i].second = b[i];
  }
  sort(arr, arr + N);
  for (i = (0); i < (N); i++) {
    a[i] = arr[i].first;
    b[i] = arr[i].second;
  }
}
inline int my_getchar_unlocked() {
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if (s == e && e == 1048576) {
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if (s == e) {
    return EOF;
  }
  return buf[s++];
}
inline void rd(char &c) {
  int i;
  for (;;) {
    i = my_getchar_unlocked();
    if (i != ' ' && i != '\\n' && i != '\\r' && i != '\\t' && i != EOF) {
      break;
    }
  }
  c = i;
}
inline int rd(char c[]) {
  int i;
  int sz = 0;
  for (;;) {
    i = my_getchar_unlocked();
    if (i != ' ' && i != '\\n' && i != '\\r' && i != '\\t' && i != EOF) {
      break;
    }
  }
  c[sz++] = i;
  for (;;) {
    i = my_getchar_unlocked();
    if (i == ' ' || i == '\\n' || i == '\\r' || i == '\\t' || i == EOF) {
      break;
    }
    c[sz++] = i;
  }
  c[sz] = '\\0';
  return sz;
}
struct MY_WRITER {
  char buf[1048576];
  int s;
  int e;
  MY_WRITER() {
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER() {
    if (s) {
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
};
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a) {
  if (MY_WRITER_VAR.s == MY_WRITER_VAR.e) {
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a) { my_putchar_unlocked(a); }
inline void wt_L(int x) {
  int s = 0;
  int m = 0;
  char f[10];
  if (x < 0) {
    m = 1;
    x = -x;
  }
  while (x) {
    f[s++] = x % 10;
    x /= 10;
  }
  if (!s) {
    f[s++] = 0;
  }
  if (m) {
    my_putchar_unlocked('-');
  }
  while (s--) {
    my_putchar_unlocked(f[s] + '0');
  }
}
template <class S> void arrErase(int k, int &sz, S a[]) {
  int i;
  sz--;
  for (i = (k); i < (sz); i++) {
    a[i] = a[i + 1];
  }
}
template <class S, class T> void arrErase(int k, int &sz, S a[], T b[]) {
  int i;
  sz--;
  for (i = (k); i < (sz); i++) {
    a[i] = a[i + 1];
  }
  for (i = (k); i < (sz); i++) {
    b[i] = b[i + 1];
  }
}
template <class S, class T, class U>
void arrErase(int k, int &sz, S a[], T b[], U c[]) {
  int i;
  sz--;
  for (i = (k); i < (sz); i++) {
    a[i] = a[i + 1];
  }
  for (i = (k); i < (sz); i++) {
    b[i] = b[i + 1];
  }
  for (i = (k); i < (sz); i++) {
    c[i] = c[i + 1];
  }
}
template <class S>
inline void arrInsert(const int k, int &sz, S a[], const S aval) {
  int i;
  sz++;
  for (i = sz - 1; i > k; i--) {
    a[i] = a[i - 1];
  }
  a[k] = aval;
}
template <class S, class T>
inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[],
                      const T bval) {
  int i;
  sz++;
  for (i = sz - 1; i > k; i--) {
    a[i] = a[i - 1];
  }
  for (i = sz - 1; i > k; i--) {
    b[i] = b[i - 1];
  }
  a[k] = aval;
  b[k] = bval;
}
template <class S, class T, class U>
inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[],
                      const T bval, U c[], const U cval) {
  int i;
  sz++;
  for (i = sz - 1; i > k; i--) {
    a[i] = a[i - 1];
  }
  for (i = sz - 1; i > k; i--) {
    b[i] = b[i - 1];
  }
  for (i = sz - 1; i > k; i--) {
    c[i] = c[i - 1];
  }
  a[k] = aval;
  b[k] = bval;
  c[k] = cval;
}
template <class S, class T, class U, class V>
inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[],
                      const T bval, U c[], const U cval, V d[], const V dval) {
  int i;
  sz++;
  for (i = sz - 1; i > k; i--) {
    a[i] = a[i - 1];
  }
  for (i = sz - 1; i > k; i--) {
    b[i] = b[i - 1];
  }
  for (i = sz - 1; i > k; i--) {
    c[i] = c[i - 1];
  }
  for (i = sz - 1; i > k; i--) {
    d[i] = d[i - 1];
  }
  a[k] = aval;
  b[k] = bval;
  c[k] = cval;
  d[k] = dval;
}
int N;
char S[1000000 + 2];
int ds;
int dp0[10000];
int dp1[10000];
int ns;
int nx0[10000];
int nx1[10000];
int main() {
  wmem = memarr;
  int i;
  int j;
  int k;
  int res;
  N = rd(S);
  int Lj4PdHRW;
  int KL2GvlyY;
  int Q5VJL1cS;
  Lj4PdHRW = 1;
  KL2GvlyY = N;
  while (Lj4PdHRW < KL2GvlyY) {
    if ((Lj4PdHRW + KL2GvlyY) % 2 == 0) {
      Q5VJL1cS = (Lj4PdHRW + KL2GvlyY) / 2;
    } else {
      Q5VJL1cS = (Lj4PdHRW + KL2GvlyY - 1) / 2;
    }
    ds = 1;
    dp0[0] = dp1[0] = 0;
    for (k = (0); k < (N); k++) {
      ns = 0;
      for (i = (0); i < (ds); i++) {
        if (S[k] == '0' || S[k] == '?') {
          nx0[ns] = max_L(0, dp0[i] + 1);
          nx1[ns] = max_L(0, dp1[i] - 1);
          ns++;
        }
        if (S[k] == '1' || S[k] == '?') {
          nx0[ns] = max_L(0, dp0[i] - 1);
          nx1[ns] = max_L(0, dp1[i] + 1);
          ns++;
        }
      }
      ds = 0;
      for (i = (0); i < (ns); i++) {
        if (nx0[i] <= Q5VJL1cS && nx1[i] <= Q5VJL1cS) {
          dp0[ds] = nx0[i];
          dp1[ds] = nx1[i];
          ds++;
        }
      }
      for (;;) {
        int fg = 0;
        for (i = (0); i < (ds); i++) {
          for (j = (0); j < (ds); j++) {
            if (i != j && dp0[i] >= dp0[j] && dp1[i] >= dp1[j]) {
              arrErase(i, ds, dp0, dp1);
              fg++;
            }
          }
        }
        if (!fg) {
          break;
        }
      }
      sortA_L(ds, dp0, dp1);
      if (ds >= 3 && dp0[1] + 2 < dp0[2]) {
        arrInsert(2, ds, dp0, dp0[1] + 2, dp1, dp1[1] - 2);
      }
      if (ds >= 3 && dp0[ds - 3] + 2 < dp0[ds - 2]) {
        arrInsert(ds - 2, ds, dp0, dp0[ds - 2] - 2, dp1, dp1[ds - 2] + 2);
      }
      while (ds > 6) {
        arrErase(3, ds, dp0, dp1);
      }
    }
    if (ds) {
      KL2GvlyY = Q5VJL1cS;
    } else {
      Lj4PdHRW = Q5VJL1cS + 1;
    }
  }
  res = KL2GvlyY;
  wt_L(res);
  wt_L('\\n');
  return 0;
}
// cLay varsion 20200509-1

// --- original code ---
// int N;
// char S[1d6+2];
//
// int ds, dp0[10000], dp1[10000];
// int ns, nx0[10000], nx1[10000];
//
// {
//   int i, j, k, res;
//   rd(S@N);
//
//   res = bsearch_min[int,x,1,N][
//     ds = 1;
//     dp0[0] = dp1[0] = 0;
//
//     rep(k,N){
//       ns = 0;
//       rep(i,ds){
//         if(S[k]=='0' || S[k]=='?'){
//           nx0[ns] = max(0, dp0[i]+1);
//           nx1[ns] = max(0, dp1[i]-1);
//           ns++;
//         }
//         if(S[k]=='1' || S[k]=='?'){
//           nx0[ns] = max(0, dp0[i]-1);
//           nx1[ns] = max(0, dp1[i]+1);
//           ns++;
//         }
//       }
//
//       ds = 0;
//       rep(i,ns) if(nx0[i] <= x && nx1[i] <= x){
//         dp0[ds] = nx0[i];
//         dp1[ds] = nx1[i];
//         ds++;
//       }
//
//       for(;;){
//         int fg = 0;
//         rep(i,ds) rep(j,ds) if(i != j && dp0[i] >= dp0[j] && dp1[i] >=
//         dp1[j]){
//           arrErase(i, ds, dp0, dp1);
//           fg++;
//         }
//         if(!fg) break;
//       }
//
//       sortA(ds, dp0, dp1);
//       if(ds >= 3 && dp0[1] + 2 < dp0[2]) arrInsert(2, ds, dp0, dp0[1]+2, dp1,
//       dp1[1]-2); if(ds >= 3 && dp0[ds-3] +2 < dp0[ds-2]) arrInsert(ds-2, ds,
//       dp0, dp0[ds-2]-2, dp1, dp1[ds-2]+2); while(ds > 6) arrErase(3, ds, dp0,
//       dp1);
//
// /*      if(x==6){
//         wtF("{S[k]}");
//         rep(i,ds) wtF(" [{dp0[i]},{dp1[i]}]"); wt("");
//       }*/
//     }
//   ](ds);
//
//   wt(res);
// }