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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused_imports, unused_macros, dead_code)]

macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| min!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| max!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}
macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

const M: i64 = 1_000_000_007;

fn runrun(n: i64) -> bool {
    let mut n = n;
    let mut last = n % 10;
    n /= 10;
    while n > 0 {
        if ((n % 10) - last).abs() <= 1 {
            last = n % 10;
            n /= 10;
        } else {
            return false
        }
    }
    true
}

fn inc(x: &mut Vec<(usize, i32)>) {
    let n = x.len();
    if (0..n).all(|i| x[i].0 == 9) {
        x.push((0, 0));
        x[0] = (1, 0);
        x[1] = (0, -1);
        for i in 2..n + 1 {
            x[i] = (0, 0);
        }
        return;
    }
    if n == 1 {
        x[0].0 += 1;
        return;
    }
    {
        let mut k = n - 1;
        while k > 0 && (x[k].1 == 1 || x[k].0 == 9) { k -= 1 }
        x[k].0 += 1;
        x[k].1 += 1;
        for j in k+1..n {
            if x[j-1].0 > 0 {
                x[j] = (x[j-1].0 - 1, -1);
            } else {
                x[j] = (x[j-1].0, 0);
            }
        }
    }
}

fn main() {

    // let mut runs = vec![];
    // {
    //     for x in 1..300000 {
    //         if runrun(x) { runs.push(x) }
    //     }
    // }

    let mut sc = Scanner::new();
    let k: usize = sc.cin();
    let mut x = vec![(1, 0)];
    for i in 0..k-1 {
        // put!(i, runs[i], x.iter().map(|&(d, _)| d.to_string()).collect::<String>());
        inc(&mut x);
    }
    put!(x.iter().map(|&(d, _)| d.to_string()).collect::<String>());
}

use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;

struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
    fn new() -> Self { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
    fn chars(&mut self) -> Vec<char> { self.cin::<String>().chars().collect() }
    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T> { (0..n).map(|_| self.cin()).collect() }
}