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

#include <bits/stdc++.h>
using namespace std;

#define int long long

int a[5005] = {
    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};
int b[1445] = {
    12345678,   21000000,   23456789,   32100000,   34567899,   43210000,
    45678999,   54321000,   56789999,   65432100,   67899999,   76543210,
    78999999,   87654321,   89999999,   98765432,   101234567,  110000000,
    112345678,  121000000,  123456789,  210000000,  212345678,  221000000,
    223456789,  232100000,  234567899,  321000000,  323456789,  332100000,
    334567899,  343210000,  345678999,  432100000,  434567899,  443210000,
    445678999,  454321000,  456789999,  543210000,  545678999,  554321000,
    556789999,  565432100,  567899999,  654321000,  656789999,  665432100,
    667899999,  676543210,  678999999,  765432100,  767899999,  776543210,
    778999999,  787654321,  789999999,  876543210,  878999999,  887654321,
    889999999,  898765432,  899999999,  987654321,  989999999,  998765432,
    1001234567, 1010000000, 1012345678, 1100000000, 1101234567, 1110000000,
    1112345678, 1121000000, 1123456789, 1210000000, 1212345678, 1221000000,
    1223456789, 1232100000, 1234567899, 2100000000, 2101234567, 2110000000,
    2112345678, 2121000000, 2123456789, 2210000000, 2212345678, 2221000000,
    2223456789, 2232100000, 2234567899, 2321000000, 2323456789, 2332100000,
    2334567899, 2343210000, 2345678999, 3210000000, 3212345678, 3221000000,
    3223456789, 3232100000};

map<int, int> mb;

int check(int x) {
  int last = x % 10;
  x /= 10;
  while (x) {
    int p = x % 10;
    x /= 10;
    if (abs(last - p) > 1)
      return 0;
    last = p;
  }
  return 1;
}

// 2100000000,3210000000,

signed main() {
  int ind = 0;
  int n, p;
  cin >> n;
  p = n / 200;
  int cnt = 0;
  for (int i = 0; i <= 1000; i += 2)
    mb[b[i]] = b[i + 1];
  for (int i = a[p];; i++) {
    if (check(i)) {
      if (cnt == n % 200) {
        cout << i;
        return 0;
      }
      ++cnt;
    }
    if (mb[i])
      i = mb[i] - 2;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long

int a[5005] = {
    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};

int check(int x) {
  int last = x % 10;
  x /= 10;
  while (x) {
    int p = x % 10;
    x /= 10;
    if (abs(last - p) > 1)
      return 0;
    last = p;
  }
  return 1;
}

signed main() {
  int ind = 0;
  int n, p;
  cin >> n;
  p = n / 200;
  int cnt = 0;
  for (int i = a[p];; i++) {
    if (check(i)) {
      if (cnt == n % 200) {
        cout << i;
        return 0;
      }
      ++cnt;
    }
  }
}