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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<long long, long long>
#define mod 1000000007
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

ll power(ll a, ll b) {
  ll ans = 1;
  while (b > 0) {
    if (b % 2 != 0) {
      ans = (ans * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}

int main() {
  IOS;
  int t = 1;
  // cin>>t;
  while (t--) {
    int k;
    cin >> k;
    vector<vector<pair<int, int>>> v;
    int cnt = 0;
    vector<pair<int, int>> z;
    z.push_back({1, -1});
    z.push_back({2, -1});
    z.push_back({3, -1});
    z.push_back({4, -1});
    z.push_back({5, -1});
    z.push_back({6, -1});
    z.push_back({7, -1});
    z.push_back({8, -1});
    z.push_back({9, -1});
    int i = 0;
    string s;
    int o = -1;
    while (1) {
      vector<pair<int, int>> ha;
      int j = 0;
      for (int ko = 0; ko < z.size(); ko++) {
        cnt++;
        if (cnt == k) {
          s += z[ko].first + '0';
          j = z[ko].second;
          i = v.size() - 1;
          while (i >= 0) {
            s += v[i][j].first + '0';
            j = v[i][j].second;
            i = i - 1;
          }
          reverse(s.begin(), s.end());
          cout << s;
          return 0;
        }
        int hl = z[ko].first;
        if (hl >= 1 && hl <= 8) {
          ha.push_back({hl - 1, ko});
          ha.push_back({hl, ko});
          ha.push_back({hl + 1, ko});
        } else if (hl == 0) {
          ha.push_back({hl, ko});
          ha.push_back({hl + 1, ko});
        } else {
          ha.push_back({hl - 1, ko});
          ha.push_back({hl, ko});
        }
      }
      v.push_back(z);
      z = ha;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<long long, long long>
#define mod 1000000007
#define IOS                                                                    \\
  std::ios::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#pragma warn - rvl /* return value */
#pragma warn - par /* parameter never used */
#pragma warn - rch /*unreachable code */

ll power(ll a, ll b) {
  ll ans = 1;
  while (b > 0) {
    if (b % 2 != 0) {
      ans = (ans * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}

int main() {
  IOS;
  int t = 1;
  // cin>>t;
  while (t--) {
    int k;
    cin >> k;
    int cnt = 0;
    if (k <= 32660) {
      cnt = 0;
      for (ll i = 1; i <= 234567899; i++) {
        ll ha = i;
        int o = 1;
        int last = ha % 10;
        ha /= 10;
        while (ha) {
          int p = ha % 10;
          if (abs(p - last) > 1) {
            o = 0;
            break;
          }
          last = p;
          ha /= 10;
        }
        cnt += o;
        if (cnt == k) {
          cout << i;
          return 0;
        }
      }
      return 0;
    }

    if (k <= 51241) {
      cnt = 32660;
      for (ll i = 321000000; i <= 567899999; i++) {
        ll ha = i;
        int o = 1;
        int last = ha % 10;
        ha /= 10;
        while (ha) {
          int p = ha % 10;
          if (abs(p - last) > 1) {
            o = 0;
            break;
          }
          last = p;
          ha /= 10;
        }
        cnt += o;
        if (cnt == k) {
          cout << i;
          return 0;
        }
      }
      return 0;
    }
    if (k <= 66345) {
      cnt = 51241;
      for (ll i = 654321000; i < 900000000; i++) {
        ll ha = i;
        int o = 1;
        int last = ha % 10;
        ha /= 10;
        while (ha) {
          int p = ha % 10;
          if (abs(p - last) > 1) {
            o = 0;
            break;
          }
          last = p;
          ha /= 10;
        }
        cnt += o;
        if (cnt == k) {
          cout << i;
          return 0;
        }
      }
      return 0;
    }

    if (k <= 79724) {
      cnt = 66345;

      for (ll i = 987654321; i <= 1234567900; i++) {
        ll ha = i;
        int o = 1;
        int last = ha % 10;
        ha /= 10;
        while (ha) {
          int p = ha % 10;
          if (abs(p - last) > 1) {
            o = 0;
            break;
          }
          last = p;
          ha /= 10;
        }
        cnt += o;
        if (cnt == k) {
          cout << i;
          return 0;
        }
      }

      return 0;
    }

    if (k <= 94828) {
      cnt = 79724;
      if (k <= 83647) {
        cnt = 79724;
        for (ll i = 2100000000; i <= 2123456789; i++) {
          ll ha = i;
          int o = 1;
          int last = ha % 10;
          ha /= 10;
          while (ha) {
            int p = ha % 10;
            if (abs(p - last) > 1) {
              o = 0;
              break;
            }
            last = p;
            ha /= 10;
          }
          cnt += o;
          if (cnt == k) {
            cout << i;
            return 0;
          }
        }
      } else if (k <= 88857) {
        cnt = 83647;
        for (ll i = 2210000000; i <= 2234567899; i++) {
          ll ha = i;
          int o = 1;
          int last = ha % 10;
          ha /= 10;
          while (ha) {
            int p = ha % 10;
            if (abs(p - last) > 1) {
              o = 0;
              break;
            }
            last = p;
            ha /= 10;
          }
          cnt += o;
          if (cnt == k) {
            cout << i;
            return 0;
          }
        }
      } else {
        cnt = 88857;
        for (ll i = 2321000000; i <= 2345678999; i++) {
          ll ha = i;
          int o = 1;
          int last = ha % 10;
          ha /= 10;
          while (ha) {
            int p = ha % 10;
            if (abs(p - last) > 1) {
              o = 0;
              break;
            }
            last = p;
            ha /= 10;
          }
          cnt += o;
          if (cnt == k) {
            cout << i;
            return 0;
          }
        }
      }
      return 0;
    }
    if (k > 1) {
      cnt = 94828;
      for (ll i = 3210000000; i <= 3456789999; i++) {
        ll ha = i;
        int o = 1;
        int last = ha % 10;
        ha /= 10;
        while (ha) {
          int p = ha % 10;
          if (abs(p - last) > 1) {
            o = 0;
            break;
          }
          last = p;
          ha /= 10;
        }
        cnt += o;
        if (cnt == k) {
          cout << i;
          return 0;
        }
      }
      return 0;
    }
  }
  return 0;
}