prithivMLmods/Coder-Stat huggingface dataset:
Problem p03246 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;

mt19937 rnd(228);

map<int, int> mp[2];

int main() {
#ifdef ONPC
  freopen("a.in", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    mp[i % 2][x]++;
  }
  int ans = 0;
  vector<vector<pair<int, int>>> f;
  for (int t = 0; t < 2; t++) {
    vector<pair<int, int>> e;
    for (auto c : mp[t])
      e.push_back({c.second, c.first});
    for (int i = 1; i <= 3; i++)
      e.push_back({mp[t][i], i});
    sort(e.rbegin(), e.rend());
    e.resize(unique(e.begin(), e.end()) - e.begin());
    f.push_back(e);
  }
  for (auto c : f[0]) {
    for (auto d : f[1]) {
      if (c.second != d.second) {
        ans = max(ans, c.first + d.first);
      }
    }
  }
  cout << n - ans << '\n';
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;

mt19937 rnd(228);

map<int, int> mp[2];

int main() {
#ifdef ONPC
  freopen("a.in", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    mp[i % 2][x]++;
  }
  int ans = 0;
  vector<vector<pair<int, int>>> f;
  for (int t = 0; t < 2; t++) {
    vector<pair<int, int>> e;
    for (auto c : mp[t])
      e.push_back({c.second, c.first});
    for (int i = 1; i <= 3; i++)
      e.push_back({mp[t][i], i});
    sort(e.rbegin(), e.rend());
    e.resize(unique(e.begin(), e.end()) - e.begin());
    e.resize(2);
    f.push_back(e);
  }
  for (auto c : f[0]) {
    for (auto d : f[1]) {
      if (c.second != d.second) {
        ans = max(ans, c.first + d.first);
      }
    }
  }
  cout << n - ans << '\n';
}
Problem p03246 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>

#define ARC(i, a, b) for (int i = (a); i < (b); ++i)
#define RC(i, n) ARC(i, 0, n)
#define ARCH(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RCH(i, n) ARCH(i, n, 0)

template <class T> T ath(const T &a, const T &b) { return a > b ? a : b; }

int n, v[2][12000], av[2], inav[2], aav[2], inaav[2];

int solve() {
  scanf("%d", &n);
  RC(i, n / 2) {
    int in;
    scanf("%d", &in);
    ++v[0][in];
    if (av[0] < v[0][in]) {
      if (inav[0] != in) {
        aav[0] = av[0];
        inaav[0] = inav[0];
      }
      av[0] = v[0][in];
      inav[0] = in;
    } else if (aav[0] < v[0][in]) {
      aav[0] = v[0][in];
      inaav[0] = in;
    }
    scanf("%d", &in);
    ++v[1][in];
    if (av[1] < v[1][in]) {
      if (inav[1] != in) {
        aav[1] = av[1];
        inaav[1] = inav[1];
      }
      av[1] = v[1][in];
      inav[1] = in;
    } else if (aav[1] < v[1][in]) {
      aav[1] = v[1][in];
      inaav[1] = in;
    }
  }
  return n - (inav[0] != inav[1] ? av[0] + av[1]
                                 : ath(av[0] + aav[1], av[1] + aav[0]));
}

int main() { printf("%d\n", solve()); }


Fixed submission:
#include <stdio.h>

#define ARC(i, a, b) for (int i = (a); i < (b); ++i)
#define RC(i, n) ARC(i, 0, n)
#define ARCH(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RCH(i, n) ARCH(i, n, 0)

template <class T> T ath(const T &a, const T &b) { return a > b ? a : b; }

int n, v[2][120000], av[2], inav[2], aav[2], inaav[2];

int solve() {
  scanf("%d", &n);
  RC(i, n / 2) {
    int in;
    scanf("%d", &in);
    ++v[0][in];
    if (av[0] < v[0][in]) {
      if (inav[0] != in) {
        aav[0] = av[0];
        inaav[0] = inav[0];
      }
      av[0] = v[0][in];
      inav[0] = in;
    } else if (aav[0] < v[0][in]) {
      aav[0] = v[0][in];
      inaav[0] = in;
    }
    scanf("%d", &in);
    ++v[1][in];
    if (av[1] < v[1][in]) {
      if (inav[1] != in) {
        aav[1] = av[1];
        inaav[1] = inav[1];
      }
      av[1] = v[1][in];
      inav[1] = in;
    } else if (aav[1] < v[1][in]) {
      aav[1] = v[1][in];
      inaav[1] = in;
    }
  }
  return n - (inav[0] != inav[1] ? av[0] + av[1]
                                 : ath(av[0] + aav[1], av[1] + aav[0]));
}

int main() { printf("%d\n", solve()); }
Problem p03246 (C++)

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

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

const int N = 1e5 + 5;
int ct[2][N], a[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ct[i & 1][a[i]]++;
  }

  set<pair<int, int>> v[2];

  int ans = n;

  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < N; j++) {
      if (ct[i][j]) {
        v[i].insert({n / 2 - ct[i][j], j});
        if (v[i].size() > 2)
          v[i].erase(prev(v[i].begin()));
        ans = min(ans, n - ct[i][j]);
      }
    }
  }

  for (auto p : v[0]) {
    for (auto q : v[1]) {
      if (p.second != q.second) {
        ans = min(ans, p.first + q.first);
      }
    }
  }

  cout << ans;

  return 0;
}

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

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

const int N = 1e5 + 5;
int ct[2][N], a[N];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ct[i & 1][a[i]]++;
  }

  set<pair<int, int>> v[2];

  int ans = n;

  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < N; j++) {
      if (ct[i][j]) {
        v[i].insert({n / 2 - ct[i][j], j});
        if (v[i].size() > 2)
          v[i].erase(prev(v[i].end()));
        ans = min(ans, n - ct[i][j]);
      }
    }
  }

  for (auto p : v[0]) {
    for (auto q : v[1]) {
      if (p.second != q.second) {
        ans = min(ans, p.first + q.first);
      }
    }
  }

  cout << ans;

  return 0;
}
Problem p03246 (C++)

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

#define int long long

using namespace std;

