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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define sc(x) scanf("%lld", &x);
#define fi first
#define se second
#define endl '\\n'
#define P pair<int, int>
priority_queue<int> pq;
bool check(int x) {
  int y = x;
  int p = x % 10;
  while (x) {
    if (abs(x % 10 - p) > 1) {
      return false;
    }
    p = x % 10;
    x /= 10;
  }
  return true;
}
int A[] = {
    0,          4445,       21122,      45434,      76667,      110111,
    221122,     323234,     432332,     456556,     565566,     667655,
    776767,     887765,     1001012,    1122111,    1232232,    2122234,
    2233211,    2334544,    3223212,    3332332,    3434343,    4321222,
    4345456,    4454545,    4556555,    5443345,    5545433,    5654444,
    5677899,    6565565,    6667654,    6776766,    7665434,    7766767,
    7877666,    8767676,    8877789,    8998887,    9987676,    10101001,
    11010111,   11123221,   11233334,   12123344,   12234321,   12343432,
    21110101,   21221112,   22111101,   22222100,   22323433,   23211223,
    23321212,   23345454,   23454543,   32122323,   32233222,   32334555,
    33223223,   33332343,   33434432,   34321233,   34345545,   34454556,
    34556566,   43222321,   43332211,   43433444,   44321101,   44344565,
    44454432,   44555656,   45443232,   45544456,   45654323,   45677778,
    54343323,   54444556,   54554344,   55433434,   55543222,   55566676,
    55676544,   56555654,   56665443,   56766776,   65433232,   65456678,
    65566544,   65667777,   66555444,   66656677,   66766566,   67654543,
    67678777,   67788766,   67899987,   76566565,   76667876,   76777766,
    77665656,   77767767,   77877888,   78767898,   78878876,   78999987,
    87677677,   87787666,   87898788,   88787789,   88898998,   89887778,
    98765666,   98876565,   98988876,   99898776,   100012110,  101000100,
    101121210,  101232110,  110101210,  111011110,  111123343,  111233456,
    112210001,  112234443,  112344332,  121111100,  121221234,  122111223,
    122222222,  122332112,  123212210,  123322121,  123345654,  123455443,
    210111100,  211012222,  211211010,  211234567,  212211212,  212321123,
    212345443,  221112212,  221223210,  222112343,  222223333,  222333223,
    223221112,  223323232,  223432322,  223456554,  232210100,  232234455,
    232344344,  233232321,  233334432,  233443443,  234323323,  234432343,
    234456567,  234565655,  321110111,  321221122,  322111111,  322222110,
    322323443,  323211233,  323321222,  323345456,  323454545,  332122333,
    332233232,  332334565,  333223233,  333332345,  333434434,  334322100,
    334345555,  334454566,  334556654,  343222323,  343332221,  343433454,
    344321111,  344344567,  344454434,  344555666,  345443234,  345544544,
    345654333,  345677788,  432123454,  432234345,  432343456,  433232211,
    433334322,  433443333,  434323221,  434432233,  434456543,  434565545,
    443223444,  443333334,  443434567,  444322233,  444432101,  444455555,
    444556787,  445444433,  445545665,  445655454,  445678989,  454344454,
    454454321,  454555545,  455434565,  455544345,  455567877,  455677667,
    456556777,  456666566,  456767899,  543221010,  543322345,  543432221,
    543455667,  544343334,  544444567,  544554433,  545433445,  545543233,
    545566765,  545676555,  554334322,  554443333,  554544565,  555432211,
    555455665,  555565445,  555666678,  556554345,  556655656,  556765545,
    556789987,  565455455,  565556765,  565666554,  566545665,  566655454,
    566678989,  566788899,  567676767,  567778887,  567889898,  654343221,
    654444454,  654545678,  655433332,  655456778,  655566566,  655667877,
    656555544,  656656777,  656766666,  665432344,  665456654,  665565656,
    665667667,  666554556,  666656567,  666765678,  667654433,  667677888,
    667787877,  667899876,  676565677,  676667766,  676777656,  677665554,
    677767665,  677877778,  678767788,  678878766,  678999876,  765454567,
    765556655,  765665666,  766545555,  766654566,  766678878,  766788789,
    767676665,  767778777,  767888998,  776556676,  776666543,  776767876,
    777656545,  777765656,  777876555,  778766565,  778876678,  778988998,
    787667787,  787777677,  787887889,  788777878,  788888877,  789877789,
    789998887,  876565565,  876667654,  876776766,  877665434,  877766767,
    877877666,  878767676,  878877789,  878998887,  887676677,  887778789,
    887889878,  888778999,  888898767,  889878988,  898765444,  898789877,
    898987789,  899888998,  987656543,  987765654,  987876545,  988766555,
    988876676,  988988988,  989898887,  998776544,  998878887,  998999998,
    999988766,  1000110000, 1001011221, 1001123454, 1001234345, 1010112233,
    1011101100, 1011212222, 1012101223, 1012212345, 1012322334, 1100000111,
    1100121221, 1101110121, 1101221210, 1110011121, 1110123432, 1111112345,
    1111223343, 1112121123, 1112232101, 1112333434, 1121012233, 1121211100,
    1122100010, 1122211223, 1122321212, 1122345454, 1123233345, 1123343234,
    1123444545, 1210012233, 1211001100, 1211122112, 1211232233, 1212122321,
    1212233212, 1212334545, 1221101011, 1221212212, 1222101221, 1222212343,
    1222322332, 1223210101, 1223234456, 1223344345, 1223445656, 1232121221,
    1232232121, 1232333454, 1233222122, 1233323455, 1233433323, 1233456777,
    1234344444, 1234445677, 1234555543, 2100012121, 2101000111, 2101121221,
    2101232121, 2110101221, 2111011121, 2111123432, 2111234323, 2112210012,
    2112234454, 2112344343, 2121111111, 2121222110, 2122111234, 2122222233,
    2122332123, 2123212221, 2123322210, 2123345665, 2123455454, 2210111111,
    2211012233, 2211211100, 2212100010, 2212211223, 2212321212, 2212345454,
    2221112223, 2221223221, 2222121000, 2222223344, 2222333234, 2223221123,
    2223323321, 2223432333, 2223456565, 2232210111, 2232234544, 2232344433,
    2233232332, 2233334443, 2233443454, 2234323334, 2234433210, 2234456656,
    2234565666, 2321110122, 2321221211, 2322111122, 2322222121, 2322323454,
    2323212101, 2323321233, 2323345545, 2323454556, 2332122344, 2332233321,
    2332343210, 2333223322, 2333333212, 2333434445, 2334322111, 2334345566,
    2334455433, 2334556665, 2343222334, 2343332232, 2343434321, 2344321122,
    2344345434, 2344454445, 2344555677, 2345443323, 2345544555, 2345654344,
    2345677877, 3210112121, 3211100111, 3211212110, 3212101111, 3212212233,
    3212322222, 3212345677, 3221121101, 3221223444, 3222121232, 3222232210,
    3222334321, 3223222211, 3223332101, 3223433334, 3223456788, 3232211211,
    3232321122, 3232345434, 3233233333, 3233343222, 3233444455, 3234332122,
    3234433433, 3234543221, 3234566667,
};
vector<int> v;
int B[15];
void init() {
  B[0] = 1;
  for (int i = 1; i < 15; i++) {
    B[i] = B[i - 1] * 10;
  }
}
signed main() {
  int n;
  init();
  sc(n) int x = n / 200;
  n = n % 200;
  if (n == 0) {
    cout << A[x] << '\\n';
  } else {
    int j = A[x] + 1;
    int k = 0, va;
    while (k < n) {
      for (j;; j++) {
        if (check(j)) {
          k++;
          va = j;
          j++;
          break;
        }
        int t = j;
        if (j > 100000) {
          v.clear();
          while (t) {
            v.pb(t % 10);
            t /= 10;
          }
          for (int _ = v.size() - 1; _ > 0; _--) {
            if (abs(v[_] - v[_ - 1]) > 1) {
              j -= j % B[_ - 1];
              j += B[_ - 1] - 1;

              break;
            }
          }
        }
      }
    }
    cout << va << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define sc(x) scanf("%lld", &x);
#define fi first
#define se second
#define endl '\\n'
#define P pair<int, int>
priority_queue<int> pq;
bool check(int x) {
  int y = x;
  int p = x % 10;
  while (x) {
    if (abs(x % 10 - p) > 1) {
      return false;
    }
    p = x % 10;
    x /= 10;
  }
  return true;
}
int A[] = {
    0,          4445,       21122,      45434,      76667,      110111,
    221122,     323234,     432332,     456556,     565566,     667655,
    776767,     887765,     1001012,    1122111,    1232232,    2122234,
    2233211,    2334544,    3223212,    3332332,    3434343,    4321222,
    4345456,    4454545,    4556555,    5443345,    5545433,    5654444,
    5677899,    6565565,    6667654,    6776766,    7665434,    7766767,
    7877666,    8767676,    8877789,    8998887,    9987676,    10101001,
    11010111,   11123221,   11233334,   12123344,   12234321,   12343432,
    21110101,   21221112,   22111101,   22222100,   22323433,   23211223,
    23321212,   23345454,   23454543,   32122323,   32233222,   32334555,
    33223223,   33332343,   33434432,   34321233,   34345545,   34454556,
    34556566,   43222321,   43332211,   43433444,   44321101,   44344565,
    44454432,   44555656,   45443232,   45544456,   45654323,   45677778,
    54343323,   54444556,   54554344,   55433434,   55543222,   55566676,
    55676544,   56555654,   56665443,   56766776,   65433232,   65456678,
    65566544,   65667777,   66555444,   66656677,   66766566,   67654543,
    67678777,   67788766,   67899987,   76566565,   76667876,   76777766,
    77665656,   77767767,   77877888,   78767898,   78878876,   78999987,
    87677677,   87787666,   87898788,   88787789,   88898998,   89887778,
    98765666,   98876565,   98988876,   99898776,   100012110,  101000100,
    101121210,  101232110,  110101210,  111011110,  111123343,  111233456,
    112210001,  112234443,  112344332,  121111100,  121221234,  122111223,
    122222222,  122332112,  123212210,  123322121,  123345654,  123455443,
    210111100,  211012222,  211211010,  211234567,  212211212,  212321123,
    212345443,  221112212,  221223210,  222112343,  222223333,  222333223,
    223221112,  223323232,  223432322,  223456554,  232210100,  232234455,
    232344344,  233232321,  233334432,  233443443,  234323323,  234432343,
    234456567,  234565655,  321110111,  321221122,  322111111,  322222110,
    322323443,  323211233,  323321222,  323345456,  323454545,  332122333,
    332233232,  332334565,  333223233,  333332345,  333434434,  334322100,
    334345555,  334454566,  334556654,  343222323,  343332221,  343433454,
    344321111,  344344567,  344454434,  344555666,  345443234,  345544544,
    345654333,  345677788,  432123454,  432234345,  432343456,  433232211,
    433334322,  433443333,  434323221,  434432233,  434456543,  434565545,
    443223444,  443333334,  443434567,  444322233,  444432101,  444455555,
    444556787,  445444433,  445545665,  445655454,  445678989,  454344454,
    454454321,  454555545,  455434565,  455544345,  455567877,  455677667,
    456556777,  456666566,  456767899,  543221010,  543322345,  543432221,
    543455667,  544343334,  544444567,  544554433,  545433445,  545543233,
    545566765,  545676555,  554334322,  554443333,  554544565,  555432211,
    555455665,  555565445,  555666678,  556554345,  556655656,  556765545,
    556789987,  565455455,  565556765,  565666554,  566545665,  566655454,
    566678989,  566788899,  567676767,  567778887,  567889898,  654343221,
    654444454,  654545678,  655433332,  655456778,  655566566,  655667877,
    656555544,  656656777,  656766666,  665432344,  665456654,  665565656,
    665667667,  666554556,  666656567,  666765678,  667654433,  667677888,
    667787877,  667899876,  676565677,  676667766,  676777656,  677665554,
    677767665,  677877778,  678767788,  678878766,  678999876,  765454567,
    765556655,  765665666,  766545555,  766654566,  766678878,  766788789,
    767676665,  767778777,  767888998,  776556676,  776666543,  776767876,
    777656545,  777765656,  777876555,  778766565,  778876678,  778988998,
    787667787,  787777677,  787887889,  788777878,  788888877,  789877789,
    789998887,  876565565,  876667654,  876776766,  877665434,  877766767,
    877877666,  878767676,  878877789,  878998887,  887676677,  887778789,
    887889878,  888778999,  888898767,  889878988,  898765444,  898789877,
    898987789,  899888998,  987656543,  987765654,  987876545,  988766555,
    988876676,  988988988,  989898887,  998776544,  998878887,  998999998,
    999988766,  1000110000, 1001011221, 1001123454, 1001234345, 1010112233,
    1011101100, 1011212222, 1012101223, 1012212345, 1012322334, 1100000111,
    1100121221, 1101110121, 1101221210, 1110011121, 1110123432, 1111112345,
    1111223343, 1112121123, 1112232101, 1112333434, 1121012233, 1121211100,
    1122100010, 1122211223, 1122321212, 1122345454, 1123233345, 1123343234,
    1123444545, 1210012233, 1211001100, 1211122112, 1211232233, 1212122321,
    1212233212, 1212334545, 1221101011, 1221212212, 1222101221, 1222212343,
    1222322332, 1223210101, 1223234456, 1223344345, 1223445656, 1232121221,
    1232232121, 1232333454, 1233222122, 1233323455, 1233433323, 1233456777,
    1234344444, 1234445677, 1234555543, 2100012121, 2101000111, 2101121221,
    2101232121, 2110101221, 2111011121, 2111123432, 2111234323, 2112210012,
    2112234454, 2112344343, 2121111111, 2121222110, 2122111234, 2122222233,
    2122332123, 2123212221, 2123322210, 2123345665, 2123455454, 2210111111,
    2211012233, 2211211100, 2212100010, 2212211223, 2212321212, 2212345454,
    2221112223, 2221223221, 2222121000, 2222223344, 2222333234, 2223221123,
    2223323321, 2223432333, 2223456565, 2232210111, 2232234544, 2232344433,
    2233232332, 2233334443, 2233443454, 2234323334, 2234433210, 2234456656,
    2234565666, 2321110122, 2321221211, 2322111122, 2322222121, 2322323454,
    2323212101, 2323321233, 2323345545, 2323454556, 2332122344, 2332233321,
    2332343210, 2333223322, 2333333212, 2333434445, 2334322111, 2334345566,
    2334455433, 2334556665, 2343222334, 2343332232, 2343434321, 2344321122,
    2344345434, 2344454445, 2344555677, 2345443323, 2345544555, 2345654344,
    2345677877, 3210112121, 3211100111, 3211212110, 3212101111, 3212212233,
    3212322222, 3212345677, 3221121101, 3221223444, 3222121232, 3222232210,
    3222334321, 3223222211, 3223332101, 3223433334, 3223456788, 3232211211,
    3232321122, 3232345434, 3233233333, 3233343222, 3233444455, 3234332122,
    3234433433, 3234543221, 3234566667,
};
signed main() {
  int n;
  sc(n) int x = n / 200;
  n = n % 200;
  if (n == 0) {
    cout << A[x] << '\\n';
  } else {
    int j = A[x] + 1;
    int k = 0, v;
    while (k < n) {
      for (j;; j++) {
        if (check(j)) {
          k++;
          v = j;
          j++;
          break;
        }
      }
    }
    cout << v << '\\n';
  }
}