Problem p01829 (C++) — buggy submission, verdict: Runtime Error

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for (int64 i = 0; i < (n); i++)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value != 0>::type fill_v(U &u, const V... v) {
  u = U(v...);
}

template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value == 0>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

string s;

int64 ten(int64 x, int64 d) {
  REP(i, d) x *= 10;
  return x;
}

int64 stoi_my(string s) {
  int64 ret = 0;
  REP(i, s.size()) ret = ret * 10 + s[i] - '0';
  return ret;
}

int64 score(int64 a) {
  int64 ss = stoi_my(s);
  return min(abs(ss - a), ten(1, s.size()) - abs(ss - a));
}

int main(void) {
  vector<int64> v(10, 0);
  cin >> s;
  REP(i, s.size()) { v[i] = i + 1; }
  sort(all(v));
  int64 ans = 0;
  do {
    string res(s.size(), '.');
    REP(i, v.size()) {
      if (v[i] != 0) {
        res[v[i] - 1] = '0' + i;
      }
    }
    if (score(ans) <= score(stoi_my(res))) {
      if (score(ans) < score(stoi_my(res)) || ans > stoi_my(res))
        ans = stoi_my(res);
    }
  } while (next_permutation(all(v)));
  cout << setfill('0') << setw(s.size()) << ans << endl;
}


Error: 0

Fixed submission:

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for (int64 i = 0; i < (n); i++)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value != 0>::type fill_v(U &u, const V... v) {
  u = U(v...);
}

template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value == 0>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

string s;

int64 ten(int64 x, int64 d) { return pow(10, d) * x; }

int64 score(int64 a) {
  int64 ss = stoi(s);
  return min(abs(ss - a), ten(1, s.size()) - abs(ss - a));
}

int64 calc(int64 h) {
  int64 used = 1 << h;
  int64 res = h;
  int64 d = 1;
  for (; d < s.size() && (used >> (s[d] - '0') & 1) == 0; d++) {
    res = res * 10 + s[d] - '0';
    used |= 1 << (s[d] - '0');
  }
  int64 r = stoi(s);
  if (d < s.size()) {
    REP(i, 10) {
      if (used >> i & 1)
        continue;
      int64 ret = res * 10 + i;
      int64 x = ret;
      int64 cnt = d + 1;
      used |= 1 << i;
      for (int64 j = 9; j >= 0; j--) {
        if (cnt == s.size())
          break;
        if (used >> j & 1)
          continue;
        x = x * 10 + j;
        cnt++;
      }
      if (score(r) < score(x))
        r = x;
      cnt = d + 1;
      x = ret;
      REP(j, 10) {
        if (cnt == s.size())
          break;
        if (used >> j & 1)
          continue;
        x = x * 10 + j;
        cnt++;
      }
      used ^= 1 << i;
      if (score(r) < score(x))
        r = x;
    }
  } else {
    r = res;
  }
  return r;
}

int main(void) {
  cin >> s;
  int64 res = stoi(s);
  int64 ret;
  if (s[0] - '0' - 5 >= 0) {
    ret = calc(s[0] - '0' - 5);
  }
  if (s[0] - '0' + 5 <= 9) {
    res = calc(s[0] - '0' + 5);
    if (score(res) > score(ret))
      ret = res;
  }
  cout << setfill('0') << setw(s.size()) << ret << endl;
}