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

#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define ff first
#define ss second
#define file_se_input                                                          \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define pp long long int
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define zip(i, a, b) for (int i = a; i < b; i++)
#define ll unsigned long long int
#define test                                                                   \\
  long int t;                                                                  \\
  cin >> t;                                                                    \\
  while (t--)
#define um unordered_map
#define en '\\n'
#define us unordered_set
#define mm 1000000007

bool check_prime(long long n) {
  int flag = 0;
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      flag = 1;
      break;
    }
  }
  if (n == 1)
    return false;
  else if (flag == 0 || n == 2 || n == 3) {
    return true;
  } else {
    return false;
  }
}

int binaryExponentiation(int x,
                         int n) // function to calculate x raise to power n
{
  int result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = result * x;
    x = x * x;
    n = n / 2;
  }
  return result;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
#endif
  //_______________________________-code
  //starts-______________________________________________________________________________

  ll k;
  cin >> k;
  queue<ll> q;
  rep(i, 1, 9) { q.push(i); }
  ll x;
  while (k--) {
    x = q.front();
    q.pop();
    if (x % 10) {
      q.push(10 * x + x % 10 - 1);
    }
    q.push(10 * x + x % 10);
    if (x % 10 != 9) {
      q.push(10 * x + x % 10 + 1);
    }
  }
  cout << x << en;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define ll unsigned long long int
#define pp long long int
#define pb push_back
#define mp make_pair
#define eb emplace_back

int binaryExponentiation(int x,
                         int n) // function to calculate x raise to power n
{
  int result = 1;
  while (n > 0) {
    if (n % 2 == 1)
      result = result * x;
    x = x * x;
    n = n / 2;
  }
  return result;
}
bool lunlun(ll t) {
  string s = to_string(t);
  int n = s.length();
  int flag = 0;
  for (int i = 0; i < n - 1; i++) {
    if (abs((int)(s[i]) - (int)(s[i + 1])) > 1) {
      flag = 1;
      break;
    }
  }
  if (flag == 0)
    return true;
  else
    return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int k;
  cin >> k;
  //  string s= to_string(k);
  ll i;
  int cnt = 0;
  ll ans = 0;
  for (int i = 1; i <= pow(10, 18); i++) {
    if (lunlun(i)) {
      cnt++;
      if (cnt == k) {
        ans = i;
        break;
      }
    }
  }
  cout << ans << endl;
  return 0;
}