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

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

const int N = 1e6 + 5;

int n;
char s[N];
int sum[N], pmx[N], pmi[N];

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  sum[0] = 0;
  pmx[0] = 0;
  pmi[0] = 0;
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + 1 - 2 * (s[i] == '0');
    pmx[i] = max(pmx[i - 1], sum[i]);
    pmi[i] = min(pmi[i - 1], sum[i]);
  }
  int suf_mi = sum[n], suf_mx = sum[n];
  int ans = pmx[n] - pmi[n];
  for (int i = n; i; i--) {
    suf_mi = min(suf_mi, sum[i]);
    suf_mx = max(suf_mx, sum[i]);
    if (s[i] == '?') {
      if (pmx[i - 1] < suf_mx) {
        ans =
            min(ans, max(pmx[i - 1], suf_mx - 2) - min(pmi[i - 1], suf_mi - 2));
      }
      if (pmx[i - 1] + 1 < suf_mx) {
        suf_mx -= 2;
        suf_mi -= 2;
        ans = min(ans, max(pmx[i - 1], suf_mx) - min(pmi[i - 1], suf_mi));
      }
    }
  }
  suf_mi = sum[n], suf_mx = sum[n];
  bool found = false;
  for (int i = n; i; i--) {
    suf_mi = min(suf_mi, sum[i]);
    suf_mx = max(suf_mx, sum[i]);
    if (s[i] == '?') {
      if (pmx[i - 1] + 1 == suf_mx) {
        ans =
            min(ans, max(pmx[i - 1], suf_mx - 2) - min(pmi[i - 1], suf_mi - 2));
        if (!found) {
          suf_mi -= 2;
          suf_mx -= 2;
          found = true;
        }
      } else if (pmx[i - 1] + 1 < suf_mx) {
        suf_mx -= 2;
        suf_mi -= 2;
        ans = min(ans, max(pmx[i - 1], suf_mx) - min(pmi[i - 1], suf_mi));
      }
    }
  }
  printf("%d\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

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

const int N = 1e6 + 5;

int n;
char s[N];
int sum[N], pmx[N], pmi[N];

int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  sum[0] = 0;
  pmx[0] = 0;
  pmi[0] = 0;
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1] + 1 - 2 * (s[i] == '0');
    pmx[i] = max(pmx[i - 1], sum[i]);
    pmi[i] = min(pmi[i - 1], sum[i]);
  }
  int suf_mi = sum[n], suf_mx = sum[n];
  int ans = pmx[n] - pmi[n];
  for (int i = n; i; i--) {
    suf_mi = min(suf_mi, sum[i]);
    suf_mx = max(suf_mx, sum[i]);
    if (s[i] == '?') {
      if (pmx[i - 1] < suf_mx) {
        ans =
            min(ans, max(pmx[i - 1], suf_mx - 2) - min(pmi[i - 1], suf_mi - 2));
      }
      if (pmx[i - 1] + 1 < suf_mx) {
        suf_mx -= 2;
        suf_mi -= 2;
        ans = min(ans, max(pmx[i - 1], suf_mx) - min(pmi[i - 1], suf_mi));
      }
    }
  }
  suf_mi = sum[n], suf_mx = sum[n];
  bool found = false;
  for (int i = n; i; i--) {
    suf_mi = min(suf_mi, sum[i]);
    suf_mx = max(suf_mx, sum[i]);
    if (s[i] == '?') {
      if (pmx[i - 1] + 1 == suf_mx) {
        ans =
            min(ans, max(pmx[i - 1], suf_mx - 2) - min(pmi[i - 1], suf_mi - 2));
        if (!found) {
          suf_mi -= 2;
          suf_mx -= 2;
          found = true;
        }
      } else if (pmx[i - 1] + 1 < suf_mx) {
        assert(found);
        suf_mx -= 2;
        suf_mi -= 2;
        ans = min(ans, max(pmx[i - 1], suf_mx) - min(pmi[i - 1], suf_mi));
      }
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02652 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <iostream>
template <typename T> void fin(T const &t) {
  std::cout << t << std::endl;
  exit(0);
}
char buf[1000010];
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::cin >> buf;
  int a0 = 0, a1 = 1, b0 = 0, b1 = -1;
  int c0 = a0 - b0, c1 = a0 - b1, c2 = a1 - b0, c3 = a1 - b1;
  for (char *p = buf; *p; p++) {
    if (*p == '0') {
      --a0;
      --a1;
      --b0;
      --b1;
    } else if (*p == '1') {
      ++a0;
      ++a1;
      ++b0;
      ++b1;
    } else {
      --a0;
      --a1;
      ++b0;
      ++b1;
    }
    if (a0 < 0)
      a0 += 2;
    if (a1 < 0)
      a1 += 2;
    if (b0 > 0)
      b0 -= 2;
    if (b1 > 0)
      b1 -= 2;
    c0 = std::max(c0, a0 - b0);
    c1 = std::max(c1, a0 - b1);
    c2 = std::max(c2, a1 - b0);
    c3 = std::max(c3, a1 - b1);
  }
  fin(std::min(std::min(c0, c1), std::min(c2, c3)));
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
template <typename T> void fin(T const &t) {
  std::cout << t << std::endl;
  exit(0);
}
char buf[100010];
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  std::cin >> buf;
  int a0 = 0, a1 = 1, b0 = 0, b1 = -1;
  int c0 = a0 - b0, c1 = a0 - b1, c2 = a1 - b0, c3 = a1 - b1;
  for (char *p = buf; *p; p++) {
    if (*p == '0') {
      --a0;
      --a1;
      --b0;
      --b1;
    } else if (*p == '1') {
      ++a0;
      ++a1;
      ++b0;
      ++b1;
    } else {
      --a0;
      --a1;
      ++b0;
      ++b1;
    }
    if (a0 < 0)
      a0 += 2;
    if (a1 < 0)
      a1 += 2;
    if (b0 > 0)
      b0 -= 2;
    if (b1 > 0)
      b1 -= 2;
    c0 = std::max(c0, a0 - b0);
    c1 = std::max(c1, a0 - b1);
    c2 = std::max(c2, a1 - b0);
    c3 = std::max(c3, a1 - b1);
  }
  fin(std::min(std::min(c0, c1), std::min(c2, c3)));
  return 0;
}
Problem p02652 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
const int N = 1000005, M = 1000000007;
int n, i, j, ans, l, r, al, ar, sp;
char c[N];
void rt(int m, int l, int r) {
  for (i = strlen(c + 1); i >= 1; --i) {
    if (c[i] == '0') {
      --l;
      --r;
      if (l < 0)
        l += 2;
    }
    if (c[i] == '1') {
      ++l, ++r;
      if (r > m)
        r -= 2;
    }
    if (c[i] == '?') {
      --l;
      if (l < 0)
        l += 2;
      ++r;
      if (r > m)
        r -= 2;
    }
    if (l > r || l > m || r < 0) {
      al = 1;
      ar = 0;
      return;
    }
  }
  al = l, ar = r;
}
bool check(int m) {
  int i;
  int a[2][2], b[2][2];
  rt(m, 0, m / 2 * 2);
  a[0][0] = al, a[0][1] = ar;
  rt(m, 1, (m & 1 ? m : m - 1));
  a[1][0] = al, a[1][1] = ar;
  for (i = 1; c[i]; ++i)
    if (c[i] == '0')
      c[i] = '1';
    else if (c[i] == '1')
      c[i] = '0';
  rt(m, 0, m / 2 * 2);
  b[0][0] = m - ar, b[0][1] = m - al;
  rt(m, 1, (m & 1 ? m : m - 1));
  b[1][0] = m - ar, b[1][1] = m - al;
  if (m & 1) {
    swap(b[0][0], b[1][0]);
    swap(b[0][1], b[1][1]);
  }
  for (i = 1; c[i]; ++i)
    if (c[i] == '0')
      c[i] = '1';
    else if (c[i] == '1')
      c[i] = '0';
  if (min(a[0][1], b[0][1]) >= max(a[0][0], b[0][0]) ||
      min(a[1][1], b[1][1]) >= max(a[1][0], b[1][0]))
    return true;
  else
    return false;
}
int main() {
  scanf("%s", c + 1);
  int l = 1, r = strlen(c + 1);
  while (l < r) {
    int mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
const int N = 500005, M = 1000000007;
int n, i, j, ans, l, r, al, ar;
char c[N];
void rt(int m) {
  l = 0, r = m;
  for (i = strlen(c + 1); i >= 1; --i) {
    if (c[i] == '0') {
      --l;
      --r;
      if (l < 0)
        l = 0;
    }
    if (c[i] == '1') {
      ++l, ++r;
      if (r > m)
        r = m;
    }
    if (c[i] == '?') {
      if (l == r) {
        --l;
        ++r;
        if (l < 0)
          l += 2;
        if (r > m)
          r -= 2;
      } else {
        --l;
        ++r;
        if (l < 0)
          ++l;
        if (r > m)
          --r;
      }
    }
    if (l > r || l > m || r < 0) {
      al = 1;
      ar = 0;
      return;
    }
  }
  al = l, ar = r;
}
bool check(int m) {
  int i;
  rt(m);
  int bl = al, br = ar;
  for (i = 1; c[i]; ++i)
    if (c[i] == '0')
      c[i] = '1';
    else if (c[i] == '1')
      c[i] = '0';
  rt(m);
  for (i = 1; c[i]; ++i)
    if (c[i] == '0')
      c[i] = '1';
    else if (c[i] == '1')
      c[i] = '0';
  bl = m - bl;
  br = m - br;
  swap(bl, br);
  if (min(ar, br) >= max(al, bl))
    return true;
  else
    return false;
}
int main() {
  scanf("%s", c + 1);
  int l = 1, r = strlen(c + 1);
  while (l < r) {
    int mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l;
}
Problem p02652 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 1 << 20, mod = 1e9 + 7;
string s;
int n, ans = 1 << 30;
int p[maxn], sm[maxn], tt[maxn];
void solvep(string s) { // stuff with +
  p[0] = 0;
  int gm = 0;
  // cout << s << '\\n';
  for (int i = 1; i <= n; i++) {
    p[i] = p[i - 1] + (s[i] == '1' ? -1 : 1);
    gm = min(gm, p[i]);
    //	cout << p[i] << " ";
  }
  //	cout << '\\n';
  sm[n] = p[n];
  for (int i = n - 1; i >= 0; i--)
    sm[i] = min(p[i], sm[i + 1]);
  // for(int i = 0; i <= n; i++) cout << sm[i] << " ";

  // for(int i = 0; i <= n; i++) cout << p[i] << ' '; cout << '\\n';
  int r = 0;
  for (int i = 0; i <= n; i++) {
    sm[i] += r;
    p[i] += r;
    // cout << i << " " << sm[i] << " " << sm[i]-2 << " " << gm << '\\n';
    if (s[i] == '?' && sm[i] - 2 >= gm) {
      r -= 2;
      // cout <<"at " << i << '\\n';
      sm[i] -= 2;
      p[i] -= 2;
      s[i] = '1';
    }
    tt[i] = r;
    if (s[i] == '?')
      s[i] = '0';
    // cout << p[i] << "\\n";
  }
  // for(int i = 0; i <= n; i++) cout << p[i] << ' '; cout << '\\n';
  // cout << s << '\\n';
  int x = 0, y = 0;
  r = 0;
  for (auto i : s) {
    if (i == '1')
      r--;
    else if (i != '.')
      r++;
    x = min(x, r);
    y = max(y, r);
  }
  ans = min(ans, y - x);
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> s;
  n = s.size();
  s = "." + s;
  for (int i = 0; i < 2; i++) {
    memset(p, 0, sizeof p);
    memset(sm, 0, sizeof sm);
    solvep(s);
    memset(p, 0, sizeof p);
    memset(sm, 0, sizeof sm);
    for (auto &i : s)
      if (i != '?' && i != '.')
        i ^= '0' ^ '1';
    solvep(s);
    reverse(s.begin(), s.end());
    s.pop_back();
    s = "." + s;
  }
  cout << ans;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 1 << 20, mod = 1e9 + 7;
string s;
int n, ans = 1 << 30;
int p[maxn], sm[maxn];
int solvep() { // stuff with +
  p[0] = 0;
  int gm = 0;
  for (int i = 1; i <= n; i++) {
    p[i] = p[i - 1] + (s[i] == '-' ? -1 : 1);
    gm = min(gm, p[i]);
  }
  sm[n] = p[n];
  for (int i = n; i--;)
    sm[i] = min(p[i], sm[i + 1]);

  for (int r = 0, i = 0; i <= n; i++) {
    sm[i] += r;
    if (s[i] == '?' && sm[i] - 2 >= gm) {
      r -= 2;
      // cout <<"at " << i << '\\n';
      sm[i] -= 2;
    }
    // cout << p[i] << "\\n";
  }
  int rm = 0;
  for (int i = 0; i <= n; i++)
    rm = max(rm, sm[i]);
  ans = min(ans, abs(gm - rm));
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> s;
  n = s.size();
  s = "." + s;
  solvep();
  // solvem();
  cout << ans;
}
Problem p02652 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
template <typename T> inline T read() {
  T x = 0, f = 0;
  char c = getchar();
  while (!isdigit(c))
    f = c == '-', c = getchar();
  while (isdigit(c))
    x = x * 10 + c - 48, c = getchar();
  return f ? -x : x;
}
namespace run {
const int N = 1e6 + 9;
int n, sum[N], g[N], ans = N;
char s[N], t[N];
inline void solve(int Z) {
  for (int i = 1; i <= n; i++)
    t[i] = s[i];
  for (int i = n; i >= 1; i--)
    g[i] = (Z - sum[i]) / 2;
  for (int i = n - 1; i >= 1; i--)
    g[i] = min(g[i], g[i + 1]);

  //	for(int i=1;i<=n;i++) cout<<g[i]<<" ";cout<<endl;
  int now = 0;
  for (int i = 1; i <= n; i++)
    if (t[i] == '?') {
      if (now < g[i])
        t[i] = '1', now++;
      else
        t[i] = '0';
    }

  int tmp = 0, mn = 0, mx = 0;
  for (int i = 1; i <= n; i++) {
    tmp += (t[i] == '1') * 2 - 1;
    mn = min(tmp, mn), mx = max(tmp, mx);
  }
  //	for(int i=1;i<=n;i++) cout<<t[i]<<" ";cout<<endl;
  //	cout<<mn<<" "<<mx<<endl;
  ans = min(ans, mx - mn);
}
int main() {
  scanf("%s", s + 1), n = strlen(s + 1);

  int Z = 0;
  for (int i = 1; i <= n; i++)
    sum[i] = sum[i - 1] + ((s[i] == '1') * 2 - 1), Z = max(Z, sum[i]);
  //	cout<<Z<<endl;
  solve(Z), solve(Z + 1);
  printf("%d\\n", ans);
  return 0;
}
} // namespace run
int main() {
#ifdef my
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
#endif
  return run::main();
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <typename T> inline T read() {
  T x = 0, f = 0;
  char c = getchar();
  while (!isdigit(c))
    f = c == '-', c = getchar();
  while (isdigit(c))
    x = x * 10 + c - 48, c = getchar();
  return f ? -x : x;
}
namespace run {
const int N = 1e6 + 9;
char s[N];
int n, ans = N;
inline void dfs(int i, int x, int y, int ret) {
  if (ret >= ans)
    return;
  if (i == n + 1)
    return ans = min(ret, ans), void();

  if (s[i] != '0')
    dfs(i + 1, x + 1, max(y - 1, 0), max(ret, x + 1));
  if (s[i] != '1')
    dfs(i + 1, max(x - 1, 0), y + 1, max(ret, y + 1));
}
int main() {
  scanf("%s", s + 1), n = strlen(s + 1);
  dfs(0, 0, 0, 0);
  printf("%d\\n", ans);
  return 0;
}
} // namespace run
// #define my
int main() {
#ifdef my
  freopen("B.in", "r", stdin);
  freopen("B.out", "w", stdout);
#endif
  return run::main();
}
Problem p02652 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int solve(string const &s, int h) {
  int n = s.size();
  vector<pair<int, int>> dp(n + 1, {0, h});
  for (int i = n - 1, par = -1; i >= 0; i--) {
    if (par != -1)
      par ^= 1;
    if (s[i] == '1') {
      dp[i] = {dp[i + 1].first - 1, dp[i + 1].second - 1};
    } else if (s[i] == '0') {
      dp[i] = {dp[i + 1].first + 1, dp[i + 1].second + 1};
    } else {
      if (dp[i + 1].first == dp[i + 1].second && par == -1) {
        par = (dp[i + 1].first + 1) & 1;
      }
      dp[i] = {dp[i + 1].first - 1, dp[i + 1].second + 1};
    }
    dp[i].first = max(dp[i].first, 0);
    dp[i].second = min(dp[i].second, h);
    if ((dp[i].first > h) || (dp[i].second < 0) ||
        (par != -1 && dp[i].first == dp[i].second &&
         (dp[i].first & 1) != par)) {
      return 0;
    }
  }
  return 1;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  int n = s.size();
  int l = 1, r = n;
  while (l != r) {
    int mid = (l + r) / 2;
    int ok = solve(s, mid);
    if (ok) {
      r = mid;
    } else
      l = mid + 1;
  }
  cout << l << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int solve(string const &s, int h) {
  int n = s.size();
  vector<pair<int, int>> dp(n + 1, {0, h});
  for (int i = n - 1, par = -1; i >= 0; i--) {
    if (par != -1)
      par ^= 1;
    if (s[i] == '1') {
      dp[i] = {dp[i + 1].first - 1, dp[i + 1].second - 1};
    } else if (s[i] == '0') {
      dp[i] = {dp[i + 1].first + 1, dp[i + 1].second + 1};
    } else {
      if (dp[i + 1].first == dp[i + 1].second && par == -1) {
        assert(dp[i + 1].first == 0 || dp[i + 1].first == h);
        if (dp[i + 1].first == 0) {
          par = 1;
        } else {
          par = (h - 1) & 1;
        }
      }
      dp[i] = {dp[i + 1].first - 1, dp[i + 1].second + 1};
    }
    dp[i].first = max(dp[i].first, 0);
    dp[i].second = min(dp[i].second, h);
    if ((dp[i].first > h) || (dp[i].second < 0) ||
        (par != -1 && dp[i].first == dp[i].second &&
         (dp[i].first & 1) != par)) {
      return 0;
    }
  }
  return 1;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  int n = s.size();
  int l = 0, r = n - 1;
  while (l != r) {
    int mid = (l + r) / 2;
    int ok = solve(s, mid);
    if (ok) {
      r = mid;
    } else
      l = mid + 1;
  }
  cout << l << '\\n';
  return 0;
}
Problem p02652 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

#define X first
#define Y second
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 1e6 + 500;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;

int mini = 0, maxi = 0, n;
char s[N];
int desno[N];

int probaj(int x) {
  if (desno[0] < x)
    return n;
  int cur = 0, gran = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      cur++;
    } else if (s[i] == '0') {
      cur--;
    } else {
      if (cur + desno[i + 1] > x)
        cur--;
      else
        cur++;
    }
    gran = max(cur, gran);
  }
  return gran - x;
}

int main() {
  scanf("%s", s);
  n = strlen(s);
  desno[n] = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1' || s[i] == '?') {
      desno[i] = min(desno[i + 1] + 1, 0);
    } else {
      desno[i] = desno[i + 1] - 1;
    }
  }
  int sol = n;
  for (int x = desno[0]; x >= desno[0] - 50; x--)
    sol = min(sol, probaj(x));
  printf("%d\\n", sol);
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>

#define X first
#define Y second
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int N = 1e6 + 500;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;

int mini = 0, maxi = 0, n;
char s[N];
int desno[N];

int probaj(int x) {
  if (desno[0] < x)
    return n;
  int cur = 0, gran = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      cur++;
    } else if (s[i] == '0') {
      cur--;
    } else {
      if (cur + desno[i + 1] > x)
        cur--;
      else
        cur++;
    }
    gran = max(cur, gran);
  }
  return gran - x;
}

int main() {
  scanf("%s", s);
  n = strlen(s);
  desno[n] = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1' || s[i] == '?') {
      desno[i] = min(desno[i + 1] + 1, 0);
    } else {
      desno[i] = desno[i + 1] - 1;
    }
  }
  if (n <= 1e4) {
    int bst = n, gdje = 0;
    for (int i = 0; 2 * i <= n; i++)
      bst = min(bst, probaj(-i));
    printf("%d\\n", bst);
  } else {
    int bst = n, gdje = 0;
    int lo = 0, hi = n;
    while (lo + 100 > hi) {
      int mid1 = (2 * lo + hi) / 3;
      int mid2 = (lo + 2 * hi) / 3;
      int s1 = probaj(-mid1), s2 = probaj(-mid2);
      bst = min(bst, min(s1, s2));
      if (s1 == s2) {
        lo = mid1, hi = mid2;
      } else if (s1 < s2) {
        hi = mid2;
      } else {
        lo = mid1;
      }
    }
    for (int i = lo; i <= hi; i++)
      bst = min(bst, probaj(-i));
    printf("%d\\n", bst);
  }
  return 0;
}
Problem p02652 (C++) — buggy submission, verdict: Runtime Error

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

