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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define lli long long int
#define fr(i, a, b) for (lli i = a; i < b; i++)
#define bfr(i, a, b) for (lli i = a; i >= b; i--)
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL), cout.tie(NULL);
#define tc                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define mod 1000000007LL
#define mod2 998244353LL
#define all(vec) vec.begin(), vec.end()
#define ld long double
#define pb push_back

#define ordered_set                                                            \\
  tree<lli, null_type, less<lli>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

bool check(string s) {
  fr(i, 0, s.length() - 1) if (abs((lli)(s[i] - s[i + 1])) > 1) return false;
  return true;
}
int main() {

  fio

      vector<lli>
          ans;
  vector<string> temp, temp2;
  fr(i, 1, 10) ans.push_back(i);
  fr(i, 0, 10) temp.push_back(to_string(i));

  lli n;
  cin >> n;

  fr(l, 0, 10) {
    if (l % 2 == 0)
      temp2.clear();
    else
      temp.clear();
    fr(i, 0, 10) {

      if (l % 2 == 0) {
        fr(j, 0, temp.size()) {
          string s = to_string(i) + temp[j];
          if (check(s) && i != 0) {
            lli o = stoll(s);
            ans.pb(o);
          }
          if (check(s))
            temp2.push_back(s);
          if (ans.size() > n)
            break;
        }
      } else {

        fr(j, 0, temp2.size()) {
          string s = to_string(i) + temp2[j];
          if (check(s) && i != 0) {
            lli o = stoll(s);
            ans.pb(o);
          }
          if (check(s))
            temp.push_back(s);
          if (ans.size() > n)
            break;
        }
      }
      if (ans.size() > n)
        break;
    }
    if (ans.size() > n)
      break;
    // cout<<ans.size()<<'\\n';
  }

  cout << ans[n - 1];

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define lli long long int
#define fr(i, a, b) for (lli i = a; i < b; i++)
#define bfr(i, a, b) for (lli i = a; i >= b; i--)
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL), cout.tie(NULL);
#define tc                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define mod 1000000007LL
#define mod2 998244353LL
#define all(vec) vec.begin(), vec.end()
#define ld long double
#define pb push_back

#define ordered_set                                                            \\
  tree<lli, null_type, less<lli>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

int main() {

  fio

      lli cnt = 0,
          x;
  lli k;
  cin >> k;

  fr(i, 1, 1e8) {
    vector<lli> p;
    bool flag = true;
    x = i;
    while (x > 0) {
      p.push_back(x % 10);
      x /= 10;
    }
    fr(i, 0, p.size() - 1) if (abs(p[i] - p[i + 1]) > 1) flag = false;
    if (flag)
      cnt++;
    if (cnt == k) {
      cout << i;
      return 0;
    }
  }

  return 0;
}