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

#include <bits/stdc++.h>

using LL = long long int;

using namespace std;

string S;

string calc(bool flag, int k) {
  string ret;
  vector<int> vec;
  for (int i = 0; i < 10; i++) {
    vec.push_back(i);
  }

  int N = S.length();
  for (size_t i = 0; i < k; i++) {
    int num = S[i] - '0';

    bool forloop = false;
    for (size_t j = 0; j < vec.size(); j++) {
      if (vec[j] == num) {
        forloop = true;
        ret += num + '0';
        vec.erase(vec.begin() + j);
      }
    }

    if (!forloop) {
      return "";
    }
  }

  if (k == N) {
    return ret;
  }

  int num = S[k] - '0';

  //??§???????????????????????????
  if (!flag) {
    auto it = upper_bound(vec.begin(), vec.end(), num);

    if (it == vec.end()) {
      return "";
    }

    ret += (*it) + '0';
    vec.erase(it);

    for (size_t j = 0; j < vec.size() && ret.length() < N; j++) {
      ret += vec[j] + '0';
    }

    if (ret.length() != N) {
      return "";
    }

    return ret;
  }
  //?°????????\\´???????????????
  else {
    auto it = lower_bound(vec.begin(), vec.end(), num);

    if (it == vec.begin()) {
      return "";
    }

    it--;
    ret += (*it) + '0';
    vec.erase(it);

    for (int j = vec.size() - 1; 0 <= j && ret.length() < N; j--) {
      ret += vec[j] + '0';
    }

    if (ret.length() != N) {
      return "";
    }

    return ret;
  }

  return ret;
}

LL convToLL(string str) {
  LL ret = 0;
  for (size_t i = 0; i < str.length(); i++) {
    ret *= 10;
    ret += str[i] - '0';
  }
  return ret;
}

string convToString(LL num, int k) {
  string ret;
  while (num) {
    ret += (num % 10) + '0';
    num /= 10;
  }
  while (ret.length() < k) {
    ret += '0';
  }

  reverse(begin(ret), end(ret));

  return ret;
}

bool comp(const string &A, const string &B) {
  if (A == "") {
    return false;
  } else if (B == "") {
    return true;
  } else {
    LL AA = convToLL(A), BB = convToLL(B);
    LL tmpS = convToLL(S);
    if (min(abs(AA - tmpS), (LL)pow((LL)10, S.length()) - (LL)abs(AA - tmpS)) >
        min(abs(BB - tmpS), (LL)pow((LL)10, S.length()) - (LL)abs(BB - tmpS))) {
      return false;
    } else if (min(abs(BB - tmpS),
                   (LL)pow((LL)10, S.length()) - (LL)abs(BB - tmpS)) >
               min(abs(AA - tmpS),
                   (LL)pow((LL)10, S.length()) - (LL)abs(AA - tmpS))) {
      return true;
    } else {
      if (A < B) {
        return true;
      } else {
        return false;
      }
    }
  }
}

int main() {
  cin >> S;

  LL tmpS = convToLL(S);

  tmpS -= pow(10, S.length()) / 2;
  if (tmpS < 0) {
    tmpS += pow(10, S.length());
  }

  S = convToString(tmpS, S.length());

  vector<string> ans;
  for (size_t i = 0; i <= S.size(); i++) {
    string tmp = calc(false, i);
    if (tmp != "") {
      ans.push_back(tmp);
    }
    tmp = calc(true, i);
    if (tmp != "") {
      ans.push_back(tmp);
    }
  }

  sort(begin(ans), end(ans), comp);

  cout << ans[0] << endl;

  return 0;
}

Error: 0
701 701 701


Fixed submission:

#include <bits/stdc++.h>

using LL = long long int;

using namespace std;

string S;

string calc(bool flag) {
  string ret;
  vector<int> vec;
  for (int i = 0; i < 10; i++) {
    vec.push_back(i);
  }

  int N = S.length();
  for (size_t i = 0; i < N; i++) {
    int num = S[i] - '0';

    bool forloop = false;

    for (size_t j = 0; j < vec.size(); j++) {
      if (vec[j] == num) {
        forloop = true;
        ret += num + '0';
        vec.erase(vec.begin() + j);
      }
    }

    if (forloop) {
      continue;
    }

    // テ・ツ、ツァテ」ツ?催」ツ??」ツつ?」ツ?、テ」ツ?ョティツソツ妥」ツ??」ツつ?」ツ?、
    if (!flag) {
      auto it = upper_bound(vec.begin(), vec.end(), num);

      if (it == vec.end()) {
        return "";
      }

      ret += (*it) + '0';
      vec.erase(it);

      for (size_t j = 0; j < vec.size() && ret.length() < N; j++) {
        ret += vec[j] + '0';
      }

      if (ret.length() != N) {
        return "";
      }

      return ret;
    }
    // テ・ツーツ湘」ツ?陛」ツ??・ツ・ツエテ」ツ?ョティツソツ妥」ツ??」ツつ?」ツ?、
    else {
      auto it = lower_bound(vec.begin(), vec.end(), num);

      if (it == vec.begin()) {
        return "";
      }

      it--;
      ret += (*it) + '0';
      vec.erase(it);

      for (int j = vec.size() - 1; 0 <= j && ret.length() < N; j--) {
        ret += vec[j] + '0';
      }

      if (ret.length() != N) {
        return "";
      }

      return ret;
    }
  }

  return ret;
}

LL convToLL(string str) {
  LL ret = 0;
  for (size_t i = 0; i < str.length(); i++) {
    ret *= 10;
    ret += str[i] - '0';
  }
  return ret;
}

string convToString(LL num, int k) {
  string ret;
  while (num) {
    ret += (num % 10) + '0';
    num /= 10;
  }
  while (ret.length() < k) {
    ret += '0';
  }

  reverse(begin(ret), end(ret));

  return ret;
}

int main() {
  cin >> S;

  LL tmpS = convToLL(S);

  tmpS -= pow(10, S.length()) / 2;
  if (tmpS < 0) {
    tmpS += pow(10, S.length());
  }

  S = convToString(tmpS, S.length());

  string A = calc(false), B = calc(true);

  cerr << A << " " << B << " " << tmpS << endl;

  if (A == "") {
    cout << B << endl;
  } else if (B == "") {
    cout << A << endl;
  } else {
    LL AA = convToLL(A), BB = convToLL(B);
    if (min(abs(AA - tmpS), (LL)pow((LL)10, S.length()) - (LL)abs(AA - tmpS)) <
        min(abs(BB - tmpS), (LL)pow((LL)10, S.length()) - (LL)abs(BB - tmpS))) {
      cout << A << endl;
    } else if (min(abs(BB - tmpS),
                   (LL)pow((LL)10, S.length()) - (LL)abs(BB - tmpS)) <
               min(abs(AA - tmpS),
                   (LL)pow((LL)10, S.length()) - (LL)abs(AA - tmpS))) {
      cout << B << endl;
    } else {
      if (A < B) {
        cout << A << endl;
      } else {
        cout << B << endl;
      }
    }
  }

  return 0;
}