/*
右側の塊を考えて、なるべく現在と逆のを書きつつ、やばそうなら右側のと逆を書く
*/
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

signed main() {
  string s;
  cin >> s;
  int n = s.size();
  vector<int> o(n + 1, 0), z(n + 1, 0);
  int ot = 0, zt = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0')
      z[i] = ++zt;
    else {
      chmax(--zt, 0);
      z[i] = zt;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1')
      o[i] = ++ot;
    else {
      chmax(--ot, 0);
      o[i] = ot;
    }
  }
  int ans = 0;
  ot = zt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      chmax(ans, ++zt);
      chmax(--ot, 0);
    } else if (s[i] == '1') {
      chmax(ans, ++ot);
      chmax(--zt, 0);
    } else {
      if (zt + z[i + 1] > ot + o[i + 1]) {
        chmax(ans, ++ot);
        chmax(--zt, 0);
      } else {
        chmax(ans, ++zt);
        chmax(--ot, 0);
      }
    }
  }
  int ans2 = ans;
  ans = ot = zt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      chmax(ans, ++zt);
      chmax(--ot, 0);
    } else if (s[i] == '1') {
      chmax(ans, ++ot);
      chmax(--zt, 0);
    } else {
      if (zt + z[i + 1] >= ot + o[i + 1]) {
        chmax(ans, ++ot);
        chmax(--zt, 0);
      } else {
        chmax(ans, ++zt);
        chmax(--ot, 0);
      }
    }
  }
  cout << min(ans, ans2) << endl;
}


