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

#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <typeinfo>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define ALL(v) v.begin(), v.end()
#define FS first
#define SC second
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define SQR(x) ((x) * (x))
#define BIT(n) (1LL << (n))
#define PCNT(x) __builtin_popcountll(x)
#define GCD(x, y) __gcd(x, y)
#define LCM(x, y) x / __gcd(x, y) * y
#define PB push_back
#define MP make_pair
#define endl "\\n"
using namespace std;
typedef long long lint;
typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<bool> VB;
typedef vector<vector<int>> VVI;
typedef vector<vector<LL>> VVLL;
typedef vector<vector<bool>> VVB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
const LL INF = 2000000000ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

map<LL, LL> lun;

long long mystoll(string s) {
  LL ans = 0;
  for (int i = s.length() - 1; i >= 0; i--) {
    ans += (s[i] - '0') * pow(10, s.length() - 1 - i);
  }
  return ans;
}

void setdfs(string s) {
  if (s.length() < 11) {
    string temp;
    if (s[0] - '0' > 1 && s[0] - '0' < 9) {
      temp = (char)(s[0] - 1) + s;
      lun[mystoll(temp)]++;
      setdfs(temp);

      temp = s[0] + s;
      lun[mystoll(temp)]++;
      setdfs(temp);

      temp = (char)(s[0] + 1) + s;
      lun[mystoll(temp)]++;
      setdfs(temp);
    } else if (s[0] == '1') {
      temp = '1' + s;
      lun[mystoll(temp)]++;
      setdfs(temp);
      temp = '2' + s;
      lun[mystoll(temp)]++;
      setdfs(temp);
      setdfs('0' + s);
    } else if (s[0] == '0') {
      temp = '1' + s;
      lun[mystoll(temp)]++;
      setdfs('1' + s);
      setdfs('0' + s);
    } else if (s[0] == '9') {
      temp = '9' + s;
      lun[mystoll(temp)]++;
      setdfs('9' + s);

      temp = '8' + s;
      lun[mystoll(temp)]++;
      setdfs('8' + s);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  int K;
  cin >> K;
  FOR(i, 1, 10) lun[i]++;

  setdfs("0");
  setdfs("1");
  setdfs("2");
  setdfs("3");
  setdfs("4");
  setdfs("5");
  setdfs("6");
  setdfs("7");
  setdfs("8");
  setdfs("9");

  VLL ans;

  for (auto itr : lun) {
    ans.push_back(itr.first);
  }
  SORT(ans);
  cout << ans[K - 1] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <typeinfo>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define ALL(v) v.begin(), v.end()
#define FS first
#define SC second
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(), (c).end())
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define SQR(x) ((x) * (x))
#define BIT(n) (1LL << (n))
#define PCNT(x) __builtin_popcountll(x)
#define GCD(x, y) __gcd(x, y)
#define LCM(x, y) x / __gcd(x, y) * y
#define PB push_back
#define MP make_pair
#define endl "\\n"
using namespace std;
typedef long long lint;
typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<bool> VB;
typedef vector<vector<int>> VVI;
typedef vector<vector<LL>> VVLL;
typedef vector<vector<bool>> VVB;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
const LL INF = 2000000000ll;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

map<LL, LL> lun;

long long mystoll(string s) {
  LL ans = 0;
  for (int i = s.length() - 1; i >= 0; i--) {
    ans += (s[i] - '0') * pow(10, s.length() - 1 - i);
  }
  return ans;
}

void setdfs(string s) {
  if (s.length() < 12) {
    string temp;
    if (s[0] - '0' > 1 && s[0] - '0' < 9) {
      temp = (char)(s[0] - 1) + s;
      lun[mystoll(temp)]++;
      setdfs(temp);

      temp = s[0] + s;
      lun[mystoll(temp)]++;
      setdfs(temp);

      temp = (char)(s[0] + 1) + s;
      lun[mystoll(temp)]++;
      setdfs(temp);
    } else if (s[0] == '1') {
      temp = '1' + s;
      lun[mystoll(temp)]++;
      setdfs(temp);
      temp = '2' + s;
      lun[mystoll(temp)]++;
      setdfs(temp);
      setdfs('0' + s);
    } else if (s[0] == '0') {
      temp = '1' + s;
      lun[mystoll(temp)]++;
      setdfs('1' + s);
      setdfs('0' + s);
    } else if (s[0] == '9') {
      temp = '9' + s;
      lun[mystoll(temp)]++;
      setdfs('9' + s);

      temp = '8' + s;
      lun[mystoll(temp)]++;
      setdfs('8' + s);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  int K;
  cin >> K;
  FOR(i, 1, 10) lun[i]++;

  setdfs("0");
  setdfs("1");
  setdfs("2");
  setdfs("3");
  setdfs("4");
  setdfs("5");
  setdfs("6");
  setdfs("7");
  setdfs("8");
  setdfs("9");

  VLL ans;

  for (auto itr : lun) {
    ans.push_back(itr.first);
  }
  SORT(ans);
  cout << ans[K - 1] << endl;
}