int32_t main() {
  int n;
  cin >> n;

  map<int, int> odd, even;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    if (i % 2) {
      odd[temp]++;
    } else {
      even[temp]++;
    }
  }

  vector<pair<int, int>> a, b;

  for (auto it = odd.begin(); it != odd.end(); it++) {
    a.push_back({it->second, it->first});
  }

  for (auto it = even.begin(); it != even.end(); it++) {
    b.push_back({it->second, it->first});
  }

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  n /= 2;
  int n1 = a.size();
  int m1 = b.size();
  if (a[n1 - 1].second != b[m1 - 1].second) {
    cout << n - a[n1 - 1].first + n - b[m1 - 1].first << endl;
    return 0;
  }

  int ans = n - max(b[m1 - 1].first, a[n - 1].first) + n;
  if (n1 >= 2) {
    ans = min(ans, n - a[n1 - 2].first + n - b[m1 - 1].first);
  }

  if (m1 >= 2) {
    ans = min(ans, n - a[n1 - 1].first + n - b[m1 - 2].first);
  }
  cout << ans << endl;
  return 0;
}


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

#define int long long

using namespace std;

int32_t main() {
  int n;
  cin >> n;

  map<int, int> odd, even;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    if (i % 2) {
      odd[temp]++;
    } else {
      even[temp]++;
    }
  }

  vector<pair<int, int>> a, b;

  for (auto it = odd.begin(); it != odd.end(); it++) {
    a.push_back({it->second, it->first});
  }

  for (auto it = even.begin(); it != even.end(); it++) {
    b.push_back({it->second, it->first});
  }

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  n /= 2;
  int n1 = a.size();
  int m1 = b.size();
  if (a[n1 - 1].second != b[m1 - 1].second) {
    cout << n - a[n1 - 1].first + n - b[m1 - 1].first << endl;
    return 0;
  }

  int ans = n - max(b[m1 - 1].first, a[n1 - 1].first) + n;
  if (n1 >= 2) {
    ans = min(ans, n - a[n1 - 2].first + n - b[m1 - 1].first);
  }

  if (m1 >= 2) {
    ans = min(ans, n - a[n1 - 1].first + n - b[m1 - 2].first);
  }
  cout << ans << endl;
  return 0;
}
Problem p03246 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define forx(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, ans = 1e9, amax = 0, bmax = 0, af, bf;
  cin >> n;
  map<int, int> a, b;
  rep(i, n / 2) {
    int x, y;
    cin >> x >> y;
    a[x]++;
    b[y]++;
  }
  for (auto i = a.begin(); i != a.end(); ++i) {
    if (amax < (i->second)) {
      amax = i->second;
      af = i->first;
    }
  }
  for (auto i = b.begin(); i != b.end(); ++i) {
    if (bmax < (i->second)) {
      bmax = i->second;
      bf = i->first;
    }
  }
  if (bf != af) {
    cout << n - amax - bmax << endl;
    return 0;
  }

  for (auto i = a.begin(); i != a.end(); ++i) {
    for (auto j = b.begin(); j != b.end(); ++j) {
      if ((i->first) == (j->first))
        ans = min(ans, n - max(i->second, j->second));
      else
        ans = min(ans, n - (i->second) - (j->second));
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define forx(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, ans = 1e9, amax = 0, bmax = 0, af, bf;
  cin >> n;
  map<int, int> a, b;
  rep(i, n / 2) {
    int x, y;
    cin >> x >> y;
    a[x]++;
    b[y]++;
  }
  for (auto i = a.begin(); i != a.end(); ++i) {
    if (amax < (i->second)) {
      amax = i->second;
      af = i->first;
    }
  }
  for (auto i = b.begin(); i != b.end(); ++i) {
    if (bmax <= (i->second)) {
      bmax = i->second;
      bf = i->first;
    }
  }
  if (bf != af) {
    cout << n - amax - bmax << endl;
    return 0;
  }

  for (auto i = a.begin(); i != a.end(); ++i) {
    for (auto j = b.begin(); j != b.end(); ++j) {
      if ((i->first) == (j->first))
        ans = min(ans, n - max(i->second, j->second));
      else
        ans = min(ans, n - (i->second) - (j->second));
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03246 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define show(x) cout << #x << " = " << x << endl;
typedef long long ll;
typedef pair<int, int> pii;
int main() {
  int n;
  cin >> n;
  vector<pii> a(10005, {0, 0}), b(10005, {0, 0});
  rep(i, n) {
    int k;
    cin >> k;
    if (i % 2) {
      a[k].first++;
      a[k].second = k;
    } else {
      b[k].first++;
      b[k].second = k;
    }
  }
  sort(a.rbegin(), a.rend());
  sort(b.rbegin(), b.rend());
  if (a[0].second != b[0].second) {
    cout << n - a[0].first - b[0].first << endl;
  } else {
    cout << min(n - a[0].first - b[1].first, n - a[1].first - b[0].first)
         << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define show(x) cout << #x << " = " << x << endl;
typedef long long ll;
typedef pair<int, int> pii;
int main() {
  int n;
  cin >> n;
  vector<pii> a(100005, {0, 0}), b(100005, {0, 0});
  rep(i, n) {
    int k;
    cin >> k;
    if (i % 2) {
      a[k].first++;
      a[k].second = k;
    } else {
      b[k].first++;
      b[k].second = k;
    }
  }
  sort(a.rbegin(), a.rend());
  sort(b.rbegin(), b.rend());
  if (a[0].second != b[0].second) {
    cout << n - a[0].first - b[0].first << endl;
  } else {
    cout << min(n - a[0].first - b[1].first, n - a[1].first - b[0].first)
         << endl;
  }
}
Problem p03246 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cstdlib>
using namespace std;

int main() {
  int n, M, m, amax = 0, amaxi = 0, bmax = 0, bmaxi = 0;
  cin >> n;
  vector<int> v(n + 10), a(100000, 0), b(100000, 0);

  for (int i = 0; i < n; i++) {
    cin >> v.at(i);
  }

  for (int i = 0; i < n - 1; i += 2) {
    a.at(v.at(i))++;
    b.at(v.at(i + 1))++;
  }

  for (int i = 0; i < 100000; i++) {
    if (a.at(i) > amax) {
      amax = a.at(i);
      amaxi = i;
    }
    if (b.at(i) > bmax) {
      bmax = b.at(i);
      bmaxi = i;
    }
  }
  if (amaxi == bmaxi) {
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());
    sort(b.begin(), b.end());
    reverse(b.begin(), b.end());

    int c = min(n - a.at(0) - b.at(1), n - a.at(1) - b.at(0));
    cout << c << endl;
  } else {
    cout << n - amax - bmax << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <cstdlib>
using namespace std;

int main() {
  int n, M, m, amax = 0, amaxi = 0, bmax = 0, bmaxi = 0;
  cin >> n;
  vector<int> v(n + 10), a(100001, 0), b(100001, 0);

  for (int i = 0; i < n; i++) {
    cin >> v.at(i);
  }

  for (int i = 0; i < n - 1; i += 2) {
    a.at(v.at(i))++;
    b.at(v.at(i + 1))++;
  }

  for (int i = 0; i < 100000; i++) {
    if (a.at(i) > amax) {
      amax = a.at(i);
      amaxi = i;
    }
    if (b.at(i) > bmax) {
      bmax = b.at(i);
      bmaxi = i;
    }
  }
  if (amaxi == bmaxi) {
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());
    sort(b.begin(), b.end());
    reverse(b.begin(), b.end());

    int c = min(n - a.at(0) - b.at(1), n - a.at(1) - b.at(0));
    cout << c << endl;
  } else {
    cout << n - amax - bmax << endl;
  }
}
Problem p03246 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 10010;
int n, s1[N], s2[N], mx, mx11, mx12, mx21, mx22, v11, v12, v21, v22;
int main() {
  int t, ans = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &t);
    mx = max(mx, t);
    if (i & 1)
      s1[t]++;
    else
      s2[t]++;
  }
  for (int i = 1; i <= mx; i++) {
    if (s1[i] >= mx11) {
      mx12 = mx11, v12 = v11;
      mx11 = s1[i], v11 = i;
    } else if (s1[i] > mx12)
      mx12 = s1[i], v12 = i;
    if (s2[i] >= mx21) {
      mx22 = mx21, v22 = v21;
      mx21 = s2[i], v21 = i;
    } else if (s2[i] > mx22)
      mx22 = s2[i], v22 = i;
  }
  if (v11 == v21) {
    if (v11 != v22)
      ans = max(ans, mx11 + mx22);
    if (v12 != v21)
      ans = max(ans, mx12 + mx21);
  } else
    ans = mx11 + mx21;
  ans = n - ans;
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 100010;
int n, s1[N], s2[N], mx, mx11, mx12, mx21, mx22, v11, v12, v21, v22;
int main() {
  int t, ans = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &t);
    mx = max(mx, t);
    if (i & 1)
      s1[t]++;
    else
      s2[t]++;
  }
  for (int i = 1; i <= mx; i++) {
    if (s1[i] >= mx11) {
      mx12 = mx11, v12 = v11;
      mx11 = s1[i], v11 = i;
    } else if (s1[i] > mx12)
      mx12 = s1[i], v12 = i;
    if (s2[i] >= mx21) {
      mx22 = mx21, v22 = v21;
      mx21 = s2[i], v21 = i;
    } else if (s2[i] > mx22)
      mx22 = s2[i], v22 = i;
  }
  if (v11 == v21) {
    if (v11 != v22)
      ans = max(ans, mx11 + mx22);
    if (v12 != v21)
      ans = max(ans, mx12 + mx21);
  } else
    ans = mx11 + mx21;
  ans = n - ans;
  printf("%d", ans);
  return 0;
}
Problem p03246 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  if (*max_element(v.begin(), v.end()) == *min_element(v.begin(), v.end())) {
    cout << n / 2 << endl;
    return 0;
  }
  map<int, int> cnt[2];
  for (int i = 0; i < n; i++) {
    if (i & 1) {
      cnt[1][v[i]]++;
    } else {
      cnt[0][v[i]]++;
    }
  }
  int res = n;
  for (auto p1 : cnt[0]) {
    for (auto p2 : cnt[1]) {
      if (p1.first != p2.first) {
        res = min(res, ((n + 1) / 2 - p1.second) + (n / 2 - p2.second));
      }
    }
  }
  cout << res << endl;
  return 0;
}


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

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  if (*max_element(v.begin(), v.end()) == *min_element(v.begin(), v.end())) {
    cout << n / 2 << endl;
    return 0;
  }
  map<int, int> cnt[2];
  for (int i = 0; i < n; i++) {
    if (i & 1) {
      cnt[1][v[i]]++;
    } else {
      cnt[0][v[i]]++;
    }
  }
  vector<pair<int, int>> p1, p2;
  for (auto v1 : cnt[0]) {
    p1.emplace_back((n + 1) / 2 - v1.second, v1.first);
  }
  for (auto v2 : cnt[1]) {
    p2.emplace_back(n / 2 - v2.second, v2.first);
  }
  sort(p1.begin(), p1.end());
  sort(p2.begin(), p2.end());
  int res = 0;
  if (p1.front().second == p2.front().second) {
    res = min(p1.front().first + ((int)p2.size() >= 2 ? p2[1].first : n / 2),
              ((int)p1.size() >= 2 ? p1[1].first : (n + 1) / 2) +
                  p2.front().first);
  } else {
    res = p1.front().first + p2.front().first;
  }
  cout << res << endl;
  return 0;
}
Problem p03246 (C++)

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

#define ll long long
#define pb push_back
#define mkp make_pair
#define ld long double

using namespace std;

int n, a[20100], ct[20100], proh;
int maxa1, maxa2, num1, num2;

int main() {
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  maxa1 = 0;
  for (int i = 0; i < n; i += 2) {
    ct[a[i]]++;
    if (maxa1 < ct[a[i]]) {
      maxa1 = ct[a[i]];
      num1 = a[i];
    }
  }

  memset(ct, 0, sizeof(ct));
  for (int i = 1; i < n; i += 2) {
    ct[a[i]]++;
    if (maxa2 < ct[a[i]]) {
      maxa2 = ct[a[i]];
      num2 = a[i];
    }
  }

  int ans;
  if (num1 == num2) {
    int proh1 = num1, proh2 = num2;
    int large1 = maxa1, large2 = maxa2;

    maxa1 = 0;
    memset(ct, 0, sizeof(ct));
    for (int i = 0; i < n; i += 2) {
      ct[a[i]]++;
      if (maxa1 < ct[a[i]] && a[i] != proh1) {
        maxa1 = ct[a[i]];
        num1 = a[i];
      }
    }

    maxa2 = 0;
    memset(ct, 0, sizeof(ct));
    for (int i = 1; i < n; i += 2) {
      ct[a[i]]++;
      if (maxa2 < ct[a[i]] && a[i] != proh2) {
        maxa2 = ct[a[i]];
        num2 = a[i];
      }
    }

    ans = max(large1 + maxa2, maxa1 + large2);
  } else
    ans = maxa1 + maxa2;

  cout << n - ans << endl;

  return 0;
}


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

#define ll long long
#define pb push_back
#define mkp make_pair
#define ld long double

using namespace std;

int n, a[200100], ct[200100], proh;
int maxa1, maxa2, num1, num2;

int main() {
  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  maxa1 = 0;
  for (int i = 0; i < n; i += 2) {
    ct[a[i]]++;
    if (maxa1 < ct[a[i]]) {
      maxa1 = ct[a[i]];
      num1 = a[i];
    }
  }

  memset(ct, 0, sizeof(ct));
  for (int i = 1; i < n; i += 2) {
    ct[a[i]]++;
    if (maxa2 < ct[a[i]]) {
      maxa2 = ct[a[i]];
      num2 = a[i];
    }
  }

  int ans;
  if (num1 == num2) {
    int proh1 = num1, proh2 = num2;
    int large1 = maxa1, large2 = maxa2;

    maxa1 = 0;
    memset(ct, 0, sizeof(ct));
    for (int i = 0; i < n; i += 2) {
      ct[a[i]]++;
      if (maxa1 < ct[a[i]] && a[i] != proh1) {
        maxa1 = ct[a[i]];
        num1 = a[i];
      }
    }

    maxa2 = 0;
    memset(ct, 0, sizeof(ct));
    for (int i = 1; i < n; i += 2) {
      ct[a[i]]++;
      if (maxa2 < ct[a[i]] && a[i] != proh2) {
        maxa2 = ct[a[i]];
        num2 = a[i];
      }
    }

    ans = max(large1 + maxa2, maxa1 + large2);
  } else
    ans = maxa1 + maxa2;

  cout << n - ans << endl;

  return 0;
}
Problem p03247 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

const int N = 1000 + 5;

int n;
int x[N], y[N];

const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const char dir[4] = {'R', 'L', 'U', 'D'};

ll dist(ll x, ll y, ll tx, ll ty) { return abs(x - tx) + abs(y - ty); }

int main() {
  scanf("%d", &n);
  int sig = 0, par = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &x[i], &y[i]);
    sig |= ((x[i] ^ y[i]) & 1);
    par |= !((x[i] ^ y[i]) & 1);
  }
  if (sig && par) {
    puts("-1");
    return 0;
  }
  if (par) {
    printf("32\n1 ");
  } else {
    printf("31\n");
  }
  for (int i = 30; i >= 0; i--) {
    printf("%d ", (int)pow(2, i));
  }
  puts("");
  for (int i = 1; i <= n; i++) {
    ll sx = 0, sy = 0;
    if (par) {
      putchar('L');
      sx--;
    }
    for (int j = 30; j >= 0; j--) {
      for (int k = 0; k < 4; k++) {
        ll nx = sx + dx[k] * pow(2, j);
        ll ny = sy + dy[k] * pow(2, j);
        int d = dist(nx, ny, x[i], y[i]);
        if (d <= pow(2, j) - 1 && !((d ^ ((int)pow(2, j) - 1)) & 1)) {
          sx = nx;
          sy = ny;
          putchar(dir[k]);
          break;
        }
      }
    }
    assert(sx == x[i] && sy == y[i]);
    puts("");
  }
  return 0;
}


Fixed submission:
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

const int N = 1000 + 5;

int n;
int x[N], y[N];

const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
const char dir[4] = {'R', 'L', 'U', 'D'};

ll dist(ll x, ll y, ll tx, ll ty) { return abs(x - tx) + abs(y - ty); }

int main() {
  scanf("%d", &n);
  int sig = 0, par = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &x[i], &y[i]);
    sig |= ((x[i] ^ y[i]) & 1);
    par |= !((x[i] ^ y[i]) & 1);
  }
  if (sig && par) {
    puts("-1");
    return 0;
  }
  if (par) {
    printf("32\n1 ");
  } else {
    printf("31\n");
  }
  for (int i = 30; i >= 0; i--) {
    printf("%d ", (int)pow(2, i));
  }
  puts("");
  for (int i = 1; i <= n; i++) {
    ll sx = 0, sy = 0;
    if (par) {
      putchar('L');
      sx--;
    }
    for (int j = 30; j >= 0; j--) {
      for (int k = 0; k < 4; k++) {
        ll nx = sx + dx[k] * pow(2, j);
        ll ny = sy + dy[k] * pow(2, j);
        ll d = dist(nx, ny, x[i], y[i]);
        if (d <= pow(2, j) - 1 && !((d ^ ((int)pow(2, j) - 1)) & 1)) {
          sx = nx;
          sy = ny;
          putchar(dir[k]);
          break;
        }
      }
    }
    assert(sx == x[i] && sy == y[i]);
    puts("");
  }
  return 0;
}
Problem p03247 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;
long long a[45], b[45];
int n, m, cnt;
long long x, y;
char s[45];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
    if ((a[i] + b[i]) & 1)
      cnt++;
    else
      cnt--;
  }
  if (abs(cnt) != n) {
    cout << -1;
    return 0;
  }
  m = 31 + (cnt < 0);
  cout << m << endl;
  for (int i = 0; i <= 30; i++) {
    cout << (1 << i) << " ";
  }
  if (cnt < 0)
    cout << 1;
  cout << endl;
  for (int i = 1; i <= n; i++) {
    int x = a[i], y = b[i];
    if (cnt < 0)
      s[31] = 'R', x--;
    int flag = 0;
    for (int j = 30; j >= 0; j--) {
      if (abs(x) < abs(y))
        swap(x, y), flag ^= 1;
      if (x > 0)
        x -= (1 << j), s[j] = flag ? 'U' : 'R';
      else
        x += (1 << j), s[j] = flag ? 'D' : 'L';
    }
    cout << s << endl;
  }
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
long long a[1005], b[1005];
int n, m, cnt;
long long x, y;
char s[45];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
    if ((a[i] + b[i]) & 1)
      cnt++;
    else
      cnt--;
  }
  if (abs(cnt) != n) {
    cout << -1;
    return 0;
  }
  m = 31 + (cnt < 0);
  cout << m << endl;
  for (int i = 0; i <= 30; i++) {
    cout << (1 << i) << " ";
  }
  if (cnt < 0)
    cout << 1;
  cout << endl;
  for (int i = 1; i <= n; i++) {
    int x = a[i], y = b[i];
    if (cnt < 0)
      s[31] = 'R', x--;
    int flag = 0;
    for (int j = 30; j >= 0; j--) {
      if (abs(x) < abs(y))
        swap(x, y), flag ^= 1;
      if (x > 0)
        x -= (1 << j), s[j] = flag ? 'U' : 'R';
      else
        x += (1 << j), s[j] = flag ? 'D' : 'L';
    }
    cout << s << endl;
  }
}
Problem p03247 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, x[1010], y[1010];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", y + i, x + i);
  int tag = !!((x[1] + y[1]) % 2);
  for (int i = 1; i <= n; i++)
    if (tag != !!((x[i] + y[i]) % 2))
      return puts("-1"), 0;
  int m = tag ? 31 : 30;
  puts("31");
  if (tag == 0)
    printf("1 ");
  for (int i = 0; i < m; i++)
    printf("%lld ", 1ll << i);
  puts("");
  for (int i = 1; i <= n; i++) {
    int dx = x[i], dy = y[i];
    if (tag == 0)
      dx--, putchar('U');
    for (int i = 1; i < m; i++) {
      assert((dx + dy) % 2);
      long long tt = ((dx + dy) % 4 + 4) % 4;
      if (dx % 2) {
        if (tt == 3)
          putchar('U'), dx--, dx /= 2, dy /= 2;
        else
          putchar('D'), dx++, dx /= 2, dy /= 2;
      } else {
        if (tt == 3)
          putchar('R'), dy--, dx /= 2, dy /= 2;
        else
          putchar('L'), dy++, dx /= 2, dy /= 2;
      }
      // cerr << dx << ' ' << dy << endl;
    }
    assert(abs(dx) + abs(dy) == 1);
    if (dx == 1)
      puts("U");
    else if (dx == -1)
      puts("D");
    else if (dy == 1)
      puts("R");
    else
      puts("L");
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, x[1010], y[1010];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", y + i, x + i);
  int tag = !!((x[1] + y[1]) % 2);
  for (int i = 1; i <= n; i++)
    if (tag != !!((x[i] + y[i]) % 2))
      return puts("-1"), 0;
  int m = tag ? 32 : 31;
  puts("32");
  if (tag == 0)
    printf("1 ");
  for (int i = 0; i < m; i++)
    printf("%lld ", 1ll << i);
  puts("");
  for (int i = 1; i <= n; i++) {
    int dx = x[i], dy = y[i];
    if (tag == 0)
      dx--, putchar('U');
    for (int i = 1; i < m; i++) {
      assert((dx + dy) % 2);
      long long tt = ((dx + dy) % 4 + 4) % 4;
      if (dx % 2) {
        if (tt == 3)
          putchar('U'), dx--, dx /= 2, dy /= 2;
        else
          putchar('D'), dx++, dx /= 2, dy /= 2;
      } else {
        if (tt == 3)
          putchar('R'), dy--, dx /= 2, dy /= 2;
        else
          putchar('L'), dy++, dx /= 2, dy /= 2;
      }
      // cerr << dx << ' ' << dy << endl;
    }
    assert(abs(dx) + abs(dy) == 1);
    if (dx == 1)
      puts("U");
    else if (dx == -1)
      puts("D");
    else if (dy == 1)
      puts("R");
    else
      puts("L");
  }
  return 0;
}
Problem p03247 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1005;

