alexjercan/bugnet huggingface dataset:
Problem p02720 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define endl "\\n"
typedef long long ll;
#define traceV(v)                                                              \\
  for (auto x : v)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl
#define traceM(mp)                                                             \\
  for (auto x : mp)                                                            \\
  cout << x.first << " " << x.second << endl
#define MOD 1000000007

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  // https://drken1215.hatenablog.com/entry/2020/04/05/150900
  // 解法２
  int K;
  cin >> K;

  queue<ll> q;
  reps(i, 1, 10) q.push(i);
  rep(i, K - 1) { // 小さい方からK-1個popするまで
    ll val = q.front();
    q.pop();
    for (int j = -1; j <= 1; ++j) {
      int add = val % 10 + j;
      if (add >= 0 && add <= 9)
        q.push(val * 10 + add);
    }
  }
  cout << q.front() << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define endl "\\n"
typedef long long ll;
#define traceV(v)                                                              \\
  for (auto x : v)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl
#define traceM(mp)                                                             \\
  for (auto x : mp)                                                            \\
  cout << x.first << " " << x.second << endl
#define MOD 1000000007

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  // https://drken1215.hatenablog.com/entry/2020/04/05/150900
  // 解法２
  int K;
  cin >> K;

  queue<ll> q;
  reps(i, 1, 10) q.push(i), --K;
  while (true) {
    ll val = q.front();
    q.pop();
    for (int j = -1; j <= 1; ++j) {
      int add = val % 10 + j;
      if (add >= 0 && add <= 9)
        q.push(val * 10 + add), --K;
      if (K == 0) {
        cout << q.back() << endl;
        return 0;
      }
    }
  }
}
Problem p02720 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int k;
  long long c[13][10] = {};
  cin >> k;
  rep(i, 10) c[0][i] = 1;
  for (int i = 0;; i++) {
    for (int j = 1; j < 10; j++) {
      if (k > c[i][j]) {
        k -= c[i][j];
      } else {
        long long r = j;
        i--;
        for (; i >= 0; i--) {
          if (j > 0 && c[i][j - 1] >= k) {
            j = j - 1;
            r = r * 10 + j;
            continue;
          }
          if (j > 0)
            k -= c[i][j - 1];
          if (c[i][j] >= k) {
            r = r * 10 + j;
            continue;
          }
          k -= c[i][j];
          j = j + 1;
          r = r * 10 + j;
        }
        cout << r << endl;
        return 0;
      }
    }
    for (int j = 0; j < 10; j++) {
      if (j > 0) {
        c[i + 1][j] += c[i][j - 1];
      }
      if (j < 9) {
        c[i + 1][j] += c[i][j + 1];
      }
      c[i + 1][j] += c[i][j];
    }
  }
}

Error: 23

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int k;
  long long c[13][10] = {};
  cin >> k;
  rep(i, 10) c[0][i] = 1;
  for (int i = 0;; i++) {
    for (int j = 1; j < 10; j++) {
      if (k > c[i][j]) {
        k -= c[i][j];
      } else {
        long long r = j;
        i--;
        for (; i >= 0; i--) {
          if (j > 0 && c[i][j - 1] >= k) {
            j = j - 1;
            r = r * 10 + j;
            continue;
          }
          if (j > 0)
            k -= c[i][j - 1];
          if (c[i][j] >= k) {
            r = r * 10 + j;
            continue;
          }
          k -= c[i][j];
          j = j + 1;
          r = r * 10 + j;
        }
        cout << r << endl;
        return r;
      }
    }
    for (int j = 0; j < 10; j++) {
      if (j > 0) {
        c[i + 1][j] += c[i][j - 1];
      }
      if (j < 9) {
        c[i + 1][j] += c[i][j + 1];
      }
      c[i + 1][j] += c[i][j];
    }
  }
}
Problem p02720 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define INF 1e9
using namespace std;

#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
#define endl "\\n"

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

