Problem p03443 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define int LL
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RE(i, n) FOR(i, 1, n)
#define REP(i, n) FOR(i, 0, (int)(n)-1)
#define R(i, n) REP(i, n)
#define VI vector<int>
#define PII pair<int, int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

template <class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template <class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << '=' << h << ',';
  _dbg(sdbg + 1, a...);
}

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[";
  for (auto vv : V)
    os << vv << ",";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr                                                                   \
  if (0)                                                                       \
  cout
#endif

const int MAX = 2e5 + 10;
int n;
string s;
int res[MAX];
int znaj(int a) {
  while (1) {
    a++;
    if (a == 2 * n)
      a = 0;
    if (res[a] != 0)
      return a;
  }
}
int sumk[MAX];
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);
  cin >> n >> s;
  s += '1';
  int ost = 1;
  int ak = 1;
  int il = 2 * n + 1;
  vector<int> spec;
  bool cz = 0;
  int ilx = 0;
  R(i, n * 2) {
    if (s[i] == '0') {
      cz = 0;
      if (ost == 0) {
        res[i] = il / 2;
        il--;
      } else {
        res[i] = ak;
      }
      ost = 0;
    } else {
      if (ost == 0) {
        res[i] = ak;
        ak++;
      } else {
        if (cz == 0) {
          cz = 1;
          spec.PB(i);
        }
        ilx++;
      }
      ost = 1;
    }
  }
  if (ilx & 1) {
    cout << "No\n";
    return 0;
  }

  R(i, n * 2) { cerr << res[i] << " "; }
  cerr << "\n";
  debug(ilx);

  debug(spec);
  if (ilx % 4 == 2) {
    debug(s[0], s[2 * n - 2]);
    if (s[0] == '1' && s[2 * n - 2] == '1') {
      spec.pop_back();
    }
    debug(spec);
    if (SZ(spec) == 1) {
      cout << "No\n";
      return 0;
    }
    res[spec[0]] = ak;
    res[spec[1]] = ak;
    ak++;
    int a = znaj(spec[0]);
    int b = znaj(spec[1]);
    swap(res[a], res[b]);
  }

  R(i, n * 2) { sumk[res[i]] += i; }
  cout << "Yes\n";

  R(i, n * 2) { cerr << res[i] << " "; }
  cerr << "\n";
  int x = 0;
  vector<int> pom;
  while (x != 2 * n) {
    if (res[x] != 0) {
      x = sumk[res[x]] - x;
    }
    if (res[x] == 0)
      pom.PB(x);
    x++;
  }
  debug(pom);
  for (int i = 0; i < SZ(pom); i += 4) {
    res[pom[i]] = ak;
    res[pom[i + 1]] = ak + 1;
    res[pom[i + 2]] = ak;
    res[pom[i + 3]] = ak + 1;
    ak += 2;
  }
  R(i, n * 2) { cout << res[i] << " "; }
  cout << "\n";
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define int LL
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define RE(i, n) FOR(i, 1, n)
#define REP(i, n) FOR(i, 0, (int)(n)-1)
#define R(i, n) REP(i, n)
#define VI vector<int>
#define PII pair<int, int>
#define LD long double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())

template <class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template <class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << '=' << h << ',';
  _dbg(sdbg + 1, a...);
}

template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[";
  for (auto vv : V)
    os << vv << ",";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr                                                                   \
  if (0)                                                                       \
  cout
#endif

const int MAX = 2e5 + 10;
int n;
string s;
int res[MAX];
int znaj(int a) {
  while (1) {
    a++;
    if (a == 2 * n)
      a = 0;
    if (res[a] != 0)
      return a;
  }
}
int sumk[MAX];
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(11);
  cerr << fixed << setprecision(6);
  cin >> n >> s;
  s += '1';
  int ost = 1;
  int ak = 1;
  int il = 2 * n + 1;
  vector<int> spec;
  bool cz = 0;
  int ilx = 0;
  R(i, n * 2) {
    if (s[i] == '0') {
      cz = 0;
      if (ost == 0) {
        res[i] = il / 2;
        il--;
      } else {
        res[i] = ak;
      }
      ost = 0;
    } else {
      if (ost == 0) {
        res[i] = ak;
        ak++;
      } else {
        if (cz == 0) {
          cz = 1;
          spec.PB(i);
        }
        ilx++;
      }
      ost = 1;
    }
  }
  if (ilx & 1) {
    cout << "No\n";
    return 0;
  }

  R(i, n * 2) { cerr << res[i] << " "; }
  cerr << "\n";
  debug(ilx);

  debug(spec);
  if (ilx % 4 == 2) {
    debug(s[0], s[2 * n - 2]);
    if (s[0] == '1' && s[2 * n - 2] == '1') {
      spec.pop_back();
    }
    debug(spec);
    if (SZ(spec) <= 1) {
      cout << "No\n";
      return 0;
    }
    res[spec[0]] = ak;
    res[spec[1]] = ak;
    ak++;
    int a = znaj(spec[0]);
    int b = znaj(spec[1]);
    swap(res[a], res[b]);
  }

  R(i, n * 2) { sumk[res[i]] += i; }
  cout << "Yes\n";

  R(i, n * 2) { cerr << res[i] << " "; }
  cerr << "\n";
  int x = 0;
  vector<int> pom;
  while (x != 2 * n) {
    if (res[x] != 0) {
      x = sumk[res[x]] - x;
    }
    if (res[x] == 0)
      pom.PB(x);
    x++;
  }
  debug(pom);
  for (int i = 0; i < SZ(pom); i += 4) {
    res[pom[i]] = ak;
    res[pom[i + 1]] = ak + 1;
    res[pom[i + 2]] = ak;
    res[pom[i + 3]] = ak + 1;
    ak += 2;
  }
  R(i, n * 2) { cout << res[i] << " "; }
  cout << "\n";
}