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

#include <bits/stdc++.h>
#define ll long long int
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb emplace_back
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define pb emplace_back
#define deb(x) cout << #x << " - " << x << endl;

using namespace std;

vector<ll> v(1000001);
queue<ll> q;
ll ptr = 9;

void showq(queue<int> gq) {
  queue<int> g = gq;
  while (!g.empty()) {
    cout << '\\t' << g.front();
    g.pop();
  }
  cout << '\\n';
}

int main() {
  IOS;
  // #ifndef ONLINE_JUDGE
  //	freopen("input.txt", "r", stdin);
  //	freopen("output.txt", "w", stdout);
  // #endif
  for (ll i = 1; i <= 9; i++) {
    v[i - 1] = i;
    q.push(i);
  }
  while (!q.empty()) {
    ll n = q.front();
    // showq(q);
    q.pop();
    ll rem = n % 10;
    ll start = n * 10 + rem - 1;
    ll end = start + 2;

    if (ptr == 1000001)
      break;
    if (rem == 9)
      end--;
    if (rem == 0)
      start++;
    for (ll i = start; i <= end; i++) {
      if (ptr == 1000001)
        break;
      v[ptr++] = i;
      q.push(i);
    }
  }
  ll n;
  cin >> n;
  cout << v[n - 1];
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define pb emplace_back
#define vi vector<int>
#define all(x) (x).begin(), (x).end()
#define pb emplace_back
#define deb(x) cout << #x << " - " << x << endl;

using namespace std;

int main() {
  IOS;
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // #endif
  ll d1, d2, x, k;
  cin >> k;
  if (k <= 12)
    cout << k;
  else if (k <= 38000) {
    k -= 12;
    ll cnt = 0;
    for (ll i = 21; i <= 3234566669; i++) {
      ll n = i, x = n / 10;
      if (n < 100 && abs((n % 10) - (x)) <= 1) {
        cnt++;
        // deb(i);
        if (cnt == k) {
          cout << i;
          break;
        }
      } else {
        bool flag = true;
        while (n > 10) {
          d1 = n % 10;
          x = n / 10;
          d2 = x % 10;
          if (abs(d1 - d2) > 1) {
            flag = false;
            break;
          }
          n /= 10;
        }
        if (flag) {
          // deb(i);
          cnt++;
        }
        if (cnt == k) {
          cout << i;
          break;
        }
      }
    }
  } else {
    k -= 38000;
    ll cnt = 0;
    for (ll i = 345443321; i <= 3234566669; i++) {
      ll n = i;
      bool flag = true;
      while (n > 10) {
        d1 = n % 10;
        x = n / 10;
        d2 = x % 10;
        if (abs(d1 - d2) > 1) {
          flag = false;
          break;
        }
        n /= 10;
      }
      if (flag) {
        // deb(i);
        cnt++;
      }
      if (cnt == k) {
        cout << i;
        break;
      }
    }
  }
  return 0;
}