ll K;
ll maxi = 1e12;
ll mdep = 12;
ll cnt = -1;
void dfs(ll v = 0, int dep = 0) {
  if (v > maxi)
    return;
  if (dep == mdep) {
    cnt++;
    if (cnt == K) {
      cout << v << endl;
      exit(0);
    }
    return;
  }

  if (v == 0) {
    REP(i, 10) dfs(i, dep + 1);
    return;
  }

  auto lk = v % 10;
  if (lk != 0)
    dfs(v * 10 + lk - 1, dep + 1);
  dfs(v * 10 + lk, dep + 1);
  if (lk != 9)
    dfs(v * 10 + lk + 1, dep + 1);
}

void solve() {
  cin >> K;
  dfs();
  cerr << cnt << endl;
}

int main() {
  solve();
  return 0;
}


Error: -11
0


Fixed submission:

#include <bits/stdc++.h>
#define INF 1e9
using namespace std;

#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
#define endl "\\n"

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

ll K;
ll ranking = -1;
int tag_depth = 5;
void dfs(ll now, int depth = 0, int rank = 0) {
  if (depth == tag_depth) {
    ranking++;
    cerr << now << endl;
    if (ranking == K) {
      cout << now << endl;
    }
  }

  if (now == 0) {
    FOR(i, 0, 10) dfs(i, depth + 1, rank);
  } else {
    ll ld = now % 10;
    if (ld != 0) {
      dfs((now * 10) + ld - 1, depth + 1, rank);
    }
    dfs((now * 10) + ld, depth + 1, rank);
    if (ld != 9)
      dfs((now * 10) + ld + 1, depth + 1, rank);
  }
}

void solve() {
  cin >> K;
  dfs(0, 5);
}

int main() {
  solve();
  return 0;
}
Problem p02720 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define dunk(n) cout << n << endl
#define all(v) v.begin(), v.end()

