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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>
#include <unordered_map>
#include <cmath>
using namespace std;

typedef unsigned int uint;
typedef long long llong;
typedef unsigned long long ullong;
// -2.1e-9 <= int <= 2.1e9
// -9.2e-18 <= long long <= 9.2e18
// 0 <= unsigned int <= 4.2e9
// 0 <= unsigned long long <= 18.4e18


int main()
{
    int K; cin >> K;
    if (K < 10) {
        cout << K << endl;
        return 0;
    }
    queue<pair<llong, llong>> Q;
    for (int i=1; i<10; i++) {
        Q.push(make_pair(i, i));
    }
    int num_lunlun = 9;
    pair<llong, llong> ans;
    while( !Q.empty() ) {
        auto parent = Q.front(); Q.pop();
        pair<llong, llong> l_child, c_child, r_child;

        if (parent.second > 0) {
            l_child.second = parent.second - 1;
            l_child.first = parent.first * 10 + l_child.second;
            num_lunlun++;
            ans = l_child;
            Q.push(l_child);
            //cout << num_lunlun << ", left :" << ans.first << endl;
            if (num_lunlun >= K) {
                break;
            }
        }

        c_child.second = parent.second;
        c_child.first = parent.first * 10 + c_child.second;
        num_lunlun++;
        ans = c_child;
        Q.push(c_child);
        //cout << num_lunlun << ", center :" << ans.first << endl;
        if (num_lunlun >= K) {
            break;
        }

        if (parent.second < 9) {
            r_child.second = parent.second + 1;
            r_child.first = parent.first * 10 + r_child.second;
            num_lunlun++;
            ans = r_child;
            Q.push(r_child);
            //cout << num_lunlun << ", right :" << ans.first << endl;
            if (num_lunlun >= K) {
                break;
            }
        }
    }
    cout << ans.first << endl;
}