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

// while( !(succeed == try()) );

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

// Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha
// // Alpha

#define int long long int
#define loop(i, s, e) for (int i = s; i < e; i++)
#define endl '\\n'
#define vii vector<int>
#define pii pair<int, int>
#define ff first
#define ss second
#define N 100001
#define pb push_back
#define INF LLONG_MAX
#define inf INT_MAX

void INP();

#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define rtn                                                                    \\
  if (0)                                                                       \\
    return;
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  rtn cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  rtn const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

// Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha
// // Alpha

void alpha() {

  int k;
  cin >> k;

  queue<int> q;
  loop(i, 1, 10) q.push(i);
  int x;

  while (k--) {
    if (k == 0) {
      cout << q.front();
      return;
    }

    x = q.front();
    q.pop();

    if (x % 10 != 0)
      q.push(10 * x + x % 10 - 1);
    q.push(10 * x + x % 10);
    if (x % 10 != 9)
      q.push(10 * x + x % 10 + 1);
  }
}

int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // INP();
  int t = 1;
  // cin>>t;

  while (t--)
    alpha();

  return 0;
}

inline void INP() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}


Error: TLE

Fixed submission:

// while( !(succeed == try()) );

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

// Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha
// // Alpha

#define int long long int
#define loop(i, s, e) for (int i = s; i < e; i++)
#define endl '\\n'
#define vii vector<int>
#define pii pair<int, int>
#define ff first
#define ss second
#define N 100001
#define pb push_back
#define INF LLONG_MAX
#define inf INT_MAX

void INP();

#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define rtn                                                                    \\
  if (0)                                                                       \\
    return;
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  rtn cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  rtn const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

// Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha // Alpha
// // Alpha

bool lunlun(int x) {

  int t = x, lst = -1, curr;
  while (t) {
    curr = t % 10;

    if (lst != -1 and abs(curr - lst) > 1)
      return 0;

    t /= 10;
    lst = curr;
  }

  return 1;
}

void alpha() {
  int k;
  cin >> k;

  int count = 0;

  loop(i, 1, 40000000000) {
    if (lunlun(i))
      count++;
    if (count == k) {
      cout << i;
      return;
    }
  }
}

int32_t main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // INP();
  int t = 1;
  // cin>>t;

  while (t--)
    alpha();

  return 0;
}

inline void INP() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}