int main() {
  int k;
  cin >> k;

  queue<long long> q;
  rep(i, 9) q.push(i + 1);

  rep(i, k - 1) {
    long long x = q.front();
    q.pop();
    if (x % 10 != 0)
      q.push(x * 10 + x % 10 - 1);
    q.push(x * 10 + x % 10);
    if (x % 10 != 9)
      q.push(x * 10 + x % 10 + 1);
  }
  dunk(q.front());
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int lunlun(int K) {
  vector<long> n;
  int cnt = 0;
  for (long i = 1; i < 10; i += 1) {
    n.push_back(i);
    cnt++;
    if (cnt == K)
      goto OUT;
  }
  for (long i = 1; i < 10; i += 1) {

    int n1 = i * 10 + i - 1;
    n.push_back(n1);
    cnt++;
    if (cnt == K)
      goto OUT;

    int n2 = i * 10 + i;
    n.push_back(n2);
    cnt++;
    if (cnt == K)
      goto OUT;

    if (i != 9) {
      int n3 = i * 10 + i + 1;
      n.push_back(n3);
      cnt++;
      if (cnt == K)
        goto OUT;
    }
  }

OUT:
  return n.at(K - 1);
}

int main() {
  int K;
  cin >> K;

  cout << lunlun(K) << endl;
}
Problem p02720 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
typedef long long ll;
const int INF = 100;
typedef pair<int, int> P;
const int ld = 10;

int main() {
  int n;
  cin >> n;
  int a[ld];
  fill(a, a + ld, 0);
  // cout<<n<<endl;
  int i = 0;
  while (i < n) {
    // cout<<n<<" "<<endl;
    int jj = -1;
    for (int j = ld - 1; j >= 0; j--) {
      if (a[j] != 9) {
        a[j]++;
        jj = j;
        break;
      }
    }
    if (jj == -1)
      return 0;
    for (int j = jj + 1; j < ld; j++) {
      a[j] = max(0, a[j - 1] - (j - jj));
    }
    bool ng = false;
    for (int j = 1, st = 0; j < ld; j++) {
      if (a[j - 1] != 0)
        st = 1;
      if (st && abs(a[j - 1] - a[j]) > 1) {
        ng = true;
        break;
      }
    }
    if (!ng)
      i++;
  }
  for (int i = 0, st = 0; i < ld; i++) {
    if (a[i] != 0)
      st++;
    if (st)
      cout << a[i];
  }
  cout << endl;
}

Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
typedef long long ll;
const int INF = 100;
typedef pair<int, int> P;
const int ld = 10;

int main() {
  int n;
  cin >> n;
  int a[ld];
  fill(a, a + ld, 0);
  // cout<<n<<endl;
  int i = 0;
  while (i < n) {
    // cout<<n<<" "<<endl;
    int jj = -1;
    for (int j = ld - 1; j >= 0; j--) {
      if (a[j] != 9) {
        a[j]++;
        jj = j;
        break;
      }
    }
    if (jj == -1)
      return 0;
    for (int j = jj + 1; j < n; j++) {
      a[j] = max(0, a[j - 1] - (j - jj));
    }
    bool ng = false;
    for (int j = 1, st = 0; j < ld; j++) {
      if (a[j - 1] != 0)
        st = 1;
      if (st && abs(a[j - 1] - a[j]) > 1) {
        ng = true;
        break;
      }
    }
    if (!ng)
      i++;
  }
  for (int i = 0, st = 0; i < ld; i++) {
    if (a[i] != 0)
      st++;
    if (st)
      cout << a[i];
  }
  cout << endl;
}
Problem p02720 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define reprr(i, m, n) for (ll i = m; i < n; i++)
#define inf 2e9
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;

vll primes;

void generate_lunlun(string s) {
  if (primes.size() >= 2e6 || s.size() == 100)
    return;
  if (s[0] != '0')
    primes.push_back(stoll(s));

  ll num = s[0] - '0';
  if (num > 0)
    generate_lunlun(to_string(num - 1) + s);
  generate_lunlun(to_string(num) + s);
  if (num < 9)
    generate_lunlun(to_string(num + 1) + s);
}

int main() {
  queue<string> q;
  ll k;
  cin >> k;
  k--;
  rep(i, 10) q.push(to_string(i));

  while (q.size()) {
    if (primes.size() > 2e6)
      break;
    string s = q.front();
    q.pop();
    if (s[0] != '0')
      primes.push_back(stoll(s));

    ll num = s[0] - '0';
    if (num > 0)
      q.push(to_string(num - 1) + s);
    q.push(to_string(num) + s);
    if (num < 9)
      q.push(to_string(num + 1) + s);
  }

  sort(all(primes));
  cout << primes[k] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define reprr(i, m, n) for (ll i = m; i < n; i++)
#define inf 2e9
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;

vll primes;

void generate_lunlun(string s) {
  if (s.size() == 7)
    return;
  if (s[0] != '0')
    primes.push_back(stoll(s));

  ll num = s[0] - '0';
  if (num > 0)
    generate_lunlun(to_string(num - 1) + s);
  generate_lunlun(s[0] + s);
  if (num < 9)
    generate_lunlun(to_string(num + 1) + s);
}

int main() {
  ll k;
  cin >> k;
  k--;
  rep(i, 10) generate_lunlun(to_string(i));
  sort(all(primes));
  cout << primes[k] << endl;
  return 0;
}
Problem p02720 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int k;
  long ans;
  // scanf("%d", &x);
  cin >> k;
  if (k <= 10) {
    cout << k << endl;
    return 0;
  }
  queue<long> que;
  long i = 0;
  rep(j, 9) {
    que.push(i + 1);
    i++;
  }
  long next;
  long prev;
  while (i < k) {
    prev = que.front();
    que.pop();
    long d = prev % 10;
    if (d != 0) {
      next = prev * 10 + d - 1;
      que.push(next);
      i++;
    }
    if (i == k) {
      break;
    }
    next = prev * 10 + d;
    que.push(next);
    i++;
    if (i == k) {
      break;
    }
    if (d != 9) {
      next = prev * 10 + d + 1;
      que.push(next);
      i++;
    }
  }

  // printf("%d\\n", x * x * x);
  cout << next << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int k;
  long ans;
  // scanf("%d", &x);
  cin >> k;
  if (k <= 10) {
    cout << k << endl;
    return 1;
  }
  queue<long> que;
  long i = 0;
  rep(j, 9) {
    que.push(i + 1);
    i++;
  }
  long next;
  long prev;
  while (i < k) {
    prev = que.front();
    que.pop();
    long d = prev % 10;
    if (d != 0) {
      next = prev * 10 + d - 1;
      que.push(next);
      i++;
    }
    if (i == k) {
      break;
    }
    next = prev * 10 + d;
    que.push(next);
    i++;
    if (i == k) {
      break;
    }
    if (d != 9) {
      next = prev * 10 + d + 1;
      que.push(next);
      i++;
    }
  }

  // printf("%d\\n", x * x * x);
  cout << next << endl;
}
Problem p02720 (C++) — buggy submission, verdict: Runtime Error

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

const int N = 1e5 + 10;

int n, kol;
string a[N], s;

void f(int x, int m, int e) {

  if (x == m + 1) {
    kol += 1;
    if (kol == n) {
      cout << s << endl;
      exit(0);
    }
  } else {
    if (e == -1) {
      for (int i = 1; i <= 9; i++) {
        s += char('0' + i);
        f(x + 1, m, i);
        s.pop_back();
      }
    } else {
      for (int i = max(0, e - 1); i <= min(9, e + 1); i++) {
        s += char('0' + i);
        f(x + 1, m, i);
        s.pop_back();
      }
    }
  }
}

int main() {
  cin >> n;
  f(1, 1, -1);
  f(1, 2, -1);
  f(1, 3, -1);
  f(1, 4, -1);
  f(1, 5, -1);
  f(1, 6, -1);
  f(1, 7, -1);
  f(1, 8, -1);
  f(1, 9, -1);
  f(1, 10, -1);
  cout << a[n];
  return 0;
}

Error: -11

Fixed submission:

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

const int N = 1e5 + 10;

int n, kol;
string a[N], s;

void f(int x, int m, int e) {
  if (x == m + 1) {
    kol += 1;
    a[kol] = s;
    cout << x << " " << m << " " << e << endl;
  } else {
    if (e == -1) {
      for (int i = 1; i <= 9; i++) {
        s += char('0' + i);
        f(x + 1, m, i);
        s = s.substr(0, s.size() - 1);
      }
    } else {
      for (int i = max(0, e - 1); i <= min(9, e + 1); i++) {
        s += char('0' + i);
        f(x + 1, m, i);
        s = s.substr(0, s.size() - 1);
      }
    }
  }
}

int main() {
  cin >> n;
  f(1, 1, -1);
  f(1, 2, -1);
  f(1, 3, -1);
  f(1, 4, -1);
  f(1, 5, -1);
  f(1, 6, -1);
  f(1, 7, -1);
  f(1, 8, -1);
  f(1, 9, -1);
  f(1, 10, -1);
  cout << a[n];
  return 0;
}
Problem p02720 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  int k;
  cin >> k;

  queue<ll> que;

  for (int i = 1; i < 10; i++) {
    que.push(i);
  }
  int count = 0;
  while (1) {

    ll x = que.front();
    count++;

    if (count == k) {
      cout << x << endl;
      return 0;
    }
    que.pop();

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

    que.push(10 * x + (x % 10));

    if (x % 10 != 9) {
      que.push(10 * x + (x % 10) + 1);
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  int k;
  cin >> k;

  if (1 <= k && k <= 12) {
    cout << k << endl;
    return 0;
  }

  int count = 12; // runrun_numberの出現回数

  for (ll i = 13; i < 99999999; i++) {
    string runrun = to_string(i);
    for (int j = 0; j < runrun.size(); j++) {
      if (abs(runrun[j] - runrun[j + 1]) > 1 && j != runrun.size() - 1) {
        // cout << "a";
        break;
      }
      if (j == runrun.size() - 1 && count != k) {
        // cout << "b";
        count++;
      }
      if (j == runrun.size() - 1 && count == k) {
        // cout << "c";
        cout << i << endl;
        return 0;
      }
    }
  }
}
Problem p02720 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define int long long
using namespace std;
signed main() {
  int k;
  cin >> k;
  queue<int> q;
  for (int i = 1; i < 10; i++) {
    q.push(i);
  }
  int cnt = 0;
  int a;
  int b;
  while (cnt < k) {
    a = q.front();
    q.pop();
    cnt++;
    b = a % 10;
    if (b != 0)
      q.push(a * 10 + b - 1);
    q.push(a * 10 + b);
    if (b != 9)
      q.push(a * 10 + b + 1);
  }
  cout << a << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define int long long
using namespace std;
signed main() {
  int k;
  cin >> k;
  int cnt = 0;
  int x = 1;
  string s;
  bool flag;
  vector<int> v;
  while (!(cnt >= k)) {
    bool flag = false;
    int d = x;
    while (d > 0) {
      v.push_back(d % 10);
      d /= 10;
    }
    for (int i = 0; i < v.size() - 1; i++) {
      if (abs(v[i] - v[i + 1]) > 1) {
        flag = true;
        break;
      }
    }

    if (!flag) {
      cnt++;
    }
    v.clear();
    x++;
  }
  cout << x - 1 << endl;

  return 0;
}
Problem p02720 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[100] = {0}, cou = 0, flag = 0;
ll k, ans;
set<ll> se;
queue<ll> que;
void bfs(ll step) {
  que.push(step);
  ll m;
  while (!que.empty()) {
    m = que.front();
    que.pop();
    // cout<<m<<"\\n";
    for (int i = 0; i <= 9; i++) {
      if (abs(i - m % 10) <= 1 || m == 0) {
        if (m * 10 + i != 0) {
          que.push(m * 10 + i);
          cou++;
        }
      }
      if (cou == k) {
        ans = m * 10 + i;
        return;
      }
    }
  }
}
int main() {
  cin >> k;
  cou = 0;
  bfs(0);
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
#define ALL(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
const long long INF = numeric_limits<long long>::max();
int main() {
  bool check;
  long long int K, i = 0;
  scanf("%lld", &K);

  while (K != -1) {
    check = true;
    int j = i;

    while (j) {
      int d = abs((j % 10) - (j / 10) % 10);
      if (d > 1) {
        check = false;
        break;
      }
      j /= 10;
      if (j < 10)
        break;
    }
    if (i < 10)
      check = true;
    if (check == true)
      K--;
    if (K == -1)
      printf("%lld\\n", i);
    i++;
  }
}
Problem p02720 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n, m) for (int i = n; i < m; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)
#define ALL(v) v.begin(), v.end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)

using namespace std;
using ll = long long;

ll mod = 1000000007;

const ll INF = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int k;

int main() {
  cin >> k;
  queue<ll> q;
  reps(i, 9) q.push(i);
  ll ans;
  while (k, k--) {
    ans = q.front();
    q.pop();
    int p = ans % 10;
    if (p)
      q.push(ans * 10 + p - 1);
    q.push(ans * 10 + p);
    if (p != 9)
      q.push(ans * 10 + p + 1);
  }
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n, m) for (int i = n; i < m; i++)
#define reps(i, n) for (int i = 1; i <= n; i++)
#define ALL(v) v.begin(), v.end()
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)

using namespace std;
using ll = long long;

ll mod = 1000000007;

const ll INF = 1LL << 60;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int k;
string ans[100005];
int main() {
  cin >> k;
  rep(i, 12) { ans[i] = to_string(i + 1); }
  for (int i = 11; i < 100005; i++) {
    string s = ans[i];
    string t = s;

    int n = s.size();
    int cnt = 0;
    for (int j = n - 1; j > 0; j--) {
      if (s[j] == '9' && s[j - 1] == '9' && j == 1) {
        string str(n, '0');
        string str1 = "1";
        ans[i + 1] = str1 + str;
        break;
      }
      if (s[j] == '9' && s[j - 1] == '9' && j != 1)
        cnt++;
      if (s[j] <= s[j - 1] && s[j] != '9') {

        t[j] = char(s[j] + 1);
        int k = j;
        while (k < n - 1) {
          t[k + 1] = char(max(t[k] - 1, 48));
          k++;
        }
        ans[i + 1] = t;
        break;
      }
      if (s[j] == '9' && s[j - 1] == '8' && cnt == n - j) {
        t[j] = '1';
        for (int p = j + 1; p < n; p++)
          t[p] = '0';
        ans[i + 1] = t;
        break;
      }
      if (j == 1) {
        reverse(ALL(s));
        ans[i + 1] = s;
        break;
      }
    }
  }
  cout << ans[k] << endl;
}