Problem p02720 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

#define SINV(t) greater<t>()
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP2(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define ALL(x) (x).begin(), (x).end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
#define vv(t) vector<t>

template <typename T> void SV(vector<T> v) {
  REP(i, v.size()) {
#ifdef LOCAL
    cout << i << " : " << v[i] << endl;
#endif
  }
}

int bitCount(int bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
  bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
  return (bits & 0x0000ffff) + (bits >> 16);
}

int main() {
  int K;
  cin >> K;
  ll n = 1;
  vv(int) s(10);
  s[0] = 1;
  REP(i, K) {
    ll tn = n;
    ll ehig = 1;
    /*
    while(tn/10){
        cout << "!"<< tn%10 << (tn/10)%10 << endl;
        if(abs(tn%10-(tn/10)%10) > 1){

        }
    }*/
    tn = 0;
    ll sss = 10;
    for (int j = 1; j < 10 && (n / sss || s[j - 1] > 9); j++, sss *= 10) {
      if (s[j - 1] > 9) {
        s[j]++;
        s[j - 1] = 0;
        tn = j;
      } else if (abs(s[j] - s[j - 1]) > 1) {
        s[j]++;
        n += sss;
        tn = j;
      } else {
        break;
      }
    }
    // cout << tn << endl;
    for (int j = tn, cnt = s[tn]; j >= 0; j--, cnt--) {
      if (cnt < 0)
        cnt = 0;
      s[j] = cnt;
    }
    // if(i > 50)
    // cout << "--------"<< n << endl;
    n = 0;
    sss = 1;
    for (int j = 0; j < 10; j++) {
      n += sss * s[j];
      sss *= 10;
    }
    s[0]++;
    // cout << n << endl;
  }
  cout << n << endl;
  //    cout << s[0]+s[1]*10+s[2]*100+s[3]*1000*s[4]*10000 << endl;;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

#define SINV(t) greater<t>()
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP2(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define ALL(x) (x).begin(), (x).end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

typedef long long ll;
#define vv(t) vector<t>

template <typename T> void SV(vector<T> v) {
  REP(i, v.size()) {
#ifdef LOCAL
    cout << i << " : " << v[i] << endl;
#endif
  }
}

int bitCount(int bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
  bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
  return (bits & 0x0000ffff) + (bits >> 16);
}

int main() {
  int K;
  cin >> K;
  ll n = 0;
  REP(i, K) {
    // n++;
    ll tn = n;
    ll ehig = 1;
    /*
    while(tn/10){
        cout << "!"<< tn%10 << (tn/10)%10 << endl;
        if(abs(tn%10-(tn/10)%10) > 1){
            tn+=8;
        }
        ehig*=11;
        tn/=10;
    }
    */
    while (1) {
      n++;
      tn = n;
      bool flg = false;
      while (tn / 10) {
        if (abs(tn % 10 - (tn / 10) % 10) > 1) {
          flg = true;
        }
        tn /= 10;
      }
      if (!flg)
        break;
    }
    // if(i > 50)
    // cout << "--------"<< n << endl;
  }
  cout << n << endl;
  ;
}