Error: 0

Fixed submission:

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

/*
右側の塊を考えて、なるべく現在と逆のを書きつつ、やばそうなら右側のと逆を書く
*/
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

signed main() {
  string s;
  cin >> s;
  int n = s.size();
  assert(n < 1e3);
  vector<int> o(n + 1, 0), z(n + 1, 0);
  int ot = 0, zt = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0')
      z[i] = ++zt;
    else {
      chmax(--zt, 0);
      z[i] = zt;
    }
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1')
      o[i] = ++ot;
    else {
      chmax(--ot, 0);
      o[i] = ot;
    }
  }
  int ans = 0;
  ot = zt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      chmax(ans, ++zt);
      chmax(--ot, 0);
    } else if (s[i] == '1') {
      chmax(ans, ++ot);
      chmax(--zt, 0);
    } else {
      if (zt + z[i] > ot + o[i]) {
        chmax(ans, ++ot);
        chmax(--zt, 0);
      } else {
        chmax(ans, ++zt);
        chmax(--ot, 0);
      }
    }
  }
  cout << ans << endl;
}
Problem p02652 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 1000010
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
int mx, n, a[N], b[N];
char s[N];
void init() {
  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    sum += (s[i] == '1' ? 1 : -1);
    mx = max(mx, sum);
    a[i] = sum;
  }
  b[n] = a[n];
  for (int i = n - 1; i >= 0; --i) {
    b[i] = max(a[i], b[i + 1]);
  }
}
int calc(int lim) {
  int mn = 0, sum = 0;
  for (int i = 1; i <= n; ++i) {
    if (s[i] == '1')
      ++sum;
    else if (s[i] == '0')
      --sum;
    else {
      if (b[i] - a[i - 1] + sum + 2 <= lim)
        ++sum;
      else
        --sum;
    }
    mn = min(mn, sum);
  }
  return mn;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  init();
  printf("%d\\n", min(mx - calc(mx), mx + 1 - calc(mx + 1)));
  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define N 100010
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return x * f;
}
int mx, n, a[N], b[N];
char s[N];
void init() {
  int sum = 0;
  for (int i = 1; i <= n; ++i) {
    sum += (s[i] == '1' ? 1 : -1);
    mx = max(mx, sum);
    a[i] = sum;
  }
  b[n] = a[n];
  for (int i = n - 1; i >= 0; --i) {
    b[i] = max(a[i], b[i + 1]);
  }
}
int calc(int lim) {
  int mn = 0, sum = 0;
  for (int i = 1; i <= n; ++i) {
    if (s[i] == '1')
      ++sum;
    else if (s[i] == '0')
      --sum;
    else {
      if (b[i] - a[i - 1] + sum + 2 <= lim)
        ++sum;
      else
        --sum;
    }
    mn = min(mn, sum);
  }
  return mn;
}
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  init();
  printf("%d\\n", min(mx - calc(mx), mx + 1 - calc(mx + 1)));
  return 0;
}