Problem:
Score : 400 points

Problem Statement

A positive integer X is said to be a lunlun number if and only if the following condition is satisfied:

In the base ten representation of X (without leading zeros), for every pair of two adjacent digits, the absolute difference of those digits is at most 1.

For example, 1234, 1, and 334 are lunlun numbers, while none of 31415, 119, or 13579 is.

You are given a positive integer K. Find the K-th smallest lunlun number.

Constraints

1 \leq K \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K

Output

Print the answer.

Sample Input 1

15

Sample Output 1

23

We will list the 15 smallest lunlun numbers in ascending order:

1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
21,
22,
23.

Thus, the answer is 23.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

13

Sample Output 3

21

Sample Input 4

100000

Sample Output 4

3234566667

Note that the answer may not fit into the 32-bit signed integer type.

Solution in C (GCC 9.2.1) (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define MAX_DATA 100001
typedef long long ll;

/*データ構造queueを実装*/
struct queue
{
    ll arr[MAX_DATA];
    int first; //次にpopする配列の添え字
    int last;  //次にpushする配列の添え字
};

ll push(struct queue *que, ll input)
{
    if (que->arr[que->last] <= MAX_DATA)
    {
        que->arr[que->last] = input;

        // 添え字の更新
        que->last++;

        //printf("push:%lld\n", input);
        return 0;
    }
    else
    {
        //printf("これ以上データを追加できません。\n");
        return -1;
    }
}

ll pop(struct queue *que)
{
    ll output = que->arr[que->first];

    if (output != -1)
    {
        que->arr[que->first] = -1;

        // 添え字の更新
        que->first++;

        //printf("pop:%lld\n", output);
        return output;
    }
    else
    {
        //printf("データがありません。\n");
        return -1;
    }
}

/*queue初期化*/
void init_queue(struct queue *que)
{
    int i;

    for (i = 0; i < MAX_DATA; i++)
        que->arr[i] = -1;

    que->first = 0;
    que->last = 0;
}

/*queueの確認*/
void show_queue(struct queue *que)
{
    int i;
    for (i = 0; i < MAX_DATA; i++)
    {
        if (que->arr[i] != -1)
            printf("%lld ", que->arr[i]);
    }

    printf("\nfirst:%d last%d\n\n", que->first, que->last);
}

/*メイン関数*/
int main(void)
{
    int k;
    ll lunlun[100001]; // ルンルン数保存用配列
    int lunlunCnt; // ルンルン数配列の添え字
    struct queue que;

    scanf("%d", &k);
    if (k < 10) // kが9以下ならそのまま出力
    {
        printf("%d\n", k);
        return 0;
    }

    /*1から9をあらかじめpushしておく*/
    init_queue(&que);
    for (int i = 1; i <= 9; i++)
        push(&que, (ll)i);

    /*1から9はあらかじめ代入しておく*/
    for (lunlunCnt = 0; lunlunCnt < 9; lunlunCnt++)
        lunlun[lunlunCnt] = lunlunCnt + 1;

    /*queueが空になるまでループ*/
    while (que.first != que.last)
    {
        /*popした数値の末尾に数をくっつけて新たなルンルン数を生成*/
        ll source = pop(&que); // もとになる数
        for (int i = -1; i <= 1; i++) // 絶対値が1以下
        {
            ll tmp = source % 10 + i; // 末尾の値

            if (tmp < 0 || tmp > 9) // 条件を満たさないならスキップ
                continue;

            /*sourceを10倍して末尾にtmpをくっつける*/
            ll num = source * 10 + (ll)tmp;

            /*保存用配列とqueueに新たなルンルン数を代入*/
            lunlun[lunlunCnt++] = num;
            push(&que, num);
        }
        if (lunlunCnt > k)
            break;
    }

    printf("%lld\n", lunlun[k - 1]);

    return 0;
}