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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll k, n, run;
  int kn, knm, f;
  cin >> k;
  run = 0;
  if (k < 20000) {
    run = 0;
    for (ll i = 1; i <= 76667876; i++) {
      n = i;
      kn = n % 10;
      f = 0;
      while (n != 0) {
        knm = kn;
        n /= 10;
        if (n == 0)
          break;
        kn = n % 10;
        if (abs(kn - knm) > 1) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        run++;
        if (run == k) {
          cout << i << endl;
          break;
        }
      }
    }
  } else if (k < 40000) {
    run = 20000 - 1;
    for (ll i = 76667876; i <= 434323221; i++) {
      n = i;
      kn = n % 10;
      f = 0;
      while (n != 0) {
        knm = kn;
        n /= 10;
        if (n == 0)
          break;
        kn = n % 10;
        if (abs(kn - knm) > 1) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        run++;
        if (run == k) {
          cout << i << endl;
          break;
        }
      }
    }
  } else if (k < 60000) {
    run = 40000 - 1;
    for (ll i = 434323221; i <= 777656545; i++) {
      n = i;
      kn = n % 10;
      f = 0;
      while (n != 0) {
        knm = kn;
        n /= 10;
        if (n == 0)
          break;
        kn = n % 10;
        if (abs(kn - knm) > 1) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        run++;
        if (run == k) {
          cout << i << endl;
          break;
        }
      }
    }
  } else if (k < 70000) {
    run = 60000 - 1;
    for (ll i = 777656545; i <= 1012101223; i++) {
      n = i;
      kn = n % 10;
      f = 0;
      while (n != 0) {
        knm = kn;
        n /= 10;
        if (n == 0)
          break;
        kn = n % 10;
        if (abs(kn - knm) > 1) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        run++;
        if (run == k) {
          cout << i << endl;
          break;
        }
      }
    }
  } else if (k < 79725) {
    run = 70000 - 1;
    for (ll i = 1012101223; i <= 1234567899; i++) {
      n = i;
      kn = n % 10;
      f = 0;
      while (n != 0) {
        knm = kn;
        n /= 10;
        if (n == 0)
          break;
        kn = n % 10;
        if (abs(kn - knm) > 1) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        run++;
        if (run == k) {
          cout << i << endl;
          break;
        }
      }
    }
  } else if (k < 80000) {
    run = 79725 - 1;
    for (ll i = 2100000000; i <= 2101000111; i++) {
      n = i;
      kn = n % 10;
      f = 0;
      while (n != 0) {
        knm = kn;
        n /= 10;
        if (n == 0)
          break;
        kn = n % 10;
        if (abs(kn - knm) > 1) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        run++;
        if (run == k) {
          cout << i << endl;
          break;
        }
      }
    }
  } else if (k < 90000) {
    run = 80000 - 1;
    for (ll i = 2101000111 + 1; i <= 2323212101; i++) {
      n = i;
      kn = n % 10;
      f = 0;
      while (n != 0) {
        knm = kn;
        n /= 10;
        if (n == 0)
          break;
        kn = n % 10;
        if (abs(kn - knm) > 1) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        run++;
        if (run == k) {
          cout << i << endl;
          break;
        }
      }
    }
  } else if (k < 94829) {
    run = 90000 - 1;
    for (ll i = 2323212101; i <= 2345678999; i++) {
      n = i;
      kn = n % 10;
      f = 0;
      while (n != 0) {
        knm = kn;
        n /= 10;
        if (n == 0)
          break;
        kn = n % 10;
        if (abs(kn - knm) > 1) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        run++;
        if (run == k) {
          cout << i << endl;
          break;
        }
      }
    }
  } else if (k <= 100000) {
    run = 94829 - 1;
    for (ll i = 3210000000; i <= 3234566667; i++) {
      n = i;
      kn = n % 10;
      f = 0;
      while (n != 0) {
        knm = kn;
        n /= 10;
        if (n == 0)
          break;
        kn = n % 10;
        if (abs(kn - knm) > 1) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        run++;
        if (run == k) {
          cout << i << endl;
          break;
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll k, n, run;
  int kn, knm, f;
  cin >> k;
  run = 0;
  if (k < 20000) {
    run = 0;
    for (ll i = 1; i <= 76667876; i++) {
      n = i;
      kn = n % 10;
      f = 0;
      while (n != 0) {
        knm = kn;
        n /= 10;
        if (n == 0)
          break;
        kn = n % 10;
        if (abs(kn - knm) > 1) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        run++;
        if (run == k) {
          cout << i << endl;
          break;
        }
      }
    }
  } else if (k < 40000) {
    run = 20000 - 1;
    for (ll i = 76667876; i <= 434323221; i++) {
      n = i;
      kn = n % 10;
      f = 0;
      while (n != 0) {
        knm = kn;
        n /= 10;
        if (n == 0)
          break;
        kn = n % 10;
        if (abs(kn - knm) > 1) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        run++;
        if (run == k) {
          cout << i << endl;
          break;
        }
      }
    }
  } else if (k < 60000) {
    run = 40000 - 1;
    for (ll i = 434323221; i <= 777656545; i++) {
      n = i;
      kn = n % 10;
      f = 0;
      while (n != 0) {
        knm = kn;
        n /= 10;
        if (n == 0)
          break;
        kn = n % 10;
        if (abs(kn - knm) > 1) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        run++;
        if (run == k) {
          cout << i << endl;
          break;
        }
      }
    }
  } else if (k < 70000) {
    run = 60000 - 1;
    for (ll i = 777656545; i <= 1012101223; i++) {
      n = i;
      kn = n % 10;
      f = 0;
      while (n != 0) {
        knm = kn;
        n /= 10;
        if (n == 0)
          break;
        kn = n % 10;
        if (abs(kn - knm) > 1) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        run++;
        if (run == k) {
          cout << i << endl;
          break;
        }
      }
    }
  } else if (k < 75000) {
    run = 70000 - 1;
    for (ll i = 1012101223; i <= 1211122112; i++) {
      n = i;
      kn = n % 10;
      f = 0;
      while (n != 0) {
        knm = kn;
        n /= 10;
        if (n == 0)
          break;
        kn = n % 10;
        if (abs(kn - knm) > 1) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        run++;
        if (run == k) {
          cout << i << endl;
          break;
        }
      }
    }
  } else if (k < 80000) {
    run = 75000 - 1;
    for (ll i = 1211122112; i <= 2101000111; i++) {
      n = i;
      kn = n % 10;
      f = 0;
      while (n != 0) {
        knm = kn;
        n /= 10;
        if (n == 0)
          break;
        kn = n % 10;
        if (abs(kn - knm) > 1) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        run++;
        if (run == k) {
          cout << i << endl;
          break;
        }
      }
    }
  } else if (k < 90000) {
    run = 80000 - 1;
    for (ll i = 2101000111; i <= 2323212101; i++) {
      n = i;
      kn = n % 10;
      f = 0;
      while (n != 0) {
        knm = kn;
        n /= 10;
        if (n == 0)
          break;
        kn = n % 10;
        if (abs(kn - knm) > 1) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        run++;
        if (run == k) {
          cout << i << endl;
          break;
        }
      }
    }
  } else if (k < 95000) {
    run = 90000 - 1;
    for (ll i = 2323212101; i <= 3210112121; i++) {
      n = i;
      kn = n % 10;
      f = 0;
      while (n != 0) {
        knm = kn;
        n /= 10;
        if (n == 0)
          break;
        kn = n % 10;
        if (abs(kn - knm) > 1) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        run++;
        if (run == k) {
          cout << i << endl;
          break;
        }
      }
    }
  } else if (k <= 100000) {
    run = 95000 - 1;
    for (ll i = 3210112121; i <= 3234566667; i++) {
      n = i;
      kn = n % 10;
      f = 0;
      while (n != 0) {
        knm = kn;
        n /= 10;
        if (n == 0)
          break;
        kn = n % 10;
        if (abs(kn - knm) > 1) {
          f = 1;
          break;
        }
      }
      if (f == 0) {
        run++;
        if (run == k) {
          cout << i << endl;
          break;
        }
      }
    }
  }
  return 0;
}