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<bits/stdc++.h>
using namespace std;
struct node
{
	int sum;
	int s[100];
	
}p[100500];
void judge(int x)
{
		int i=0;
		int y=p[x-1].s[0];
		if(p[x-1].sum==1)
		{
			if(y+1<10)
			{
				p[x].s[0]=y+1;
				p[x].sum=1;
			}
			else
			{
				p[x].s[0]=0;
				p[x].s[1]=1;
				p[x].sum=2;
			}
		}
		else
		{
			if(y+1<10&&fabs(y+1-p[x-1].s[i+1])<=1)
			{
				memcpy(p[x].s,p[x-1].s,sizeof(p[x-1].s));
				p[x].s[0]++;
				p[x].sum=p[x-1].sum;
			}
			else
			{
				for(int j=1;j<p[x-1].sum;j++)
				{
					if(j==p[x-1].sum-1)
					{
						if(p[x-1].s[j]==9)
						{
							p[x].s[j+1]=1;
							for(int h=0;h<=j;h++)
							{
								p[x].s[h]=0;
							}
							p[x].sum=p[x-1].sum+1;
							break;
						}
						else
						{
							memcpy(p[x].s,p[x-1].s,sizeof(p[x-1].s));
							p[x].s[j]++;
							for(int h=j-1;h>=0;h--)
							{
								if(p[x].s[h+1]-1>=0)
								{
									p[x].s[h]=p[x].s[h+1]-1;
								}
								else
								{
									p[x].s[h]=0;
								}
							}
							p[x].sum=p[x-1].sum;
							break;
						}
					}
					else if(p[x-1].s[j]+1<10&&abs(p[x-1].s[j]+1-p[x-1].s[j+1])<=1)
					{
						
						memcpy(p[x].s,p[x-1].s,sizeof(p[x-1].s));
						p[x].s[j]++;
						for(int h=j-1;h>=0;h--)
						{
							if(p[x].s[h+1]-1>=0)
							{
								p[x].s[h]=p[x].s[h+1]-1;
							}
							else
							{
								p[x].s[h]=0;
							}
						}
						p[x].sum=p[x-1].sum;
						break;	
					}
				}	
			}
		}
}
int main()
{	
	int k;
	cin>>k;
	p[1].s[0]=1;
	p[1].sum=1;
	for(int i=2;i<=k;i++)
	{
		judge(i);
//		for(int j=p[i].sum-1;j>=0;j--)
//		{
//			cout<<p[i].s[j];
//		}
//		cout<<endl;
	}
	for(int i=p[k].sum-1;i>=0;i--)
	{
		cout<<p[k].s[i];
	}
	cout<<endl;
    return 0;
}