int n, m;
int X[N], Y[N];
int d[N];

const int dx[5] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
const char dc[5] = {'R', 'L', 'U', 'D'};

void getp(ll x, ll y) {
  for (int i = m; i; i--) {
    int now = d[i];
    for (int j = 0; j < 4; j++) {
      ll curx = x + d[i] * dx[j], cury = y + d[i] * dy[j];
      if (abs(curx) + abs(cury) < now) {
        cout << dc[j];
        x = curx, y = cury;
        break;
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  int sign = 1, mx = 0;
  for (int i = 1; i <= n; i++)
    cin >> X[i] >> Y[i];
  for (int i = 2; i <= n; i++) {
    if ((abs(X[i] + Y[i]) & 1) != (abs(X[i - 1] + Y[i - 1]) & 1))
      sign = 0;
    mx = max(mx, abs(X[i]) + abs(Y[i]));
  }
  if (!sign) {
    cout << -1;
    return 0;
  }
  for (d[m = 1] = 1; d[m] << 1 <= mx; ++m, d[m] = d[m - 1] << 1)
    ;
  if (abs(X[1] + Y[1]) & 1)
    sign = 0;
  cout << m + sign << '\n';
  if (sign)
    cout << 1 << ' ';
  for (int i = m; i; i--)
    cout << d[i] << " \n"[i == 1];
  for (int i = 1; i <= n; i++) {
    if (sign)
      cout << 'R';
    getp(X[i] - sign, Y[i]);
    cout << '\n';
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1005;

int n, m;
int X[N], Y[N];
ll d[N];

const int dx[5] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
const char dc[5] = {'R', 'L', 'U', 'D'};

void getp(ll x, ll y) {
  for (int i = m; i; i--) {
    int now = d[i];
    for (int j = 0; j < 4; j++) {
      ll curx = x + d[i] * dx[j], cury = y + d[i] * dy[j];
      if (abs(curx) + abs(cury) < now) {
        cout << dc[j];
        x = curx, y = cury;
        break;
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  int sign = 1, mx = 0;
  for (int i = 1; i <= n; i++)
    cin >> X[i] >> Y[i];
  for (int i = 2; i <= n; i++) {
    if ((abs(X[i] + Y[i]) & 1) != (abs(X[i - 1] + Y[i - 1]) & 1))
      sign = 0;
    mx = max(mx, abs(X[i]) + abs(Y[i]));
  }
  if (!sign) {
    cout << -1;
    return 0;
  }
  for (d[m = 1] = 1; d[m] << 1 <= mx; ++m, d[m] = d[m - 1] << 1)
    ;
  if (abs(X[1] + Y[1]) & 1)
    sign = 0;
  cout << m + sign << '\n';
  if (sign)
    cout << 1 << ' ';
  for (int i = m; i; i--)
    cout << d[i] << " \n"[i == 1];
  for (int i = 1; i <= n; i++) {
    if (sign)
      cout << 'R';
    getp(X[i] - sign, Y[i]);
    cout << '\n';
  }
  return 0;
}
Problem p03247 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define fd(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

const int N = 55;

int n, x[N], y[N], d[N], m;
bool in[2];

int main() {
  scanf("%d", &n);
  fo(i, 1, n) {
    scanf("%d%d", &x[i], &y[i]);
    in[(x[i] + y[i]) & 1] = 1;
  }
  if (in[0] && in[1]) {
    puts("-1");
    return 0;
  }
  if (in[0])
    d[++m] = 1;
  fd(i, 30, 0) d[++m] = 1 << i;
  printf("%d\n", m);
  fo(i, 1, m) printf("%d ", d[i]);
  puts("");
  fo(i, 1, n) {
    fo(j, 1, m) if (abs(x[i]) > abs(y[i])) {
      if (x[i] > 0)
        x[i] -= d[j], putchar('R');
      else
        x[i] += d[j], putchar('L');
    }
    else {
      if (y[i] > 0)
        y[i] -= d[j], putchar('U');
      else
        y[i] += d[j], putchar('D');
    }
    puts("");
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define fd(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

const int N = 1005;

int n, x[N], y[N], d[N], m;
bool in[2];

int main() {
  scanf("%d", &n);
  fo(i, 1, n) {
    scanf("%d%d", &x[i], &y[i]);
    in[(x[i] + y[i]) & 1] = 1;
  }
  if (in[0] && in[1]) {
    puts("-1");
    return 0;
  }
  if (in[0])
    d[++m] = 1;
  fd(i, 30, 0) d[++m] = 1 << i;
  printf("%d\n", m);
  fo(i, 1, m) printf("%d ", d[i]);
  puts("");
  fo(i, 1, n) {
    fo(j, 1, m) if (abs(x[i]) > abs(y[i])) {
      if (x[i] > 0)
        x[i] -= d[j], putchar('R');
      else
        x[i] += d[j], putchar('L');
    }
    else {
      if (y[i] > 0)
        y[i] -= d[j], putchar('U');
      else
        y[i] += d[j], putchar('D');
    }
    puts("");
  }
  return 0;
}
Problem p03247 (C++)

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

using namespace std;
using ll = long long;

bool check(pair<ll, ll> a, int lim) {
  return abs(a.first) + abs(a.second) <= lim;
}

int main() {
  freopen("test.in", "r", stdin);
  int n;
  cin >> n;
  vector<pair<int, int>> a(n);
  int parity = -1;
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    if (parity == -1)
      parity = abs(x + y) % 2;
    else {
      if (parity != abs(x + y) % 2)
        return cout << "-1\n", 0;
    }
    a[i] = {x, y};
  }
  int m = 31;
  vector<int> d(m);
  d[0] = 1;
  for (int i = 1; i < m; ++i)
    d[i] = 2 * d[i - 1];
  if (parity == 0)
    d.push_back(1), sort(d.begin(), d.end()), m++;
  reverse(d.begin(), d.end());
  cout << m << '\n';
  for (int i = 0; i < m; ++i)
    cout << d[i] << " ";
  cout << '\n';
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      int lim = d[j] - parity;
      if (check(make_pair(a[i].first - d[j], a[i].second), lim))
        cout << "R", a[i].first -= d[j];
      else if (check(make_pair(a[i].first + d[j], a[i].second), lim))
        cout << "L", a[i].first += d[j];
      else if (check(make_pair(a[i].first, a[i].second - d[j]), lim))
        cout << "U", a[i].second -= d[j];
      else if (check(make_pair(a[i].first, a[i].second + d[j]), lim))
        cout << "D", a[i].second += d[j];
      else
        cout << "?";
    }
    cout << '\n';
  }
}


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

using namespace std;
using ll = long long;

bool check(pair<ll, ll> a, int lim) {
  return abs(a.first) + abs(a.second) <= lim;
}

int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> a(n);
  int parity = -1;
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    if (parity == -1)
      parity = abs(x + y) % 2;
    else {
      if (parity != abs(x + y) % 2)
        return cout << "-1\n", 0;
    }
    a[i] = {x, y};
  }
  int m = 31;
  vector<int> d(m);
  d[0] = 1;
  for (int i = 1; i < m; ++i)
    d[i] = 2 * d[i - 1];
  if (parity == 0)
    d.push_back(1), sort(d.begin(), d.end()), m++;
  reverse(d.begin(), d.end());
  cout << m << '\n';
  for (int i = 0; i < m; ++i)
    cout << d[i] << " ";
  cout << '\n';
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      int lim = d[j] - parity;
      if (check(make_pair(a[i].first - d[j], a[i].second), lim))
        cout << "R", a[i].first -= d[j];
      else if (check(make_pair(a[i].first + d[j], a[i].second), lim))
        cout << "L", a[i].first += d[j];
      else if (check(make_pair(a[i].first, a[i].second - d[j]), lim))
        cout << "U", a[i].second -= d[j];
      else if (check(make_pair(a[i].first, a[i].second + d[j]), lim))
        cout << "D", a[i].second += d[j];
      else
        cout << "?";
    }
    cout << '\n';
  }
}
Problem p03248 (C++)

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

int main() {

  string s;
  cin >> s;
  int n = (int)s.size();
  // 1 indexed にする
  s = "x" + s;
  if (s[1] == '0') {
    cout << -1 << endl;
    return 0;
  }

  if (s[n] == '1') {
    cout << -1 << endl;
    return 0;
  }

  for (int i = 1; i < n / 2; i++) {
    if (s[i] != s[n - i]) {
      cout << -1 << endl;
      return 0;
    }
  }

  int a = 0;
  int b = 1;
  cout << b / a << endl;
  return 0;
}

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

int main() {

  string s;
  cin >> s;
  int n = (int)s.size();
  // 1 indexed にする
  s = "x" + s;
  if (s[1] == '0') {
    cout << -1 << endl;
    return 0;
  }

  if (s[n] == '1') {
    cout << -1 << endl;
    return 0;
  }

  for (int i = 1; i < n / 2; i++) {
    if (s[i] != s[n - i]) {
      cout << -1 << endl;
      return 0;
    }
  }

  // この時点で構築可能
  vector<pair<int, int>> ans;
  int cur = 0;
  for (int i = 1; i < n; i++) {
    cout << cur + 1 << " " << i + 1 << endl;
    if (s[i] == '1')
      cur = i;
  }

  return 0;
}
Problem p03248 (C++)

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

using namespace std;

int n;
string s;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> s;
  for (int i = 1; i < s.length() - 1; i++)
    if (s[0] == '0' || s[s.length() - 1] == '1' ||
        s[i] != s[s.length() - i - 2]) {
      cout << -1;
      return 1;
    }
  cout << 1 << " " << 2 << endl;
  int last = 1;
  for (int i = 1; i <= s.length() - 2; i++) {
    cout << last << " " << i + 2 << endl;
    if (s[i] == '1')
      last = i + 2;
  }
}


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

using namespace std;

int n;
string s;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> s;
  for (int i = 1; i < s.length() - 1; i++)
    if (s[0] == '0' || s[s.length() - 1] == '1' ||
        s[i] != s[s.length() - i - 2]) {
      cout << -1;
      return 0;
    }
  cout << 1 << " " << 2 << endl;
  int last = 1;
  for (int i = 1; i <= s.length() - 2; i++) {
    cout << last << " " << i + 2 << endl;
    if (s[i] == '1')
      last = i + 2;
  }
}
Problem p03248 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
  string s, s_rev;
  int n, j;
  cin >> s;
  n = s.length();
  if (s[n - 1] == '1') {
    cout << "-1";
    return 0;
  }
  s.erase(n - 1);
  reverse(s.begin(), s.end());
  s_rev = s;
  reverse(s.begin(), s.end());
  if (s != s_rev) {
    cout << "-1";
    return 0;
  }
  for (int i = 0; i < n - 1; i++) {
    if (s[i] == '1') {
      cout << i + 1 << ' ' << i + 2 << '\n';
    } else {
      j = i;
      cout << i + 1 << ' ' << i + 2 << '\n';
      while (s[j] != '1') {
        cout << i + 1 << ' ' << j + 3 << '\n';
        j++;
      }
      i = j;
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
  string s, s_rev;
  int n, j;
  cin >> s;
  n = s.length();
  if (s[n - 1] == '1' || s[0] == '0') {
    cout << "-1";
    return 0;
  }
  s.erase(n - 1);
  reverse(s.begin(), s.end());
  s_rev = s;
  reverse(s.begin(), s.end());
  if (s != s_rev) {
    cout << "-1";
    return 0;
  }
  for (int i = 0; i < n - 1; i++) {
    if (s[i] == '1') {
      cout << i + 1 << ' ' << i + 2 << '\n';
    } else {
      j = i;
      cout << i + 1 << ' ' << i + 2 << '\n';
      while (s[j] != '1') {
        cout << i + 1 << ' ' << j + 3 << '\n';
        j++;
      }
      i = j;
    }
  }
  return 0;
}
Problem p03248 (C++)

Buggy submission (status: Runtime Error):
#define pnode node *
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<ll, ll, hash<ll>> ht;

void bad() {
  cout << -1 << "\n";
  exit(0);
}

ll n;
string s;
vector<ll> a;
void solve() {
  cin >> s;
  n = s.size();
  a.resize(n + 1);
  for (ll i = 1; i <= n; ++i) {
    a[i] = s[i - 1] - '0';
  }
  if (!a[1] || a[n])
    bad();
  for (ll i = 1; i <= n; ++i) {
    if (a[i] != a[n - i])
      bad();
  }
  vector<pair<ll, ll>> b;
  ll curid = 1;
  ll prvver = 0;
  ll prv = 1;
  for (ll i = 2; i < n; ++i) {
    if (a[i] == 0)
      continue;
    ll cid = curid;
    b.push_back({curid, prvver});
    for (ll j = curid + 1; j < curid + (i - prv); ++j) {
      b.push_back({curid, j});
    }
    prvver = curid;
    curid = curid + (i - prv);
  }
  if (curid != n) {
    b.push_back({curid, prvver});
    for (ll i = curid + 1; i < n; ++i) {
      b.push_back({curid, i});
    }
  }
  for (auto i : b) {
    cout << i.first + 1 << " " << i.second + 1 << "\n";
  }
}

signed main() {
  int t = 1;
  srand(time(0));
#ifdef DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // cin >> t
  while (t--) {
    solve();
  }
}


Fixed submission:
#define pnode node *
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<ll, ll, hash<ll>> ht;

void bad() {
  cout << -1 << "\n";
  exit(0);
}

ll n;
string s;
vector<ll> a;
void solve() {
  cin >> s;
  n = s.size();
  a.resize(n + 1);
  for (ll i = 1; i <= n; ++i) {
    a[i] = s[i - 1] - '0';
  }
  if (!a[1] || a[n])
    bad();
  for (ll i = 1; i <= n; ++i) {
    if (a[i] != a[n - i])
      bad();
  }
  vector<pair<ll, ll>> b;
  ll curid = 1;
  ll prvver = 0;
  ll prv = 1;
  for (ll i = 2; i < n; ++i) {
    if (a[i] == 0)
      continue;
    ll cid = curid;
    b.push_back({curid, prvver});
    for (ll j = curid + 1; j < curid + (i - prv); ++j) {
      b.push_back({curid, j});
    }
    prvver = curid;
    curid = curid + (i - prv);
    prv = i;
  }
  if (curid != n) {
    b.push_back({curid, prvver});
    for (ll i = curid + 1; i < n; ++i) {
      b.push_back({curid, i});
    }
  }
  for (auto i : b) {
    cout << i.first + 1 << " " << i.second + 1 << "\n";
  }
}

signed main() {
  int t = 1;
  srand(time(0));
#ifdef DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  // cin >> t
  while (t--) {
    solve();
  }
}
Problem p03248 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
#include <set>

using std::set;

int u[101000], v[101000], tot = 0;
char c[101000];
set<int> st;

void dfs(int x, int siz) {
  int rt = x;
  siz--;
  x++;
  while (1) {
    set<int>::iterator it = st.lower_bound(siz);
    if (it == st.begin() && (*it) != siz)
      break;
    if ((*it) != siz)
      --it;
    u[++tot] = rt;
    v[tot] = x;
    int new_rt = x;
    x += (*it);
    siz -= (*it);
    st.erase(it);
    dfs(new_rt, (*it));
  }
  while (siz)
    u[++tot] = rt, v[tot] = x, x++, siz--;
}
int main() {
  scanf("%s", c + 1);
  int n = strlen(c + 1);
  for (int i = 1; i <= (n >> 1); i++) {
    if (c[i] != c[n - i])
      return 0 * puts("-1");
  }
  if (c[1] != '1' || c[n] == '1')
    return 0 * puts("-1");
  for (int i = 2; i <= (n >> 1); i++)
    if (c[i] == '1')
      st.insert(i);
  dfs(1, n);
  if (st.size() != 0)
    return 0 * puts("-1");
  for (int i = 1; i < n; i++)
    printf("%d %d\n", u[i], v[i]);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <set>

using std::set;

int u[101000], v[101000], tot = 0;
char c[101000];
set<int> st;

void dfs(int x, int siz) {
  int rt = x;
  siz--;
  x++;
  while (1) {
    set<int>::iterator it = st.lower_bound(siz);
    if (st.size() == 0 || (it == st.begin() && (*it) != siz))
      break;
    if ((*it) != siz)
      --it;
    u[++tot] = rt;
    v[tot] = x;
    int new_rt = x;
    x += (*it);
    siz -= (*it);
    st.erase(it);
    dfs(new_rt, (*it));
  }
  while (siz)
    u[++tot] = rt, v[tot] = x, x++, siz--;
}
int main() {
  scanf("%s", c + 1);
  int n = strlen(c + 1);
  for (int i = 1; i <= (n >> 1); i++) {
    if (c[i] != c[n - i])
      return 0 * puts("-1");
  }
  if (c[1] != '1' || c[n] == '1')
    return 0 * puts("-1");
  for (int i = 2; i <= (n >> 1); i++)
    if (c[i] == '1')
      st.insert(i);
  dfs(1, n);
  if (st.size() != 0)
    return 0 * puts("-1");
  for (int i = 1; i < n; i++)
    printf("%d %d\n", u[i], v[i]);
  return 0;
}
Problem p03248 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ctime>
#include <iostream>
using namespace std;
#define _LL long long
#define rep(i, n) for (_LL i = 0; i < (_LL)(n); i++)
#define vecrep(itr, v) for (auto itr = (v).begin(); itr != (v).end(); itr++)

bool check(vector<pair<int, int>> &branch) {
  string s;
  cin >> s;
  int n = s.length();
  int nodeindex = 0;
  int lastparent = 0;
  int lastnum = 0;
  if (s[n - 1] == '1')
    return false;
  rep(i, n / 2) {
    if (s[i] != s[n - 2 - i])
      return false;
    if (i == 0) {
      if (s[i] != '1')
        return false;
      lastparent = nodeindex++;
      lastnum = i;
    } else {
      if (s[i] == '1') {
        branch.emplace_back(lastparent, nodeindex);
        lastparent = nodeindex;
        nodeindex++;

        while (i > lastnum + 1) {
          branch.emplace_back(lastparent, nodeindex++);
        }
        lastnum = i;
      }
    }
  }

  int tsize = nodeindex;
  int t1top = lastparent;
  int branchsize = branch.size();
  int t2top = lastparent + tsize;
  rep(j, branchsize) {
    branch.emplace_back(branch[j].first + tsize, branch[j].second + tsize);
  }
  nodeindex += nodeindex;

  branch.emplace_back(t1top, t2top);
  while (nodeindex < n) {
    branch.emplace_back(t2top, nodeindex++);
  }

  return true;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  vector<pair<int, int>> branch;
  if (check(branch)) {
    vecrep(itr, branch) {
      cout << itr->first + 1 << " " << itr->second + 1 << endl;
    }
  } else {
    cout << -1 << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#include <ctime>
#include <iostream>
using namespace std;
#define _LL long long
#define rep(i, n) for (_LL i = 0; i < (_LL)(n); i++)
#define vecrep(itr, v) for (auto itr = (v).begin(); itr != (v).end(); itr++)

bool check(vector<pair<int, int>> &branch) {
  string s;
  cin >> s;
  int n = s.length();
  int nodeindex = 0;
  int lastparent = 0;
  int lastnum = 0;
  if (s[n - 1] == '1')
    return false;
  rep(i, n / 2) {
    if (s[i] != s[n - 2 - i])
      return false;
    if (i == 0) {
      if (s[i] != '1')
        return false;
      lastparent = nodeindex++;
      lastnum = i;
    } else {
      if (s[i] == '1') {
        branch.emplace_back(lastparent, nodeindex);
        lastparent = nodeindex;
        nodeindex++;

        while (i > lastnum + 1) {
          branch.emplace_back(lastparent, nodeindex++);
          lastnum++;
        }
        lastnum = i;
      }
    }
  }

  int tsize = nodeindex;
  int t1top = lastparent;
  int branchsize = branch.size();
  int t2top = lastparent + tsize;
  rep(j, branchsize) {
    branch.emplace_back(branch[j].first + tsize, branch[j].second + tsize);
  }
  nodeindex += nodeindex;

  branch.emplace_back(t1top, t2top);
  while (nodeindex < n) {
    branch.emplace_back(t2top, nodeindex++);
  }

  return true;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  vector<pair<int, int>> branch;
  if (check(branch)) {
    vecrep(itr, branch) {
      cout << itr->first + 1 << " " << itr->second + 1 << endl;
    }
  } else {
    cout << -1 << endl;
  }
}