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

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int n = s.size();
  lint ok = n;
  lint ng = 0;
  auto check = [&](lint mid) {
    for (int i = 0; i < 2; ++i) {
      int a = i;
      int b = mid;
      if (a % 2 != b % 2)
        b--;
      bool ok = true;
      for (int j = 0; j < n; ++j) {
        if (s[j] == '1') {
          a++, b++;
        } else if (s[j] == '0') {
          a--, b--;
        } else {
          a--, b++;
        }
        if (a < 0)
          a += 2;
        if (b > mid)
          b -= 2;
        if (a > b) {
          ok = false;
          break;
        }
      }
      if (ok)
        return true;
    }
    return false;
  };
  while (abs(ok - ng) != 1) {
    lint mid = (ok + ng) / 2;
    (check(mid) ? ok : ng) = mid;
  }
  cout << ok << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int n = s.size();
  vector<pair<int, int>> ret;
  ret.emplace_back(0, 0);
  for (int i = n - 1; i >= 0; --i) {
    if (s[i] == '1') {
      vector<pair<int, int>> r2;
      for (auto &r : ret) {
        auto p1 = r;
        p1.first--;
        p1.second--;
        p1.first = max(0, p1.first);
        r2.push_back(p1);
      }
      sort(r2.begin(), r2.end());
      vector<pair<int, int>> r3;
      for (int j = (int)r2.size() - 1; j >= 0; --j) {
        if (j == (int)r2.size() - 1 || r2[j].first != r2[j + 1].first) {
          r3.emplace_back(r2[j].second, r2[j].first);
        }
      }
      ret.clear();
      sort(r3.begin(), r3.end());
      for (int j = 0; j < (int)r3.size(); ++j) {
        if (j == 0 || r3[j].first != r3[j - 1].first) {
          ret.emplace_back(r3[j].second, r3[j].first);
        }
      }
    } else if (s[i] == '0') {
      vector<pair<int, int>> r2;
      for (auto &r : ret) {
        auto p2 = r;
        p2.first++;
        p2.second++;
        p2.second = min(0, p2.second);
        r2.push_back(p2);
      }
      sort(r2.begin(), r2.end());
      vector<pair<int, int>> r3;
      for (int j = (int)r2.size() - 1; j >= 0; --j) {
        if (j == (int)r2.size() - 1 || r2[j].first != r2[j + 1].first) {
          r3.emplace_back(r2[j].second, r2[j].first);
        }
      }
      ret.clear();
      sort(r3.begin(), r3.end());
      for (int j = 0; j < (int)r3.size(); ++j) {
        if (j == 0 || r3[j].first != r3[j - 1].first) {
          ret.emplace_back(r3[j].second, r3[j].first);
        }
      }
    } else if (s[i] == '?') {
      vector<pair<int, int>> r2;
      for (auto &r : ret) {
        auto p1 = r;
        p1.first--;
        p1.second--;
        p1.first = max(0, p1.first);
        auto p2 = r;
        p2.first++;
        p2.second++;
        p2.second = min(0, p2.second);
        r2.push_back(p1);
        r2.push_back(p2);
      }
      sort(r2.begin(), r2.end());
      vector<pair<int, int>> r3;
      for (int j = (int)r2.size() - 1; j >= 0; --j) {
        if (j == (int)r2.size() - 1 || r2[j].first != r2[j + 1].first) {
          r3.emplace_back(r2[j].second, r2[j].first);
        }
      }
      ret.clear();
      sort(r3.begin(), r3.end());
      for (int j = 0; j < (int)r3.size(); ++j) {
        if (j == 0 || r3[j].first != r3[j - 1].first) {
          ret.emplace_back(r3[j].second, r3[j].first);
        }
      }
    }
  }
  lint res = inf;
  for (auto &r : ret) {
    res = min(res, lint(r.first - r.second));
  }
  cout << res << "\\n";
  return 0;
}